Assigning a variable as the caption of a table

Hi everyone,
My PHP page is consisted of an HTML table.
At the “style” area of the table definition I want to insert a variable ($numOfRows) for a caption. I want the
caption to be the number of rows of a select query.
Here is the code:

require_once 'myInitial.php';
require_once 'myLogin.php';

$myQue = "SELECT  
counter AS Counter, 
shiftdate AS Date 
FROM hourshifts";

$myResult = $myConnection->query("$myQue");
if(!$myResult) echo "Browse failed: ". $myConnection->error . "<br><br>";

echo <<<_END
<style>
table, th, td {border-collapse: collapse;direction:rtl}
th {background-color: #f1f1c1;border: 1px solid black;}
td {border: 1px solid black;}
</style>
<table style="width:40%; border: 1px solid black;">
<caption>'$numOfRows'</caption>
  <tr>
    <th>Counter</th>
    <th>Date</th> 
  </tr>
_END;

$numOfRows = $myResult->num_rows;
for ($j = 0 ; $j < $numOfRows ; $j++)
{
	$myResult->data_seek($j);
	$row = $myResult->fetch_array(MYSQLI_ASSOC);
	echo <<<_END
		<tr>
			<td>{$row["Counter"]}</td> 
			<td>{$row["Date"]}</td>
		</tr>
_END;
 }
?>

Uploading the page I get an error message that says:
Undefined variable: numOfRows in C:\xampp\htdocs\to_forum_caption.php on line 24

Could anyone please explain me how to do it right? Assigning a variable as the caption of a table.
Thanks a lot

You’re echoing $numOfRows before initialising and assigning a value to it; so just move this line

$numOfRows = $myResult->num_rows;

above the echo statement.

1 Like

Thank you so much m3g4p0p !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.