dachshund Posted December 6, 2011 Share Posted December 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/ Share on other sites More sharing options...
freelance84 Posted December 6, 2011 Share Posted December 6, 2011 There are a few things you could do. You could use a for loop Or you could increase a variable within your while loop: $i=1; while ($rows = mysql_fetch_array($result)) { echo "$i - $rows['title']"; ++$i; } Quote Link to comment https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/#findComment-1294858 Share on other sites More sharing options...
dachshund Posted December 6, 2011 Author Share Posted December 6, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/#findComment-1294862 Share on other sites More sharing options...
freelance84 Posted December 6, 2011 Share Posted December 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/#findComment-1294867 Share on other sites More sharing options...
dachshund Posted December 6, 2011 Author Share Posted December 6, 2011 that's the ticket thanks! Quote Link to comment https://forums.phpfreaks.com/topic/252569-increase-number-by-1-each-time/#findComment-1294871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.