Jump to content

Increase Number by 1 each time


dachshund

Recommended Posts

Probably an easy question but I can't find a solution on Google.

 

I have this code:

 

$sql = "SELECT * FROM store WHERE id LIKE '$id'";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result))	{
echo $rows['title'];
}

 

before the title i would like to have a number, starting at 1. so for example if it echoed out 10 titles each would have a corresponding number -  1, 2, 3, etc.

 

anyone know an easy solution?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/
Share on other sites

thanks, i tried that out but it's just giving me 1 for each.

 

my code is:

 

<?php
$sql = "SELECT * FROM store WHERE id LIKE '$id'";
$result = mysql_query($sql);
while ($rows = mysql_fetch_array($result))	{
?>
<input type="hidden" name="item_number_<?php $i=1; echo $i; ++$i; ?>" value="<?php $i=1; echo $i; ++$i; ?>">
<?php
}
?>

thanks, i tried that out but it's just giving me 1 for each.

 

my code is:

 

<?php
$sql = "SELECT * FROM store WHERE id LIKE '$id'";
$result = mysql_query($sql);
$i=1;
while ($rows = mysql_fetch_array($result))	{
?>
<input type="hidden" name="item_number_<?php  echo $i; ++$i; ?>" value="<?php $i=1; echo $i; ++$i; ?>">
<?php
}
?>

 

Try that.... you have to set the $i once, and set it outside of the loop, then each run through the loop simply increment its value by 1. You had it inside the loop and set it to 1 each run through.

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.