Jump to content

Splitting db records for page formatting?


Switch0r

Recommended Posts

Hey peeps, wonder if anyone can help me. I'm prob just being numb, but i could do with the help :)

I'm trying to split a set of records from a db into a 2 column table, rather than one long list.

eg

1
2
3
4
5

-->

1 | 4
2 | 5
3 |

If you see what i mean....

would i be best in creating 2 queries and echoing the results separately or is there an easier way?

and if the above is the case, how do i only echo a range of results?

thanks to anyone who can help me :)
Link to comment
https://forums.phpfreaks.com/topic/5486-splitting-db-records-for-page-formatting/
Share on other sites

well ill explain what ive got:

a set of 25 records, for example, which is set out in 1 column, that i want to split into 2 columns (by way of a table) with records 1 - 13 in column 1, and 14 - 25 in column 2. but the record set is variable in length, so i though by halfing the record number and rounding it up, then taking that away from the total would give numbers corresponding to the records.

that in itself works ok, its outputting the records to the browser that im having trouble with.

hope this makes it a bit clearer?
Here you go, try this out. Will have to chenge some layout stuff but you should get the idea

[code]<?php
// Make DB connection here

//set  number of columns here
$numcols = 2; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT * FROM tablename";
$mysql_result = mysql_query($query) or die (mysql_error());

// get each row
while($myrow = mysql_fetch_row($mysql_result))
{

//get data - eg, reading fields 0 and 1
$tn = $myrow[0];
$in= $myrow[1];

if ($numcolsprinted == $numcols) {
print "</tr>\n<tr>\n";
$numcolsprinted = 0;
}

// output row from database
echo "<td>$in $tn</td>\n";

// bump up row counter
$numcolsprinted++;

} // end while loop

$colstobalance = $numcols - $numcolsprinted;
for ($i=1; $i<=$colstobalance; $i++) {

}
print "<TD></TD>\n";
?>[/code]

Hope that works

Ray
cheers craygo, not entirely sure what all that meant, but i figured it out all by my lonesome anyway (one to tell the grandkids i reckon!)

just put all the records into an array, and looped through the array to display the info. i cant believe i didnt think of it before really :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.