| | |
dynamic array and dymanic variables
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
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?
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:
PHP Syntax (Toggle Plain Text)
$in_colour = new InputSelect('in_colour'); $in_colour->rows = array(array($co1,$co1), array($co2,$co2), array($co3,$co3));
colour.php?val=4&co1=Red&co2=Blue&co3=Green&co4=Yellow
Please can someone help me?
•
•
Join Date: Jul 2008
Posts: 95
Reputation:
Solved Threads: 4
PHP Syntax (Toggle Plain Text)
// get number of colors $val = $_GET['val']; // $val starts from 1 for($i = 1; $i <= $val; $i++) { $color = $_GET["col".$val]; echo $color; }
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.
PHP Syntax (Toggle Plain Text)
$variable_name = "col" . $val; echo ${$variable_name} // gives the last color
Last edited by jakesee; Apr 20th, 2009 at 7:07 am.
•
•
Join Date: Apr 2009
Posts: 3
Reputation:
Solved Threads: 0
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 .....);
$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 .....);
•
•
Join Date: Jul 2008
Posts: 95
Reputation:
Solved Threads: 4
•
•
•
•
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 .....);
PHP Syntax (Toggle Plain Text)
// this code will (should) do what you are asking for, but it's not the best method imo. $final_array = array(); for($i = $val; $i >= 1; $i--) { $array = "array". $val; $co = "co".$val; ${$array} = array(${$co}, ${$co}); array_push($final_array, ${$array}); }
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
PHP Syntax (Toggle Plain Text)
// this is better. for($i= count($co); $i >= 0; $i++) { $final_array[$i] = array($co[$i], $co[$i]); }
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.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Page authentication
- Next Thread: ajax problem
Views: 409 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download duplicates dynamic echo email error execution file files folder form forms function functions google href htaccess html htmlspecialchars image include insert integration ip java javascript joomla jquery limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php phpvotingscript problem query radio random recursion regex remote script search select server session sessions sms soap source space speed sql structure syntax system table tutorial update updates upload url validation validator variable video web xml youtube





