Jump to content


Photo

Splitting db records for page formatting?


  • Please log in to reply
5 replies to this topic

#1 Switch0r

Switch0r
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationManchester, UK

Posted 22 March 2006 - 09:50 AM

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 :)
The Human Torch was denied a bank loan...

#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 March 2006 - 01:32 PM

Do you want seperate columns on one page, or divide up the results on seperate pages with one column on each page??

#3 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 22 March 2006 - 01:59 PM

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...
wisewood: proven fact, I am both wise, and wooden.

#4 Switch0r

Switch0r
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationManchester, UK

Posted 22 March 2006 - 05:35 PM

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?
The Human Torch was denied a bank loan...

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 March 2006 - 06:29 PM

Here you go, try this out. Will have to chenge some layout stuff but you should get the idea

<?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";
?>

Hope that works

Ray

#6 Switch0r

Switch0r
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationManchester, UK

Posted 23 March 2006 - 07:43 AM

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 :)
The Human Torch was denied a bank loan...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users