RomanRemake Posted August 27, 2008 Share Posted August 27, 2008 Hi! I'm trying to use a php foreach tag count from 1 to 5 (I have 5 things listed). E.g. Right now I have: Dogs Cats Squirrels Mice Rats What I want is (using php for each): 1. Dogs 2. Cats 3. Squirrels 4. Mice 5. Rats Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/ Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 Where is the data coming from, an array? A database? If a database is it stored in seperate fields, exploded from one field??? Please give more info... Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626512 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 If these are in an array, you can do this: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626513 Share on other sites More sharing options...
genericnumber1 Posted August 27, 2008 Share Posted August 27, 2008 I would suggest using a for() loop, or merge()+range()... but if you HAVE to use a foreach loop... <?php $array = array('Dogs', 'Cats', 'Squirrels', 'Mice', 'Rats'); $i = 1; $newArray = array(); foreach($array as $element) { $newArray[] = "$i. $element"; ++$i; } ?> If you're using it to display in html, I would suggest the method above. Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626514 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 Using a foreach: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); $i = 1; foreach($things as $element) echo $i++ . '. '. $element . '<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626518 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 <?php $array = array('Dogs', 'Cats', 'Squirrels', 'Mice', 'Rats'); for ($i = 0; $i < count($array); $i++){ $n = $i + 1; echo $n . '.' . $array[$i] . '<br />'; } ?> Personally I would do it that way... Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626521 Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 If these are in an array, you can do this: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>'; ?> Ken Ooh, that is slick! Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626524 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 Another method using a foreach: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); foreach($things as $n => $element) echo ++$n . '. ' . $element . '<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626526 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 If these are in an array, you can do this: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>'; ?> Ken Ooh, that is slick! Yeh I just tested it and it worked, but I dont get how imploding an array works :S Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626530 Share on other sites More sharing options...
Guest Xanza Posted August 27, 2008 Share Posted August 27, 2008 You'll have to put the items into an array, but you don't have to use foreach()... You could ues a simple for() like so: <?php $info[1] = "Dogs"; $info[2] = "Cats"; $info[3] = "Squirrels"; $info[4] = "Mice"; $info[5] = "Rats"; for($i = 1; $i <= count($info); $i++){ echo "$i. $info[$i]"."<br>"; } ?> Outputs: 1. Dogs 2. Cats 3. Squirrels 4. Mice 5. Rats Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626532 Share on other sites More sharing options...
genericnumber1 Posted August 27, 2008 Share Posted August 27, 2008 If these are in an array, you can do this: <?php $things = array('Dogs','Cats','Squirrels','Mice','Rats'); echo '<ol><li>' . implode('</li><li>',$things) . '</li></ol>'; ?> Ken Ooh, that is slick! Yeh I just tested it and it worked, but I dont get how imploding an array works :S an array is all you can implode http://us.php.net/implode Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626533 Share on other sites More sharing options...
akitchin Posted August 27, 2008 Share Posted August 27, 2008 for the record, we should be in the habit of running the count() function outside of the for() loop, otherwise the loop runs it on every iteration and wastes valuable DIGIRESOURCES!! Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626534 Share on other sites More sharing options...
RomanRemake Posted August 27, 2008 Author Share Posted August 27, 2008 Thank you for all of the replies (and so fast!). Sorry for not clarifying. I'm pulling the info from a database. Here's the current code: <?php foreach($listings AS $listing):?> <?php echo $Routes->content($listing['Listing']['title'],$listing);?> <?php endforeach;?> Take care, RR Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626544 Share on other sites More sharing options...
Andy-H Posted August 27, 2008 Share Posted August 27, 2008 :s my bad, long time since I used implode/explode lol Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626549 Share on other sites More sharing options...
kenrbnsn Posted August 27, 2008 Share Posted August 27, 2008 You really shouldn't go in and out of php for each line. Do something like this: <?php $i = 1; foreach($listings AS $listing) echo $i++ . '. ' . $Routes->content($listing['Listing']['title'],$listing) . '<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626568 Share on other sites More sharing options...
RomanRemake Posted August 27, 2008 Author Share Posted August 27, 2008 Thank you, Ken, it worked perfect! Is there a way to donate to this place? Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626590 Share on other sites More sharing options...
akitchin Posted August 27, 2008 Share Posted August 27, 2008 Thank you, Ken, it worked perfect! Is there a way to donate to this place? why yes, there is! http://www.phpfreaks.com/forums/index.php/topic,45685.0.html we're glad people are appreciative . Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626602 Share on other sites More sharing options...
genericnumber1 Posted August 27, 2008 Share Posted August 27, 2008 for the record, we should be in the habit of running the count() function outside of the for() loop, otherwise the loop runs it on every iteration and wastes valuable DIGIRESOURCES!! NOT THE DIGIRESOURCES!!!111oneleven :s my bad, long time since I used implode/explode lol but they're EXPLODE and IMPLODE... they're the coolest named functions in the language.. Of course they lose to fopen (phhhhopen!!!!) in the "most fun to say" category. Yeah, I hold my own language awards where I give awards to the "most *" functions in the library... that doesn't make me weird, right? .. I'll shut up now. Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626611 Share on other sites More sharing options...
RomanRemake Posted August 27, 2008 Author Share Posted August 27, 2008 why yes, there is! http://www.phpfreaks.com/forums/index.php/topic,45685.0.html we're glad people are appreciative . I'm very appreciate of all the generous help. Just donated. Keep up the great work! Quote Link to comment https://forums.phpfreaks.com/topic/121490-number-series-12345/#findComment-626639 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.