Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: count results?

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default count results?

    is it possible to assign a number to a result in loop?

    e.g.

    PHP Code:
    <?php
    $iddaddress 
    "localhost";
    $iddusername "xxxx";
    $iddpassword "xxxx";
    $idddb "xxxx";
    $iddconn mysql_connect($iddaddress$iddusername$iddpassword);
    $iddrs mysql_select_db($idddb$iddconn);
    $iddsql="SELECT * FROM handset GROUP BY handset";
    $iddrs mysql_query($iddsql$iddconn);
    $iddj 0;
    while(
    $iddrow mysql_fetch_array($iddrs)) { 
    $iddmake $iddrow[make];
    $iddhand $iddrow[handset];
    $idddetails $iddrow[details];

    echo(
    "This is result number: $resultnumber");
      
    $j++; 
    }
    mysql_close();
    ?>
    Thanks for your help.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    $iddj = 0;
    while($iddrow = mysql_fetch_array($iddrs)) { 
    $iddmake = $iddrow[make];
    $iddhand = $iddrow[handset];
    $idddetails = $iddrow[details];
    
    echo("This is result number: $iddj");
      $iddj++; 
    }
    Should work.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    for ($i=0;$i=-1;$i++) {
    //do something;
    }
    I wouldn't recommend it, though. Could cause your computer to crash, but I believe some servers only allow so many to go through before it stops. But, yes, that script will loop indefinitely.
    - Mike

  4. #4
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    hey.. thanks for your replies..

    i've tried the 1st code, but it seems to show all the deals as number 0

    any ideas why?! cheers.

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Because the while loop does not iterate through a number, it only returns the values until the condition is false.
    - Mike

  6. #6
    Join Date
    Jun 2006
    Location
    Acton Ontario Canada.
    Posts
    677
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    $num = 0;
    while (....) {
    ....
    echo $num; num++;
    }
    - Ryan "Boxxertrumps" Trumpa
    Come back once it validates: HTML, CSS, JS.

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by boxxertrumps View Post
    $num = 0;
    while (....) {
    ....
    echo $num; num++;
    }
    That's basically what I did, with the exception of incorporate it into the OP's code.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is what for loops are for:
    Code:
    for($resultnumber = 1; $iddrow = mysql_fetch_array($iddrs); ++$resultnumber)
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Shouldn't it be:
    Code:
    for($resultnumber = 1; $iddrow = count(mysql_fetch_array($iddrs)); ++$resultnumber)
    Seeings it returns an array?
    - Mike

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Not sure if this is related, but for counting, this is helpful--
    http://www.php.net/manual/en/functio...l-num-rows.php
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •