Page 1 of 1

Table in HTML box

Posted: Tue Jan 08, 2019 2:27 pm
by Janusz
I'am trying to display table in HTML box.

I am testing it based on the following example.
http://webdevzoom.com/display-mysql-dat ... using-php/

There is no issue with the HTML part and I can display the table itself,
But I am not able to properly connect and read data from the table.

Can you please give me some advice how to replace the following part of code in order to be able to run it from nuBuilder HTML box?

Code: Select all

<?php
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = ''; // Password
$db_name = 'tutorial'; // Database Name

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
	die ('Failed to connect to MySQL: ' . mysqli_connect_error());	
}
$sql = 'SELECT * 
		FROM sales';
$query = mysqli_query($conn, $sql);

if (!$query) {
	die ('SQL Error: ' . mysqli_error($conn));
}
?>

		<?php
		$no 	= 1;
		$total 	= 0;
		while ($row = mysqli_fetch_array($query))
		{
			$amount  = $row['amount'] == 0 ? '' : number_format($row['amount']);
			echo '<tr>
					<td>'.$no.'</td>
					<td>'.$row['name'].'</td>
					<td>'.$row['item'].'</td>
					<td>'. date('F d, Y', strtotime($row['date'])) . '</td>
					<td>'.$amount.'</td>
				</tr>';
			$total += $row['amount'];
			$no++;
		}?>

Re: Table in HTML box

Posted: Tue Jan 08, 2019 2:56 pm
by kev1n
You can't run PHP code in an html object. You need to run the PHP in the Before Edit event and then pass the data to JavaScript or use ajax to retrieve data from the database.
(https://stackoverflow.com/questions/518 ... html-table)