Jump to content

[SOLVED] get $r loop to increment number?


wmguk

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 "'>";
 }
?>

Archived

This topic is now archived and is closed to further replies.

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