soma56 Posted June 15, 2010 Share Posted June 15, 2010 Does anyone have an example code of a counter? For example: If I had an array echoing results I'd like to have a counter that remains in the same spot - the value would change as results were echoed. I can't seem to find any samples online. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/ Share on other sites More sharing options...
B0b Posted June 15, 2010 Share Posted June 15, 2010 $counter = 0; foreach ( $array as $element ) { echo $element; $counter++; } Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072651 Share on other sites More sharing options...
soma56 Posted June 15, 2010 Author Share Posted June 15, 2010 Thanks Bob, assuming that the array only has 10 values would that result in: 12345678910 or 10 ? What I'm looking for is that latter. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072658 Share on other sites More sharing options...
B0b Posted June 15, 2010 Share Posted June 15, 2010 This would display 12345678910. What you would be looking for is: echo count( $array ) . ''; Or: $counter = 0; foreach ( $array as $element ) { $counter++; } echo $counter; You're welcome. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072670 Share on other sites More sharing options...
soma56 Posted June 15, 2010 Author Share Posted June 15, 2010 Thanks Bob. I appreciate it and i'll try it now. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072700 Share on other sites More sharing options...
soma56 Posted June 15, 2010 Author Share Posted June 15, 2010 Again, I appreciate it Bob but it's not what I'm looking for. I'm trying to figure out how to have a counter that is static, doesn't move or is otherwise always in the same place. I understand the concept of adding++ 'while' something is 'not true' however what I don't get is how you can echo a variable to the same spot as a means of letting know in real time. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072710 Share on other sites More sharing options...
Alex Posted June 16, 2010 Share Posted June 16, 2010 You can't do what you're looking for in PHP. You can accomplish this with JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072713 Share on other sites More sharing options...
soma56 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks Alex, I'm looking at a javascript solution now. Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072719 Share on other sites More sharing options...
B0b Posted June 16, 2010 Share Posted June 16, 2010 You should go through W3Schools tutorials: http://www.w3schools.com/ <body> <div id='counter'></div> </body> <script type="text/javascript"> theArray = new Array( '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ); for ( counter in theArray ) { document.getElementById( 'counter' ).innerHTML = theArray[counter]; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072722 Share on other sites More sharing options...
soma56 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks Bob, I'm still lost and if you care to point out where that tutorial is I'd be happy to review it. http://www.google.com/search?sitesearch=www.w3schools.com&as_q=counter+java Quote Link to comment https://forums.phpfreaks.com/topic/204893-counter-quesiton/#findComment-1072755 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.