Page 1 of 1

Simple Mysql horizontal list problem

Posted: Mon Feb 01, 2010 9:18 am
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

Re: Simple Mysql horizontal list problem

Posted: Mon Feb 01, 2010 2:52 pm
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.

Re: Simple Mysql horizontal list problem

Posted: Mon Feb 01, 2010 3:04 pm
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

Re: Simple Mysql horizontal list problem

Posted: Tue Feb 02, 2010 2:41 pm
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
}

Re: Simple Mysql horizontal list problem

Posted: Tue Feb 02, 2010 4:32 pm
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

Re: Simple Mysql horizontal list problem

Posted: Thu Feb 04, 2010 2:35 pm
by tr0gd0rr
`$column['agent_id']`

Re: Simple Mysql horizontal list problem

Posted: Thu Feb 04, 2010 3:16 pm
by cap2cap10
Ureka!!!!!!!!!!!!!!!!!!!!!!! :drunk:

Thanks for your Help!!!


Batoe :D