PDA

View Full Version : Rearranging Table Rows


user55
09-26-2007, 03:40 PM
Hi,

How can I rearrange my table rows to display like the following:

I would like it to display like this:

first row results
first row results
first row results

second row results
second row results
second row results

Right now it alternates and it displays like this:

first row results
second row results
first row results

echo "<table cellpadding=\'2\' cellspacing=\'0\'>";
echo "<tr>";
echo "<td class=\"th\" >COL1</td>";
echo "<td class=\"th\" >COL2</td>";
echo "<td class=\"th\" >COL3</td>";
echo "</tr>";

if($result && mysql_num_rows($result) > 0)
{ while($row = mysql_fetch_array($result))
{
echo ('<tr>');//first row
echo ('<td>'.$row[1].'</td>');
echo ('<td>'.$row[2].'</td>');
echo ('<td>'.$row[3].'</td>');
echo ('</tr>');

echo ('<tr>');//second row
echo ('<td>'.$row[4].'</td>');
echo ('<td>'.$row[5].'</td>');
echo ('<td>'.$row[6].'</td>');
echo ('</tr>');
} // end while
} // end if results
echo ('</tr></table>');

Fumigator
09-26-2007, 04:19 PM
Create an array in your fetch loop, then build another couple of loops to create the html markup.


if($result && mysql_num_rows($result) > 0)
{ for ($i = 0; $i < mysql_num_rows; $i++) {
$resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
echo ('<tr>');//first row
echo ('<td>'.$val[1].'</td>');
echo ('<td>'.$val[2].'</td>');
echo ('<td>'.$val[3].'</td>');
echo ('</tr>');
}

foreach ($resultArray as $val) {
echo ('<tr>');//second row
echo ('<td>'.$val[4].'</td>');
echo ('<td>'.$val[5].'</td>');
echo ('<td>'.$val[6].'</td>');
echo ('</tr>');
}

} // end if results

user55
09-26-2007, 04:59 PM
I tried it, but I get the following error on both of the "foreach" lines:

Warning: Invalid argument supplied for foreach()

Inigoesdr
09-26-2007, 06:31 PM
Post your code. That error usually means that the variable you used wasn't an array.

Fumigator
09-26-2007, 09:19 PM
I forgot the ($result) in for ($i = 0; $i < mysql_num_rows; $i++), should be for ($i = 0; $i < mysql_num_rows($result); $i++). I'm not sure that will solve your problem, as I can't see your query text nor can I see your data.

Also keep in mind I post code as a guideline and an example of a concept, not necessarily something you can copy and paste which will work without tweaking. Any time you copy and paste a code snippet that you didn't write, you should look it over carefully and be sure you understand what it is doing.

user55
09-28-2007, 03:00 PM
Hi,

The table rows now display separately. The second table row repeats itself though. It should only appear once. For it to display once, would I have to use a mysql query on the second table row with the fields I need like this?

"SELECT one.division, two.division, two.quota, two.amount ".
"FROM one, two ".
"WHERE one.division = two.division";

If so, where would I put it on the second row? The code is below:

Sara


if($result && mysql_num_rows($result) > 0)
{ for ($i = 0; $i < mysql_num_rows($result); $i++) {
$resultArray[$i] = mysql_fetch_array($result);
}

foreach ($resultArray as $val) {
echo ('<tr>');//first row
echo ('<td>'.$val[1].'</td>');
echo ('<td>'.$val[2].'</td>');
echo ('<td>'.$val[3].'</td>');
echo ('</tr>');
}

foreach ($resultArray as $val) {
echo ('<tr>');//second row
echo ('<td>'.$val[4].'</td>');
echo ('<td>'.$val[5].'</td>');
echo ('<td>'.$val[6].'</td>');
echo ('</tr>');
}

} // end if results
echo ('</table>');