Jump to content

Multiple Table Rows in "While" Loop


user55

Recommended Posts

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>";

Link to comment
Share on other sites

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>');

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>');

 

 

Link to comment
Share on other sites

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 ;D

 

~ Chocopi

Link to comment
Share on other sites

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

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.