user55 Posted September 26, 2007 Share Posted September 26, 2007 Hi, I have the code below with two table rows, but the second row won't display any data. How can I make both table rows work? Sara 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>"; $rowcount = 0; $group1_current_row = 0; $group2_current_row = 0; $group3_current_row = 0; $group4_current_row = 0; $group5_current_row = 0; $grouptotal_current_row = 0; $isStart01 = 0; $isStart02 = 0; $isStart03 = 0; $isStart04 = 0; $isStart05 = 0; while ($row = mysql_fetch_array($result)) { if ($rowcount == 0) { } $newgroupindex = -1; echo "<tr>";//this row displays multiple rows if (($rowcount%2) == 0) { $css_class = "\"one\""; } else { $css_class = "\"two\""; } $cellvalue = "" . $row[1] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>"; $cellvalue = "" . $row[2] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>"; $cellvalue = "" . $row[3] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "</tr>"; echo "<tr>";//this is the new row that should only display the row of data once echo "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>"; $cellvalue = "" . $row[4] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>"; $cellvalue = "" . $row[5] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "<td class=" . $css_class . " align=Default >" . $cellvalue . "</td>"; $cellvalue = "" . $row[6] . ""; if ($cellvalue == "") { $cellvalue = " "; } echo "</tr>"; $newgroupindex = -1; $rowcount = $rowcount + 1; $isStart01++; $isStart02++; $isStart03++; $isStart04++; $isStart05++; }//end while echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/ Share on other sites More sharing options...
freakus_maximus Posted September 26, 2007 Share Posted September 26, 2007 Not sure with your code, it looks like you are assigning $cellvalue a value after you echo it, don't think it is directly related to why the second row wont work for you. What happens when you run this with your query? I used your beginning table section and 3 column display. 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>'); Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355640 Share on other sites More sharing options...
user55 Posted September 26, 2007 Author Share Posted September 26, 2007 Hi, Thanks for the reply. Your code worked. It displays the first row of results, then the second row, then the first row again...etc... Can it display results without alternating rows? For example, it would display all the results for the first row, then all the results for the second row instead of alternating. Right now, it displays like this: first row results second row results first row results 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 ======================================= Sara Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355674 Share on other sites More sharing options...
user55 Posted September 26, 2007 Author Share Posted September 26, 2007 I was told if I create an array in my fetch loop, then build another couple of loops to create the html markup, then it should work. But I get an error on each of the "foreach ($resultArray as $val) {" lines. This is the error that I get: Warning: Invalid argument supplied for foreach() What am I doing wrong here? ??? 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) { 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>'); } } echo ('</table>'); Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355740 Share on other sites More sharing options...
user55 Posted September 26, 2007 Author Share Posted September 26, 2007 I am really stuck on this and need help fixing these errors. ??? Sara Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355799 Share on other sites More sharing options...
chocopi Posted September 26, 2007 Share Posted September 26, 2007 I think its because you are using $resultArray in your foreach and seeing as $resultArray has an array stored in it your foreach will be like this foreach ("Array" as $val) So you will need to take a value from the array like you have done earlier with $resultArray[$i]. I dont know what value you want but you need to put one in there. Hope that helps ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355813 Share on other sites More sharing options...
freakus_maximus Posted September 26, 2007 Share Posted September 26, 2007 Not saying this is an ideal or perfect solution, but I think it gets you want you need. This is based on my first bit of help above. You still need your initial query as normal, but there is a second query in between the two WHILE. if($result && mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo ('<tr>'); echo ('<td>'.$row[1].'</td>'); echo ('<td>'.$row[2].'</td>'); echo ('<td>'.$row[3].'</td>'); echo ('</tr>'); } // end 1st while $result = mysql_query ($yourquery) or die ("Query failed");// add your same query to this while($row = mysql_fetch_array($result)) { echo ('<tr>'); echo ('<td>'.$row[4].'</td>'); echo ('<td>'.$row[5].'</td>'); echo ('<td>'.$row[6].'</td>'); echo ('</tr>'); } // end 2ndwhile } // end if results echo ('</table>'); Before anyone slams my coding...lol... just trying to get a fast result to resolve the problem. I know there are other ways. Hope that helps... Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355831 Share on other sites More sharing options...
user55 Posted September 26, 2007 Author Share Posted September 26, 2007 Thanks again. It shows the first table of results before the second table of results now, but now it doesn't display anything for the second row. It did display the second row of data before with the previous code but it was alternating. Sara Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355849 Share on other sites More sharing options...
user55 Posted September 26, 2007 Author Share Posted September 26, 2007 It looks like it might work. I will let you know. Thanks! Sara Quote Link to comment https://forums.phpfreaks.com/topic/70744-multiple-table-rows-in-while-loop/#findComment-355861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.