| | |
Help with Dynamic Variable Names
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2008
Posts: 1
Reputation:
Solved Threads: 0
I'm a PHP newbie, I need to assign query results to variables that have an incrementing number in the variable name, can someone help me with this?
So let say I return a few rows of from MySQL, I would like loop though the results and assign the values to variables with incrementing numbers like the following:
where 1 is the incrementing
I eventually need to put these results into a URL like this
&sku_1=1234&quantity_1=1
&sku_2=1235&quantity_2=1
&sku_3=1236&quantity_3=2
Thanks,
Marc
So let say I return a few rows of from MySQL, I would like loop though the results and assign the values to variables with incrementing numbers like the following:
while($row1 = mysql_fetch_assoc($result1)) {
$sku_1 = row1["sku"];
$quantity_1 = row1["quantity"];
};where 1 is the incrementing
I eventually need to put these results into a URL like this
&sku_1=1234&quantity_1=1
&sku_2=1235&quantity_2=1
&sku_3=1236&quantity_3=2
Thanks,
Marc
•
•
Join Date: Jan 2008
Posts: 141
Reputation:
Solved Threads: 19
Hi Marc,
I had a look at this last night. Sorry I didn't post a reply then. But here is what I was thinking you could do.
If you create a multi-dimensional array within which you store the results from your query. The inner array would be indexed by the keys 'sku' and 'qty', although these could be anything.
If you provide a little more detail as to how you want to use the output, there might be a better solution for dealing with the construction of the url's.
Also, I noticed that you had a ; after the trailing while bracket. This is not required.
Hope this is helpful.
R
I had a look at this last night. Sorry I didn't post a reply then. But here is what I was thinking you could do.
If you create a multi-dimensional array within which you store the results from your query. The inner array would be indexed by the keys 'sku' and 'qty', although these could be anything.
$products = array();
$intC = 1; // Integer counter to iterate through array.
while($row1 = mysql_fetch_assoc($result1)) {
$products[$intC] = array('sku' => row1["sku"], 'qty' => row1["quantity"]);
$intC++;
}
// For the output, you could then use a for loop. Or alternatively nested foreach loops. And you will be given an array indexed by an integer with each url string
$url = array();
for($i = 1; $i <= $intC; $i++) {
$url[$i] = "&sku_$i={$products[$i]['sku']}&quantity_$i={$products[$i]['qty']}";
}If you provide a little more detail as to how you want to use the output, there might be a better solution for dealing with the construction of the url's.
Also, I noticed that you had a ; after the trailing while bracket. This is not required.
Hope this is helpful.
R
Last edited by robothy; May 6th, 2008 at 4:26 am.
•
•
•
•
I'm a PHP newbie, I need to assign query results to variables that have an incrementing number in the variable name, can someone help me with this?
So let say I return a few rows of from MySQL, I would like loop though the results and assign the values to variables with incrementing numbers like the following:
while($row1 = mysql_fetch_assoc($result1)) { $sku_1 = row1["sku"]; $quantity_1 = row1["quantity"]; };
where 1 is the incrementing
I eventually need to put these results into a URL like this
&sku_1=1234&quantity_1=1
&sku_2=1235&quantity_2=1
&sku_3=1236&quantity_3=2
Thanks,
Marc
PHP Syntax (Toggle Plain Text)
$var1 = "hel"; $var2 = "lo"; $hello = "yo there"; echo ${$var1.$var2}; output = "yo there";
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
![]() |
Similar Threads
- get length of a dynamic array (C++)
- dynamic array variable.. (PHP)
- Workaround to (true)dynamic arrays (VB.NET)
- dynamic memory... (C++)
- Using mysqli_bind_param with an unknown number of variables (PHP)
- Dynamic Javascript (JavaScript / DHTML / AJAX)
- Java Semantic Analyzer (Java)
- How to use flash successfully (Graphics and Multimedia)
- Messy array things. (C++)
Other Threads in the PHP Forum
- Previous Thread: JPGraph+headers
- Next Thread: Puzzle..IE7 slow while firefox and safari run fine
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube





