Jump to content

Recommended Posts

I want to use 2 WHILE functions but it only displays the first one

 

<form name="mainitem" action="../aea/result/mainitem.php" method="POST">

                Select Supplier<select name="mainitem">

                <?php while ($row2 = mysql_fetch_array($getmainitem)) {?>

                <option value=" <?php echo $row2['id']; ?> ">

                <?php echo $row2['mainitem'];} ?> </option>></select><BR>

                <input type="submit"/>

</form><BR><BR>

 

<?php

echo "<table border='1'>

<tr>

<th>Itemname</th>

<th>TotalStock</th>

<th>Totalmass</th>

<th>Totalprice</th>

</tr>";

 

while($row3 = mysql_fetch_array($getmainitem))

  {

  echo "<tr>";

  echo "<td>" . $row3['itemname'] . "</td>";

  echo "<td>" . $row3['totalstock'] . "</td>";

  echo "<td>" . $row3['totalmass'] . "</td>";

  echo "<td>" . $row3['totalprice'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

 

Is there a way to stop the function so i can do it again

Link to comment
https://forums.phpfreaks.com/topic/111824-noob-help-with-while-function/
Share on other sites

please put inside code tags,

 

<form name="mainitem" action="../aea/result/mainitem.php" method="POST">
                Select Supplier<select name="mainitem">
                <?php while ($row2 = mysql_fetch_array($getmainitem)) {?>
                <option value=" <?php echo $row2['id']; ?> ">
                <?php echo $row2['mainitem'];} ?> </option>></select><BR>
                <input type="submit"/>
</form><BR><BR>

<?php
echo "<table border='1'>
<tr>
<th>Itemname</th>
<th>TotalStock</th>
<th>Totalmass</th>
<th>Totalprice</th>
</tr>";

while($row3 = mysql_fetch_array($getmainitem))
  {
  echo "<tr>";
  echo "<td>" . $row3['itemname'] . "</td>";
  echo "<td>" . $row3['totalstock'] . "</td>";
  echo "<td>" . $row3['totalmass'] . "</td>";
  echo "<td>" . $row3['totalprice'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

try

<form name="mainitem" action="../aea/result/mainitem.php" method="POST">
                Select Supplier<select name="mainitem">
                <?php while ($row2 = mysql_fetch_array($getmainitem)) {?>
                <option value=" <?php echo $row2['id']; ?> ">
                <?php echo $row2['mainitem'];} ?> </option>></select><BR>
                <input type="submit"/>
</form><BR><BR>

<?php
echo "<table border='1'>
<tr>
<th>Itemname</th>
<th>TotalStock</th>
<th>Totalmass</th>
<th>Totalprice</th>
</tr>";
mysql_data_seek($getmainitem,0);
while($row3 = mysql_fetch_array($getmainitem))
  {
  echo "<tr>";
  echo "<td>" . $row3['itemname'] . "</td>";
  echo "<td>" . $row3['totalstock'] . "</td>";
  echo "<td>" . $row3['totalmass'] . "</td>";
  echo "<td>" . $row3['totalprice'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

Do the while statement first, storing the results for each type of HTML you want to produce:

<?php
$tmp1 = array();
$tmp2 = array();
while ($rw = mysql_fetch_array($getmainitem)) {
    $tmp1[] = '<option value="' . $rw['id'] . '">' . $rw['mainitem]' . '</option>';
    $tmp = array();
    $tmp[] = "<tr>";
    $tmp[] = "<td>" . $rw['itemname'] . "</td>";
    $tmp[] = "<td>" . $rw['totalstock'] . "</td>";
    $tmp[] = "<td>" . $rw['totalmass'] . "</td>";
    $tmp[] = "<td>" . $rw['totalprice'] . "</td>";
    $tmp[] = "</tr>";
    $tmp2[] = implode("\n",$tmp);
}?>
<form name="mainitem" action="../aea/result/mainitem.php" method="POST">
           Select Supplier<select name="mainitem">
<?php echo implode("\n",$tmp1) . "\n"; ?>
           </select>
           <input type="submit" name="submit" />
<?php
echo "<table border='1'>
<tr>
<th>Itemname</th>
<th>TotalStock</th>
<th>Totalmass</th>
<th>Totalprice</th>
</tr>";
echo implode("\n",$tmp2) . "\n";
echo '</table>';
?>

 

Ken

 

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.