Current location: Hot Scripts Forums » Programming Languages » PHP » how do i get the total number of listed items in a for loop?


how do i get the total number of listed items in a for loop?

Reply
  #1 (permalink)  
Old 02-06-05, 11:08 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
how do i get the total number of listed items in a for loop?

i have a for loop that lists a number of things. i'm trying to get the total number of things listed in the loop but can't make it work.

any ideas, tips or hints?



this is what my loop looks like (which by the way works great, thanks to moronovich/mike)
PHP Code:

for($i=0;$i<count($selsongvalue); $i++) {

    echo 
'
    <tr height="20">
        <td class="musicsearchlist">'
$strNumber $i 1; echo $strNumber; echo '<!-- song -->'.str_replace('+',' ',$selsongvalue[$i]).'</td>
        <td height="20"><!-- chkbox --><input type="checkbox" name="'
.$selcheckboxvalue[$i].'" value="songName='.$selcheckboxvalue[$i].'" checked></td>
        <td></td>
        <td></td>
        <td class="musicsearchlist"><!-- album -->'
.str_replace('+',' ',$selalbumvalue[$i]).'</td>
        <td></td>
        <td></td>
        <td class="musicsearchlist">'
.str_replace('+',' ',$selartistvalue[$i]).'</td>
        <td></td>
    </tr>'
        
;} 


thankful for any help on this!
Reply With Quote
  #2 (permalink)  
Old 02-06-05, 11:27 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by nassau
i have a for loop that lists a number of things. i'm trying to get the total number of things listed in the loop but can't make it work.

PHP Code:

for($i=0;$i<count($selsongvalue); $i++) {

    echo 
'
    <tr height="20">
        <td class="musicsearchlist">'
$strNumber $i 1; echo $strNumber; echo '<!-- song -->'.str_replace('+',' ',$selsongvalue[$i]).'</td>
        <td height="20"><!-- chkbox --><input type="checkbox" name="'
.$selcheckboxvalue[$i].'" value="songName='.$selcheckboxvalue[$i].'" checked></td>
        <td></td>
        <td></td>
        <td class="musicsearchlist"><!-- album -->'
.str_replace('+',' ',$selalbumvalue[$i]).'</td>
        <td></td>
        <td></td>
        <td class="musicsearchlist">'
.str_replace('+',' ',$selartistvalue[$i]).'</td>
        <td></td>
    </tr>'
        
;} 
Maybe I've misunderstood, but couldn't you use the value of $i?

If not, stick a simple counter at the bottom of the loop and use its value:

$mycounter++;

Each time it loops it'll increment $mycounter. Make sure you initiaslize it before you begin counting or you'll be off by 1 every time.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 02-07-05, 06:42 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
hi, i know i can use $i but i don't know how to do it. i can use $i to number the rows but not get a total number of rows listed. how is that done?

thanks
Reply With Quote
  #4 (permalink)  
Old 02-07-05, 10:09 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
problem solved - thank you!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
is it a good idea to run 23 search expressions? lordmerlin ASP 1 03-24-04 04:05 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM


All times are GMT -5. The time now is 05:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.