Help with Dynamic Variable Names

Reply

Join Date: Apr 2008
Posts: 1
Reputation: Lamaison is an unknown quantity at this point 
Solved Threads: 0
Lamaison Lamaison is offline Offline
Newbie Poster

Help with Dynamic Variable Names

 
0
  #1
May 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 141
Reputation: robothy is an unknown quantity at this point 
Solved Threads: 19
robothy robothy is offline Offline
Junior Poster

Re: Help with Dynamic Variable Names

 
0
  #2
May 6th, 2008
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.

$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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Help with Dynamic Variable Names

 
0
  #3
May 6th, 2008
Originally Posted by Lamaison View Post
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
the way to declare vars with dynamic names is as follows.

  1. $var1 = "hel";
  2. $var2 = "lo";
  3. $hello = "yo there";
  4.  
  5. echo ${$var1.$var2};
  6.  
  7. output = "yo there";
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC