Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-10-2007, 10:24 AM   PM User | #1
prostcode
New to the CF scene

 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
prostcode is an unknown quantity at this point
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 
prostcode is offline   Reply With Quote
Old 07-10-2007, 11:34 AM   PM User | #2
StupidRalph
Senior Coder

 
Join Date: Mar 2003
Location: Atlanta
Posts: 1,037
Thanks: 14
Thanked 30 Times in 28 Posts
StupidRalph is on a distinguished road
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.
StupidRalph is offline   Reply With Quote
Old 07-11-2007, 03:03 AM   PM User | #3
prostcode
New to the CF scene

 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
prostcode is an unknown quantity at this point
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?
prostcode is offline   Reply With Quote
Old 07-11-2007, 04:46 PM   PM User | #4
StupidRalph
Senior Coder

 
Join Date: Mar 2003
Location: Atlanta
Posts: 1,037
Thanks: 14
Thanked 30 Times in 28 Posts
StupidRalph is on a distinguished road
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.
StupidRalph is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:59 AM.


Advertisement
Log in to turn off these ads.