Haveing a dynamic list, in PHP code help.


#1

Im trying to have it on this page so that if the value $playernum = 3, that this list will appear. but currently its giveing me the
[Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
error, however.
If i take out the 2 ' ' around name in $row_player['name'] it shows me a list on the page…but the list only gives me the first of the values/records that its supposed to, whereas if i was JUST useing html to output it, it would give me all of the values/records i need.

Is there a way to still have it output ALL of the values? while its in the php code?

<?php 
if($playernum == 3){echo "<select name='1'>
do {<option value='$row_player['name']'>$row_player['name']</option>
} 
while ($row_player = mysql_fetch_assoc($player));
  $rows = mysql_num_rows($player);
  if($rows > 0) {
      mysql_data_seek($player, 0);
	  $row_player = mysql_fetch_assoc($player);
  }</select> ";} 
?>

#2

Try the below: May need some minor tweaking as I am unable to test it.

It is always better to keep your HTML out of the PHP paser as much as possible. Less processing is needed to jump in and out of php and then to have the paser try and run through HTML and parse it all.

[php]<?php
if($playernum == 3)
{
echo “”

while($row_player = mysql_fetch_assoc($player))
{

?>
<?php echo $row_player['name']; ?>

<?php } $rows = mysql_num_rows($player); if($rows > 0) { mysql_data_seek($player, 0); $row_player = mysql_fetch_assoc($player); } ?>
</select>
<?php } ?>[/php]

#3

Not sure if it makes a difference, really. The file itself will be run through the PHP Engine. The webserver will definately NOT dissect the file and present the appropriate chunks to the PHP Engine :wink:


#4
Not sure if it makes a difference, really. The file itself will be run through the PHP Engine. The webserver will definately NOT dissect the file and present the appropriate chunks to the PHP Engine ;)

Yes it does make a differance. In small scripts not so much, but just makes it easier to read and you end up with less errors like this. I just was having a discussion with a couple of co-workers on this matter and its always best to keep HTML out of the paser. I and co-workers have done speed tests and found it does make a differance.

The file is sent through the PHP engine, but anything not in the <?php ?> tags is ignored and just rendered as is, HTML, not parsed. Anything in the tags is actually parsed and then rendered as HTML.

So say if you have 5,000 lines of code. At line 1,000 php is turned on, then say you have it turn off at line 3,000. Now, only 2,000 lines of code is acutally parsed, whereas the rest is basically just skipped.


#5

I beg to differ :wink:

I did some benchmarking (Celeron 800MHz, 128MB RAM, requesting the scripts over 10Mbit LAN). Total filesize in the browser: 12.588.921 bytes (12.0 MBytes).

HTML Parsed:
[php]<?php

$start = microtime(true);

for ($i = 0; $i < 100000; $i++) {
echo “

”;
echo “Current Iteration: $i
”;
}

echo "

Duration: ".(microtime(true) - $start);

?>[/php]

Duration times (in seconds):

[code]19.34 (Cold Start)
26.77
22.71
22.9
22.54
22.97
19.89
18.91
22.61
24.27

Average: 22.29220972062
[/code]

HTML Unparsed:
[php]<?php

$start = microtime(true);

for ($i = 0; $i < 100000; $i++) {?>


Current Iteration: <?php echo $i; ?>
<?
}

echo "

Duration: ".(microtime(true) - $start);

?>[/php]

Duration times (in seconds):

[code]28.76 (Cold Start)
28.74
37.73
31.9
23.82
34.76
22.17
24.93
34.55
26.6

Average: 29.39580221176
[/code]

So simply echoing excess HTML seems faster by almost 25% (at least in my environment).


#6
So simply echoing excess HTML seems faster by almost 25% (at least in my environment).

I guess this seems, at least to me, the uncommon result as I was chatting awhile back about this on another forum and almost all there said the same, that keeping HTML out of the paser was the best practice.

But to each is there own and I guess that its not to often people will be working on scripts large enough to make a difference to the end user anyway.


#7

True.

I’m using an HTML templating system though, HTML goes into the MySQL database and PHP pulls it out. Impossible to keep HTML outside PHP that way, but it keeps things clean when it comes to file/folder structures :wink:

I learned that little trick from vBulletin.


#8

Just as a side note: I got wondering and had to try it myself…

A friend of mine took his site and echo out all the html on his homepage…

Script Generation took 0.0010s

Then we hopped in and out of PHP and we came up with

script generation took 0.0005 s

Half the time. But as you can see by the numbers, it is such a miniscual amount of time, it is pretty trivial anyway. Just thought I would share with anyone who cares! :)


#9

Yep, unless your PHP outputs crazy amounts of HTML, I guess it won’t really matter. The only argument is personal preference I guess.