Re: mysql list to two-column list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 19 Mar 2012, at 22:43, Tom Sparks wrote:

> I have a members list witch I print out once a week,
> I would like to make the list into two-column list, but I dont know where to start looking to change the code?
> 
> here is the code
> "
> $result = mysql_query("SELECT * FROM customers ORDER BY LastName");
> 
> while($row = mysql_fetch_array($result))
>  {
>  echo $row['LastName'];
>  echo " " . $row['FirstName'];
>  echo " " . $row['CustomNo'];
>  echo "<br />";
>  }
> "


The following is untested so it may contain syntax errors, but I'm pretty sure the logic is sound and I think it will give you what you want. You may want to play with the cellpadding value to adjust the amount of space between the table cells.

echo '<table cellspacing="0" cellpadding="5" border="0">';
$column = 1;
while ($row = mysql_fetch_assoc($result))
{
  if ($column == 1) {
    echo '<tr>'.PHP_EOL;
  }
  echo '  <td>'.$row['LastName'].' '.$row['FirstName'].' '.$row['CustomNo'].'</td>';
  $column++;
  if ($column > 2) {
    echo '</tr>'.PHP_EOL;
    $column = 1;
  }
}
if ($column == 2) {
  echo '<td>&nbsp;</td>'.PHP_EOL;
}
echo '</tr></table>';

Assuming I'm right in my interpretation of what you want, the following needs to be said... this is *very* basic HTML being rendered by *very* basic PHP. I suggest you learn about basic HTML first, then learn basic PHP, then work out how to use logic in PHP to build the HTML you want.

If you're unsure how the above works, start by viewing the source of the page in your browser. If it's still not clear, run it in your head. Use two pieces of paper, one to store the variables and the other to keep track of the output. Go through each loop and for each line update the variables and update the output for each echo statement.

If I got what you want wrong then I have absolutely no clue what you're after.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux