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
Share on other sites

splitting the results into two columns as follows;

1 | 2
3 | 4
5 | 6
7

is straight forward... but i'm looking for a way to do;

1 | 5
2 | 6
3 | 7
4 |

and haven't figured out how to do it yet.
If you find anything, please let me know...
Link to comment
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?
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.