Vertical Record Display

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
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Vertical Record Display

Post by Eric Praline »

Hi peeps

This may have been asked before, but I couldn't find anything suitable with a search, so...

I want to display records from a database search, but vertically (in a table), ie
A E
B F
C G
D

Obviously the number of records is unknown, so there may be uneven rows (as above) - and I'm kind of stumped at the minute! I'm fairly new at PHP so can't really think of a way to run through the table displaying the records in such a way as I need. It only needs to be a 2-column layout, but having the ability to have more could be useful too.

Any help would be much appreciated!

Cheers.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

What is the problem?

If you want order a script write to Job Hunt thread please.
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Post by Eric Praline »

The problem is I don't know how to write the PHP to get records to display vertically!

It's probably quite simple, but I'm not sure of the right way to go about it. Can anyone help?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

how about something like:

Code: Select all

for($i=0;$i<floor(count($records)/2);$i++){
  echo "<tr>";
  echo "<td>".$records[$i]."</td>"
  echo "<td>".$records[$i+floor(count($records)/2)]."</td>"
  echo "</tr>"
}
*totally untested - that's in your court!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If memory serves, the second thread link in Useful Posts talks about this very issue.
Eric Praline
Forum Commoner
Posts: 32
Joined: Wed Aug 29, 2007 8:37 am

Post by Eric Praline »

Thanks feyd, the thread you mentioned had some useful info, in particular the code...

Code: Select all

// assume $result is the result set

// cache the data
$cache = array();
while($row = mysql_fetch_assoc($result))
{
  $cache[] = $row;
}

$col2 = ceil(count($cache) / 2);
for($i = 0; $i < $col2; $i++)
{
  echo '<tr><td>'.$cache[$i].'</td><td>'.(isset($cache[$i + $col2]) ? $cache[$i + $col2] : '&nbsp;').'</td>';
}
... was just the sort of thing I was looking for! [:D]

Now, it works - as in it splits results into 2 columns - but it doesn't display the actual data result, instead it just prints 'Array'. Now, I think it may be something to do with how the information is cached with the mysql_fetch_assoc line, so I tried mysql_fetch_array instead but that does the same. Unfortunately my brain has stopped working and I can't for the life of me remember what to do to fix it.

So, one last bit of help is required please to figure out how to display the actual query result...

But apart from that it's all been very helpful, cheers!
Post Reply