How to create a desire tabular structure with the data comin

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

How to create a desire tabular structure with the data comin

Post by djdon11 »

Hello Friends.....

I am here to find some code which can show my data which is coming from database in a desirable tabular form..
Means when while loops creates new row for each record .. instead of that i want to create new column for the next record & i want 3 column in a row & after that a new row should be creat....

can anybody help me for this...........



vaibhav....
User avatar
ReverendDexter
Forum Contributor
Posts: 193
Joined: Tue May 29, 2007 1:26 pm
Location: Chico, CA

Post by ReverendDexter »

You could output it as an html table...

something along the lines of

Code: Select all

$sql = $your_query;
$result_set = mysql_query($sql);
$cur_row = mysql_fetch_assoc($result_set); 
$headrow = "<tr>";
$firstrow = "<tr>";
//open the table
echo "<table><tbody>";
        //get headers and first row
	foreach($cur_row as $header => $value)
	{
		$headrow .= "<th>$header</th>";
		$firstrow .= "<td>$value</td>";
	}
	$headrow .= "</tr>";
	$firstrow .= "</tr>";
	echo "$headrow"."$firstrow";

	//Get all other rows
	while ($cur_row = mysql_fetch_assoc($result_set)) 
	{
	    $output_row = "<tr>";
	    foreach($cur_row as $h=>$value)
	    {
		$output_row .= "<td>$value</td>";
	    }
	    $output_row .="</tr>";
	    echo $output_row;
	}
	else
	{
		echo "<tr><td>No rows returned</td></tr>";
	}
//close the table
echo "</tbody>
	</table>";
Not promising that will work 100% right as-is, I copied/pasted and edited it down, but I hope it helps get you started!
-Dex
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts links to a two threads which may be of interest. The first two links.
Post Reply