Page 1 of 1

Vertical Record Display

Posted: Wed Aug 29, 2007 8:52 am
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.

Posted: Wed Aug 29, 2007 8:55 am
by xpgeek
What is the problem?

If you want order a script write to Job Hunt thread please.

Posted: Wed Aug 29, 2007 9:58 am
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?

Posted: Wed Aug 29, 2007 12:52 pm
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!

Posted: Thu Aug 30, 2007 8:01 am
by feyd
If memory serves, the second thread link in Useful Posts talks about this very issue.

Posted: Fri Aug 31, 2007 5:34 am
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!