Jump to content

Recommended Posts

Hey,

 

I have existing code, and I've been trying to get this to display $r as the row number - ie if there are 4 results found the $r = 1 on the first, 2 on the second etc etc, but I can only get it to display either 2, or 1234, not loop through... Can anyone see my mistake?

 

<?PHP
while ($row=mysql_fetch_assoc($countresult))
{
$r = 1;
$r <= $used;
$r++ ;
?>
<tr><td class="rightmenu">Size:td>
<td width="5"> </td>
<td><?
echo "<input type='text' class='boxes' name='size_$r' value='";
echo $row['size'];
echo "'>";
?>
</td>
<td class="rightmenu">Cost:</td>
<td width="5"> </td>
<td><?
echo "<input type='text' class='boxes' name='cost_$r' value='";
echo $row['cost']; 
echo "'>";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/113369-solved-get-r-loop-to-increment-number/
Share on other sites

Well this bunch of code is not quote right:

$r = 1;
$r <= $used;
$r++ ;

Your set $r to 1

then you are incorrectly comparing to see if $r is then or equal to $used (which doesn't seem to exis), if it is increment $r

 

The first line should be outside of the while loop. The next two lines should be:

if($r <= $used) $r++;

Well this bunch of code is not quote right:

$r = 1;
$r <= $used;
$r++ ;

Your set $r to 1

then you are incorrectly comparing to see if $r is then or equal to $used (which doesn't seem to exis), if it is increment $r

 

The first line should be outside of the while loop. The next two lines should be:

if($r <= $used) $r++;

 

Sorry, $used is set before the head of the page

<?PHP
$refnum = $_GET['refnum'];
include "scripts/connection.php";
$countresult = mysql_query("SELECT * FROM sizes WHERE originalprod = '$refnum' ");
$used = mysql_num_rows($countresult);
$count = 10 ;
?>

Excellent thank you, thats solved it!!

 

<?PHP
$r = 0;
while ($row=mysql_fetch_assoc($countresult))
{
if($r <= $used) $r++;
?>
<tr><td class="rightmenu">Size:<td>
<td width="5"> </td>
<td><?
echo "<input type='text' class='boxes' name='size_$r' value='";
echo $row['size'];
echo "'>";
?>
</td>
<td class="rightmenu">Cost:</td>
<td width="5"> </td>
<td><?
echo "<input type='text' class='boxes' name='cost_$r' value='";
echo $row['cost']; 
echo "'>";
 }
?>

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.