How can I display results of an array in individual rows.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facarroll
Forum Newbie
Posts: 8
Joined: Tue Jul 27, 2010 6:05 pm

How can I display results of an array in individual rows.

Post by facarroll »

I've got my code working OK in that the correct results are drawn from the database. What I have here in the code is an array consisting of an image, accompanied by its title and thirdly a link to activate a quiz associated with the image. Everything works fine, except that I have an unknown number of sets of data (image, title and link) which I want to display so that each set of data takes up a new row.
The code I have here places all of the results into the same row.
Any suggestions are most welcome.

Code: Select all

 <?php
// Query the database
$query1 = mysql_query("SELECT title FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC"); 
$query2 = mysql_query("SELECT url_small FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC"); 
$query3 = mysql_query("SELECT title FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC");
while($row1 = mysql_fetch_array($query3))
{
$linkname .= $row1['title']."<br />\n";
}
?>
                  <table>
                    <tr>
                      <td>
                      <?php
                      while ($row2 = mysql_fetch_array($query2)) 
                      { 
                      $thumbnail.= $row2['url_small'];
                      echo "<img src='wood_tool_images/{$row2['url_small']}' alt='' /><br />\n";
                      }
                      ?>
                      </td>
                      <td height="200">
                      <?php
                      echo $linkname 
                      ?>
                      </td>
                      <td>
                      <?php
                      while ($row1 = mysql_fetch_array($query1)) 
                      { 
                      $quizname.= $row1['title'];
                      echo "<a href='../{$row1['title']} Safety Quiz/{$row1['title']} Safety Quiz.php'>Take This Quiz</a><br />\n";
                      }
                      ?>
                      </td>
                     </tr>
                  </table>
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: How can I display results of an array in individual rows

Post by Mince »

the solution is quite easy:

your while loops happen inside the row - <tr>. You should instead have it outside, example:

Code: Select all

<table>
<?php
// do your queries, etc here, then your while statement
while ($row1 = mysql_fetch_array($query1)) 
{ 
?>
<tr>
<td>bla bla bla</td>
</tr>
<?php
}
?>
</table>
so, for each row in the db, a row is echo'd in the table
facarroll
Forum Newbie
Posts: 8
Joined: Tue Jul 27, 2010 6:05 pm

Re: How can I display results of an array in individual rows

Post by facarroll »

I can understand that. I think you've fixed it. It's late here though, and past my bedtime. I'll check it out on Saturday. I'll let you know how it works out. Thanks. Appreciate it.
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: How can I display results of an array in individual rows

Post by Mince »

No problem :)
Mince
Forum Commoner
Posts: 25
Joined: Mon Aug 03, 2009 9:36 am

Re: How can I display results of an array in individual rows

Post by Mince »

Oh, and one more thing:
$query1 = mysql_query("SELECT title FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC");
$query2 = mysql_query("SELECT url_small FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC");
$query3 = mysql_query("SELECT title FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC");
Is not necessary. rather use:

Code: Select all

$query1 = mysql_query("SELECT * FROM topics WHERE managerId='".$managerId."' AND egroup1='"."1"."' ORDER BY title ASC"); 

<table>
<?php
while ($row1 = mysql_fetch_array($query1)) 
{ 
?>
<tr>
<td>
$thumbnail.= $row1['url_small'];
echo "<img src='wood_tool_images/{$row2['url_small']}' alt='' /><br />\n";
</td>
<td height="200">
<?php
echo $linkname 
?>
</td>
<td>
$quizname.= $row1['title'];
echo "<a href='../{$row1['title']} Safety Quiz/{$row1['title']} Safety Quiz.php'>Take This Quiz</a><br />\n";
</td>
</tr>
<?php
}
?>
<table>
so you have one loop, and echo the data where required in the table
Post Reply