Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning a variable as the caption of a table 2

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
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:
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
to_forum-caption_qairnp.gif

Could anyone please explain me how to do it right? Assigning a variable as the caption of a table.
Thanks a lot
 
$numOfRows isn't defined when first called, move "$numOfRows = $myResult->num_rows" above the "echo"

PHP:
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>";

[b]$numOfRows = $myResult->num_rows;[/b]

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>'[b]$numOfRows[/b]'</caption>
  <tr>
    <th>Counter</th>
    <th>Date</th> 
  </tr>
_END;

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;
 }
?>

"Trying is the first step to failure..." - Homer
 
Janni surely has the correct answer. You seem to mistake the order of events, what happens where and when. Just think about what component (webserver, PHP, browser) is doing what and when.

Bye, Olaf.
 
Thank you so much Janni78 !
And yes Olaf, I'm confused working with that 3rd dimention programming. I'm procedural programming biased....
 
Well, it's as simple as that, in the moment of the echo command execution the active component is still PHP, everything echoed goes to the browser, but it's still executed at the webserver from php and so the variable has to exist and be set at that moment. Things only begin to get confusing to a novice, if you have javascript, which executes after php has send it out and the browser renders the html page and executes javascript. But even for that the different name conventions hinder you to confuse php and javascript code/variables, eg PHP variables must start with $ and javascript variables can't start with $.

Bye, Olaf.
 
What started my confusion was an HTML form which was placed at the utmost end of the program, even after the ?> symbol while PHP code executed commands where values sent from that form ($_POST..) were involved...
Giving it another thought, execution of those commands too place inside a "IF ISSET($_POST.. so long as no value assigned to the form, it was not executed while the form itself, after alerting it, initiated the page again vie "action=php file".
Thank you Olaf for drawing my attention and make things clear!
 
I dont know wat comes first and what next in a PHP page....

It's relatively simple.

Everything that is in PHP 'tags' <?php .... ?> runs before anything that isn't.

And PHP starts at the top of the document, runs the script code it finds, then quits at the end of the document, sending the resulting text stream to the user agent.

And when the 'form' is submitted to the server, the WHOLE document starts from the top again, this time including any values that were passed in the HTTP POST or GET structure.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind these jesus character, stars had to die for me to live.
 
Thanks, I changed my notification after I gave it another thought.
 
What is after ?> (and also in between ?> and the next <?php is just output sent back to the browser as is. You may think of it as an echo command. The whole file is executed by php first anyway and then the browser renders what came to it, executing eventual javascript etc. You can also declare functions with PHP, it is in fact a mix of a procedureal and oop programming. Most people start using just the procedural part of it, so everything is just one php procedure executed from top to bottom, the only twist in comparison with eg a VBA script is, you can mix in HTML with php via the <?php and ?> brackets.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top