Link to home
Create AccountLog in
Avatar of katlees
katleesFlag for United States of America

asked on

Loop in PHP

I had someone write some php code for me as I am very new at it. They did a great job, but I need the table to be three columns across instead of just one. How can I adjust the code to do this?

Can you comment on the code so I can follow what is going on?
<?php
 
$team_query = "SELECT * FROM Teams WHERE Active='1' ORDER BY Name, Nickname";
$team_query_result = mysql_query($team_query);
 
if (!$team_query_result)
{
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}
 
if (!mysql_num_rows($team_query_result))
{
	echo "No teams found\n";
	exit;
}
 
echo "<table>\n";
 
while($team_row = mysql_fetch_assoc($team_query_result))
{
	$team_id = $team_row['ID'];	
	$team_name = $team_row['Name'];
	$team_nickname = $team_row['Nickname'];
 
	echo "<tr>\n";
	echo "<td><form action=\"managerbs.php\" method=\"post\"><input type=\"hidden\" name=\"teamid\" value=\"$team_id\"><input type=\"submit\" value=\"$team_name $team_nickname\"></form></td>\n";
	echo "</tr>\n";
}
 
echo "</table>\n";
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
whoops the "echo" statement on line 8 should be below all the $team_xxx assignments. It should be moved down to line 12.
Avatar of katlees

ASKER

So this is the code as I understand you want me to have it.  I get an error in line 121 which is the

            if( $i < 3 ) {
                  $team_row = mysql_fetch_assoc($team_query_result)
            } (this is line 121)

<?php

$team_query = "SELECT * FROM Teams WHERE Active='1' ORDER BY Name, Nickname";
$team_query_result = mysql_query($team_query);

if (!$team_query_result)
{
      echo "Could not successfully run query ($sql) from DB: " . mysql_error();
      exit;
}

if (!mysql_num_rows($team_query_result))
{
      echo "No teams found\n";
      exit;
}

echo "<table>\n";

while($team_row = mysql_fetch_assoc($team_query_result))
{
 
      echo "<tr>\n";
 
      for($i=1;$i<=3;$i++)
      {
            
            $team_id = $team_row['ID'];      
            $team_name = $team_row['Name'];
            $team_nickname = $team_row['Nickname'];
 echo "<td><form action=\"managerbs.php\" method=\"post\"><input type=\"hidden\" name=\"teamid\" value=\"$team_id\"><input type=\"submit\" value=\"$team_name $team_nickname\"></form></td>\n";
            // don't fetch for the last entry. The fetch in the while loop will handle that.
            if( $i < 3 ) {
                  $team_row = mysql_fetch_assoc($team_query_result)
            }
 
            // if we ran out of records, stop immedately
            if( !$team_row ) {
                  break;
            }
 
      }
 
 
      echo "</tr>\n";
}

?>
you're missing the ";" at the end of this line

$team_row = mysql_fetch_assoc($team_query_result)

should be

$team_row = mysql_fetch_assoc($team_query_result);
Avatar of katlees

ASKER

That works great. Can you please put comments in so I understand what the code means?
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Somehow, I feel cheated here. I wonder why?
Avatar of katlees

ASKER

Frosty - I am sorry. I didn't realize someone else had jumped in there. Is there a way to go back and redo points? I can open a different question and give them to you....
I guess that's what happens when I go for lunch! ;)

No, don't open another question. You can request that a moderator change the answer. There's a "request attention" button at the top of the question where you put an explaination of what you want addressed and why. So you can write in there "please split points between MMDeveloper (comment #22162146) and Frosty555 (comment #22161765)"
Avatar of katlees

ASKER

Frosty, I did this. I asked that you get 200 and MMDeveloper get 50 as you did most of the work. I apologize! I'll play closer attention next time.
Okay, no problem. Though I don't want to cheat MMDeveloper out of deserved points either, so if whoever reviews this question thinks that a 50/50 split would be more appropriate, then I don't object to that.
no it's cool. I dont mind if he gets all the points. I'm not here for the points, this site is just something for me to do to help my workday go by faster.
Avatar of katlees

ASKER

Thank you!