Jump to content


Photo

while loop processing


  • Please log in to reply
10 replies to this topic

#1 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 29 September 2003 - 06:18 AM

I\'m having trouble with my while loop. My code is setup as follows but it only returns 1 user when I know it should return 2:

[php:1:1dfc5c6460]<?php
$players_sql = mysql_query(\"SELECT username FROM sh_player WHERE dead=\'0\'\");
$players = mysql_fetch_array($players_sql);
$num_players = mysql_num_rows($players_sql);
$class_sql = mysql_query(\"SELECT class FROM sh_player WHERE dead=\'0\'\");
$class = mysql_fetch_array($class_sql);
$level_sql = mysql_query(\"SELECT level FROM sh_player WHERE dead=\'0\'\");
$level = mysql_fetch_array($level_sql);
/*print \"$players[0]\";
print \"$players[1]\";*/

echo \"<table><tr><td><b><font size=\"2\" face=\"verdana\" color=\"#6699CC\">Name</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Class</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Level</b></td></tr>\";

$n = 0;
while ($n < $num_players)
{
echo \"<tr><td><font size=\"2\" face=\"verdana\" color=\"#6699CC\">$players[$n]</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\">$class[$n]</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\">$level[$n]</td></tr><td><tr>\";
$n++;
}
?>[/php:1:1dfc5c6460]

Is there something wrong with the way I\'m using the mysql_fetch_array function?
[table]



Don't worry, the printer fairies will sort it out.

#2 shivabharat

shivabharat
  • Members
  • PipPipPip
  • Advanced Member
  • 371 posts
  • LocationChennai, India

Posted 29 September 2003 - 07:56 AM

The code looks fine but tell me \"$num_players \" whats the value returned here?
Knowledge --- Reading Enriches Mind But Sharing Enhances It.[br][br]Note: Before you request help enusre that you have had a look at the tutorials @phpfreaks

#3 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 29 September 2003 - 05:29 PM

It returns 2
[table]



Don't worry, the printer fairies will sort it out.

#4 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 29 September 2003 - 06:04 PM

Actually there are a few things wrong.. try this:
<?php  $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'");  $total = mysql_num_rows($query);  echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\';  while($row = mysql_fetch_array($query,MYSQL_ASSOC)  {    echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $row[\'username\'] . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'class\'] . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'level\'] . \'</font></td></tr>\';  }  echo \'</table>\';?>

Hope that helps.

#5 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 29 September 2003 - 06:12 PM

No, it\'s OK. I did a bit of research and used the mysql_result() function:

[php:1:50f9efcd4d]<?php
$players_sql = mysql_query(\"SELECT username FROM sh_player WHERE dead=\'0\'\");

$num_players = mysql_num_rows($players_sql);
$class_sql = mysql_query(\"SELECT class FROM sh_player WHERE dead=\'0\'\");

$level_sql = mysql_query(\"SELECT level FROM sh_player WHERE dead=\'0\'\");

echo \"<table><tr><td><b><font size=\"2\" face=\"verdana\" color=\"#6699CC\">Name</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Class</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\"><b>Level</b></td></tr>\";

$n = 0;
while ($n < $num_players)
{
$players = mysql_result($players_sql, $n, \'username\');
$class = mysql_result($class_sql, $n, \'class\');
$level = mysql_result($level_sql, $n, \'level\');
echo \"<tr><td><font size=\"2\" face=\"verdana\" color=\"#6699CC\">
<a href=\"view.php?username=$players\">$players</a></td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\">$class</td><td>
<font size=\"2\" face=\"verdana\" color=\"#6699CC\">$level</td></tr><td><tr>\";
++$n;
}
?>[/php:1:50f9efcd4d]

Thanks for your time though.
[table]



Don't worry, the printer fairies will sort it out.

#6 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 29 September 2003 - 06:39 PM

Why must you use 3 queries where 1 would generate the same result?

#7 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 29 September 2003 - 06:47 PM

huh?
[table]



Don't worry, the printer fairies will sort it out.

#8 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 29 September 2003 - 07:02 PM

You are using 3 queries:
SELECT username FROM sh_player WHERE dead=\'0\'SELECT class FROM sh_player WHERE dead=\'0\'SELECT level FROM sh_player WHERE dead=\'0\'

A quicker method, for both the script and the server, is to use 1 query to return all of the results:
SELECT username,class,level FROM sh_player WHERE dead=\'0\'
then loop through the results.

You\'ve made it harder than it needs to be.. look into that.

#9 pauper_i

pauper_i
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts
  • LocationPhoenix, AZ, USA

Posted 29 September 2003 - 07:08 PM

take a look at the code implications:
Metalblend:

$query = mysql_query(\\\"SELECT username,class,level FROM sh_player WHERE dead=\'0\'\\\");

Your version:

$players_sql = mysql_query(\\\"SELECT username FROM sh_player WHERE dead=\'0\'\\\");  
$class_sql = mysql_query(\\\"SELECT class FROM sh_player WHERE dead=\'0\'\\\");  
$level_sql = mysql_query(\\\"SELECT level FROM sh_player WHERE dead=\'0\'\\\");  

Why would you go to the well three times when you can do the job in one. In other words, your server is going to work its butt off trying to keep up with your extra queries when it doesn\'t need to since you could pull the same info all in one go and access it from a memory array!

D
Genius is one percent inspiration, and ninety-nine percent perspiration. [br]Thomas Alva Edison

#10 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 30 September 2003 - 06:29 AM

I think I see what you mean. But how would I get the results from that query into 3 seperate unique variables?
[table]



Don't worry, the printer fairies will sort it out.

#11 metalblend

metalblend
  • Members
  • PipPipPip
  • Advanced Member
  • 89 posts
  • LocationCalifornia, USA

Posted 01 October 2003 - 04:54 PM

Using the method I gave earlier:
<?php  $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'");  $total = mysql_num_rows($query);  echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\';  while($row = mysql_fetch_array($query,MYSQL_ASSOC)  {    echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $row[\'username\'] . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'class\'] . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $row[\'level\'] . \'</font></td></tr>\';  }  echo \'</table>\';?>

or using your mysql_result() method (which still results in more code):
<?php  $query = mysql_query("SELECT username,class,level FROM sh_player WHERE dead=\'0\'");  $total = mysql_num_rows($query);  echo \'<table><tr><td><b><font size="2" face="verdana" color="#6699CC">Name</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Class</font></td><td>\';  echo \'<font size="2" face="verdana" color="#6699CC"><b>Level</b></font></td></tr>\';  for($i=0;$i<$total;$i++)  {    $username = mysql_result($query,$i,\'username\');    $class    = mysql_result($query,$i,\'class\');    $level    = mysql_result($query,$i,\'level\');    echo \'<tr><td><font size="2" face="verdana" color="#6699CC">\' . $username . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $class . \'</font></td><td>\';    echo \'<font size="2" face="verdana" color="#6699CC">\' . $level . \'</font></td></tr>\';  }  echo \'</table>\';?>

Hope that helps.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users