×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Assigning a variable as the caption of a table
2

Assigning a variable as the caption of a table

Assigning a variable as the caption of a table

(OP)
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

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

RE: Assigning a variable as the caption of a table

$numOfRows isn't defined when first called, move "$numOfRows = $myResult->num_rows" above the "echo"

CODE --> 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>";

$numOfRows = $myResult->num_rows;

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;

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

RE: Assigning a variable as the caption of a table

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.

RE: Assigning a variable as the caption of a table

(OP)
Thank you so much Janni78 !
And yes Olaf, I'm confused working with that 3rd dimention programming. I'm procedural programming biased....

RE: Assigning a variable as the caption of a table

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.

RE: Assigning a variable as the caption of a table

(OP)
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!

RE: Assigning a variable as the caption of a table

Quote:

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.

RE: Assigning a variable as the caption of a table

(OP)
Thanks, I changed my notification after I gave it another thought.

RE: Assigning a variable as the caption of a table

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close