Simple Mysql horizontal list problem

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
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Simple Mysql horizontal list problem

Post by cap2cap10 »

Hello again, members of the php Technorati. I bring another one of my problems to your door step and humbly ask for your guidance. How do I get Mysql to list all ids horizontally and vertically. The code I have lists vertically, but repeats the same id horizontally. Here is the code:

Code: Select all

$query = "SELECT agentID FROM agent_login LIMIT 0, 200 ";
    $result = mysql_query($query) or die(mysql_error());
 
        $check = '0';
 
    while($row = mysql_fetch_assoc($result))
    {
        if ($check % 2 == 0)
            {
                $text1 .= '<tr width="800" bgcolor=#FCE6A0 class="style1" align=center>';
            }
            else
            {
                $text1 .= '<tr bgcolor=#FCE6A0 class="style1" align=center>';
            }
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '<td width="100" height="24" align="left" valign="middle" style="padding-left: 15px;"><span class="style1"><a href=http://www.resu-pic.com/get_agent.php?id='.$row['agentID'].'>'.$row['agentID'].'</a></span></td>';
         $text1 .= '</tr>';
 
            $check ++;
        }
 
    if($check == 0)
    {
        $text1 .= '<tr width = "800">';
        $text1 .= '<td align="center" width="100%"><span class="style1"><b>No Agents found!</b></span></td>';
        $text1 .= '</tr>';
 
    }
     echo $text1;
Please enlighten me. As always , Thanks in advance,


Batoe
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Simple Mysql horizontal list problem

Post by tr0gd0rr »

Well if you need only one row, echo <tr>, inside the while loop echo <td> elements, then echo </tr> after the while loop.

If you need multiple rows, (e.g. up to 10 columns per row) you could put the rows into an array then use `array_chunk()` to divide it into groups of 10.
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Simple Mysql horizontal list problem

Post by cap2cap10 »

Thank you. Can you give me an example of how the code would look like since I would need 8 columns and unknown # of rows since it would be determined by how many agentIDs are in the database?

Thanks in advance,


Batoe

Sorry, still a novice
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Simple Mysql horizontal list problem

Post by tr0gd0rr »

Code: Select all

<?php
$rs = array();
while (($row = mysql_fetch_assoc($result))) {
  $rs[] = $row;
}
// up to 8 columns per row
$rows = array_chunk($rs, 8);
foreach ($rows as $row) {
  // open tr
  foreach ($row as $column) {
    // output td and contents
  }
  // close tr
}
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Simple Mysql horizontal list problem

Post by cap2cap10 »

Ok, but what is my variable now? I want agentIDs only, but we placed it into an array, so how would I ask the server to list my agentIDs? it is not $row['agentID'] or $rs['agentID'] anymore, so what is it?

Thanks again,

Batoe
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: Simple Mysql horizontal list problem

Post by tr0gd0rr »

`$column['agent_id']`
User avatar
cap2cap10
Forum Contributor
Posts: 158
Joined: Mon Apr 14, 2008 11:06 pm

Re: Simple Mysql horizontal list problem

Post by cap2cap10 »

Ureka!!!!!!!!!!!!!!!!!!!!!!! :drunk:

Thanks for your Help!!!


Batoe :D
Post Reply