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
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
}
?>

Link to comment
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);
$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.

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.