Jump to content

Displaying Results


timmah1

Recommended Posts

This is probably and easy question, but I can't seem to find an easy answer.

When I pull info out of the database and put it into a table, how do I get the info to go horizontal instead of vertical?

For example, If I want to pull all of the values of $id from the database and list them in a table, instead of listing them straight down, I'd like for the results to show a minimum of 4 values across.

See below:
[img]http://www.fotobins.com/Picture-2.jpg[/img]
Link to comment
https://forums.phpfreaks.com/topic/29519-displaying-results/
Share on other sites

I think you will need to use arrays, never did this before, but maybe..

create an array, like $row, into this array, you will record the data, like a table.. $row[0][] = $id, $row[1][] = foo, .. got it?

Now you print your table..

tr, foreach $row[0] as $x, td $x /td, loop, /tr
tr, foreach $row[1] as $x, td $x /td, loop, /tr

So, you got your table! Test it, and tell me the result? :)

D.Soul
Link to comment
https://forums.phpfreaks.com/topic/29519-displaying-results/#findComment-135451
Share on other sites

use this
[code]<?php
/***** database connection start *****/

/***** database connection end ******/

echo "<table width=300 border=0 align=center>";

//set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc
$numcols = 5; // how many columns to display
$numcolsprinted = 0; // no of columns so far

// get the results to be displayed
$query = "SELECT * FROM table_name ORDER BY id ASC";
$mysql_result = mysql_query($query);

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

//get data - eg,
$id = $myrow['id'];

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

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

// bump up row counter
$numcolsprinted++;

} // end while loop

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

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

Ray
Link to comment
https://forums.phpfreaks.com/topic/29519-displaying-results/#findComment-135454
Share on other sites

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.