Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation create array from while loop

    PHP Code:
    $query "SELECT * FROM table";
    $result mysql_query($query);

       while(
    $rows mysql_fetch_array($result)){
           
    $code=$rows['code'];
           
    $pt=$rows['points'];
           
    $code_array = array($code);

           foreach(
    $code_array as $key => $value) {
               echo 
    $key" " $value "<br>";
           }

       } 
    instead the output is:
    PHP Code:
    0 Code1
    0 Code2
    0 Code3
    0 Code4
    0 Code5 
    I want the output to be:
    PHP Code:
    0 Code1
    1 Code2
    2 Code3
    3 Code4
    4 Code5 

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    $query "SELECT * FROM table";
    $result mysql_query($query);
    $code_array = array();

       while(
    $rows mysql_fetch_array($result)){
           
    $code=$rows['code'];
           
    $pt=$rows['points'];
           
    $code_array[] = $code;

           foreach(
    $code_array as $key => $value) {
               echo 
    $key" " $value "<br>";
           }

       } 
    Try this instead...I have not tested it...
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by StupidRalph View Post
    PHP Code:
    $query "SELECT * FROM table";
    $result mysql_query($query);
    $code_array = array();

       while(
    $rows mysql_fetch_array($result)){
           
    $code=$rows['code'];
           
    $pt=$rows['points'];
           
    $code_array[] = $code;

           foreach(
    $code_array as $key => $value) {
               echo 
    $key" " $value "<br>";
           }

       } 
    Try this instead...I have not tested it...
    thanks. it works this time but the output looks weird

    PHP Code:
    0 Code1
    0 Code1
    1 Code2
    2 Code3
    3 Code4
    0 Code5
    0 Code5 

    How? Actually I want to do sorting. I want to get the duplicate values to do some comparison. Do you know how?

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    PHP Code:
    $query "SELECT * FROM table";
    $result mysql_query($query);
    $code_array = array();

       while(
    $rows mysql_fetch_array($result)){
           
    $code=$rows['code'];
           
    $pt=$rows['points'];
           
    $code_array[] = $code;       
       }  

    foreach(
    $code_array as $key => $value) {
               echo 
    $key" " $value "<br>";
           } 
    Pull the foreach out of the while loop. PHP has a lot of different sort functions. Consult the php manual
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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