Jump to content

Echo'ing SQL Result Over Multiple Table Columns


leachus2002

Recommended Posts

Hi There,

 

I have an SQL query that returns 10 rows, which I want to echo over 2 columns and 5 rows, however, I want rows 1-5 on the left hand column and rows 6-10 on the right hand side.

 

Is there an easy way to do this? Normally I would do a fetch_array but, that would place the rows in order of, left, right, left, right - if that makes sense?

 

Table is a standard table with 2 columns and 5 rows.

 

Thanks

Matt

Not the most elegant; however, since you are only dealing with 10 records, perhaps something like this...

 

<?PHP
/* connect to db */
include('db.php');

/* create query */
$query = "SELECT * FROM table_name";

/* execute query */
$result = mysql_query($query);

/* put the content into an array */
while($row=mysql_fetch_array($result)) {
$cell_content[] = $row['some_field_name];
}

/* start the table */
echo "<table>";

/* loop thru the array */
$i=0;
while($i<10) {
echo "<tr><td>" . $cell_content[$i] . "</td><td>" . $cell_content[$i+5] . "</td></tr>";
$i = $i +2;
}

/* close the table */
echo "</table>";

?>

Thanks for that code. Looking good. However its only bringing back 8 rows and I have the following error:

 

Notice: Undefined offset: 11 in .............

Notice: Undefined offset: 13 in .............

 

Any ideas? My guess is that its going above 10?

 

Cheers

Matt

echo " <table width='100%' cellspacing='0' cellpadding='2'><form name='sysstatus' action='index.php' method='post'>";
$i = 1;
while($i<10){
echo "<tr><td width='150'>".$sysname[$i]."</td><td> </td><td width='30'> </td><td width='150'>".$sysname[$i+5]."</td><td> </td></tr>";
$i = $i +2;
}
echo "</form></table>";

My error, should be...

echo "<table>";
/* loop thru the array */
$i=0;
while($i<5) {
echo "<tr><td>" . $cell_content[$i] . "</td><td>" . $cell_content[$i+5] . "</td></tr>";
$i = $i +1;
}
/* close the table */
echo "</table>";

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.