...

View Full Version : How can I display more than one column from retreived mysql data?



javanoobie9
02-29-2008, 02:44 AM
When I retrieve data from mysql it automatically lists one row and one column. How can I have it list more than one column in one row?

Fou-Lu
02-29-2008, 03:05 AM
You will probably need to provide more information as to what you are doing. We don't know what your query is, the database your using, etc.
However, I will tell you this, and it will probably solve your problem. Database queries in php usually create a result resource, think of it kinda like an array (its not quite, but just bear with it).


while ($row = $qry->fetch_assoc())
{
// Do what you want on each returned row
}

As for the columns, that is likely due to either your query itself only fetching one field, or your data handling of extracting each value. MySQLi fetch_assoc method fetches each field => value pair and stores them into an associative array, so from the above example you can retrive them by doing $row['fieldName'];

javanoobie9
02-29-2008, 03:49 AM
while ($row = mysql_fetch_assoc($queryresults)) {
echo $row['image'];
}


I want the row image to be in 2 columns when displayed instead of one column.

Fou-Lu
02-29-2008, 04:15 AM
You mean for use in a <table>?


<table>
<?php
$i = 0;
while ($row = mysql_fetch_assoc($queryresults))
{
if ($i &#37; 2 == 0)
{
echo '<tr>';
}
echo '<td>';
echo $row['image'];
echo '</td>';
if ($i % 2 == 1)
{
echo '</tr>';
}
$i++;
}
if ($i % 2 == 1)
{

echo '<td></td></tr>';
}
?>
</table>

Lesse, will that work? Odd step opens the tr tags, while even ones close it. If it ends in an odd number, close the row. That sounds like it will work. Personally, I'm a css sort of guy, so I'd do something like the following:


$i = 0;
while ($row = mysql_fetch_assoc($queryresults))
{
$class = '';
if ($i % 2 == 1)
{
$class = 'leftFloat';
}
if ($i % 2 == 0)
{
$class = 'rightFloat';
}
...

And apply a class to the image instead.
That looks like it would work, but I haven't tested it out.

Is that along the idea of what you're looking for?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum