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