dynamic array and dymanic variables

Thread Solved

Join Date: Apr 2009
Posts: 3
Reputation: cinsPHP is an unknown quantity at this point 
Solved Threads: 0
cinsPHP cinsPHP is offline Offline
Newbie Poster

dynamic array and dymanic variables

 
0
  #1
Apr 20th, 2009
Hi all, Im a still very new to PHP and don't know how to attempt the following:
I'm trying to build a dynamic array in a form i'm utilizing. Also I am passing parameters from the url as follows:
colour.php?val=3&co1=Red&co2=Blue&co3=Green
//where val is the iteration number and co1,co2,co3 is the three colours to input // I need to change in such a way that val can be any number and where i'll accordingly pass the colours to match that number
Currently my static code is:
  1. $in_colour = new InputSelect('in_colour');
  2. $in_colour->rows = array(array($co1,$co1),
  3. array($co2,$co2),
  4. array($co3,$co3));
This is working fine and creates a input select with the three colours I have passed. How do I change the code to build a dynamic array where I can change the val to lets say 4 or 5 or whatever: For example
colour.php?val=4&co1=Red&co2=Blue&co3=Green&co4=Yellow

Please can someone help me?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 95
Reputation: jakesee is an unknown quantity at this point 
Solved Threads: 4
jakesee jakesee is offline Offline
Junior Poster in Training

Re: dynamic array and dymanic variables

 
0
  #2
Apr 20th, 2009
  1. // get number of colors
  2. $val = $_GET['val'];
  3.  
  4. // $val starts from 1
  5. for($i = 1; $i <= $val; $i++)
  6. {
  7. $color = $_GET["col".$val];
  8. echo $color;
  9. }

After a second thought, i'm not sure if i understood you question. Because there's no dynamic arrays involved. If you really want dynamic array.

  1. $variable_name = "col" . $val;
  2. echo ${$variable_name} // gives the last color
Last edited by jakesee; Apr 20th, 2009 at 7:07 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: cinsPHP is an unknown quantity at this point 
Solved Threads: 0
cinsPHP cinsPHP is offline Offline
Newbie Poster

Re: dynamic array and dymanic variables

 
0
  #3
Apr 20th, 2009
The thing is that I need to build the array as follow:
$array1 = array($co1,$co1);
$array2 = array($co2,$co2);
$array3 = array($co3,$co3);
$array.... = array($co....,$co....);
//where the ..... represents an iteration number so if the iteration number was 5 then it need to build 5 arrays
// so i've got a variable $array[i] and also a variable $co[i] - these two's [i]-value correpsond

finally i need to put the arrays together:
$final_array = array($array1, $array2, $array3, $array .....);
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 95
Reputation: jakesee is an unknown quantity at this point 
Solved Threads: 4
jakesee jakesee is offline Offline
Junior Poster in Training

Re: dynamic array and dymanic variables

 
0
  #4
Apr 20th, 2009
Originally Posted by cinsPHP View Post
The thing is that I need to build the array as follow:
$array1 = array($co1,$co1);
$array2 = array($co2,$co2);
$array3 = array($co3,$co3);
$array.... = array($co....,$co....);
//where the ..... represents an iteration number so if the iteration number was 5 then it need to build 5 arrays
// so i've got a variable $array[i] and also a variable $co[i] - these two's [i]-value correpsond

finally i need to put the arrays together:
$final_array = array($array1, $array2, $array3, $array .....);
  1. // this code will (should) do what you are asking for, but it's not the best method imo.
  2. $final_array = array();
  3. for($i = $val; $i >= 1; $i--)
  4. {
  5. $array = "array". $val;
  6. $co = "co".$val;
  7. ${$array} = array(${$co}, ${$co});
  8. array_push($final_array, ${$array});
  9. }

alter the URL such that it goes
colour.php?val=3&co[0]=Red&co[1]=Blue&co[2]=Green
or
colour.php?co[]=Red&co[]=Blue&co[]=Green
  1. // this is better.
  2. for($i= count($co); $i >= 0; $i++)
  3. {
  4. $final_array[$i] = array($co[$i], $co[$i]);
  5. }


codes not tested but the logic is there. I think you need to read up on "php arrays", the "php array functions" and "passing arrays using HTML GET". Google for these.
Last edited by jakesee; Apr 20th, 2009 at 7:59 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 3
Reputation: cinsPHP is an unknown quantity at this point 
Solved Threads: 0
cinsPHP cinsPHP is offline Offline
Newbie Poster

Re: dynamic array and dymanic variables

 
0
  #5
Apr 20th, 2009
Thank you so much! It's working.
Also thank you on the advise of the co[0], co[1], etc.
Very clever - Now I can access these variables with the for statement.
Thanx!!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum


Views: 409 | Replies: 4
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC