By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

This should be easy, but...

P: n/a
Hi

I'm trying to build a simple, one-dimensional array of prices, where the key
to each element is the product code.

So I start by executing a select query "SELECT Code, Price FROM Products",
and the results are returned in a two-dimensional array, $data, which I can
loop through like this:

while (list(, $value) = each($data))
{
$code = $value[0];
$price = $value[1];

I would have thought I could build my array here by writing something like:

$command = "\$arr['$code'] = $price;";
exec($command);

But this doesn't work.

Can anyone explain?
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
iuz
Captain Nemo wrote:
Hi

I'm trying to build a simple, one-dimensional array of prices, where the
key to each element is the product code.

So I start by executing a select query "SELECT Code, Price FROM Products",
and the results are returned in a two-dimensional array, $data, which I
can loop through like this:

while (list(, $value) = each($data))
{
$code = $value[0];
$price = $value[1];

I would have thought I could build my array here by writing something
like:

$command = "\$arr['$code'] = $price;";
exec($command);

But this doesn't work.

Can anyone explain?


there are some errors in your code..
if you want an array like this
array(
'code' => 'price'
)
you can do something like this..

while(list($code, $value) = mysql_fetch_array($data))
$product[$code] = $value;

--
www.iuz-lab.info
Jul 17 '05 #2

P: n/a
Panic over!

I was confusing the exec() function with the eval() function.
Jul 17 '05 #3

P: n/a
while (list(, $value) = each($data))
{
$code = $value[0];
$price = $value[1];
/*
$command = "\$arr['$code'] = $price;"; // I belive you
// need \ before all $s
exec($command); // you want eval() here
*/
// wouldn't this be a bit more concise:
$arr[$code] = $price; // why all the command and eval hubbaabaloo?
}

or how about this:
foreach ( $data as $values )
{
$arr[$values[0]] = $values[1];
// or if you used mysql_fetch_assoc or mysql_fetch_array:
// $arr[$values['Code']] = $values['Price'];
}


"Captain Nemo" <Ca*****@NoSpam.com> wrote in message news:<Hj****************@text.news.blueyonder.co.u k>...
Hi

I'm trying to build a simple, one-dimensional array of prices, where the key
to each element is the product code.

So I start by executing a select query "SELECT Code, Price FROM Products",
and the results are returned in a two-dimensional array, $data, which I can
loop through like this:

while (list(, $value) = each($data))
{
$code = $value[0];
$price = $value[1];

I would have thought I could build my array here by writing something like:

$command = "\$arr['$code'] = $price;";
exec($command);

But this doesn't work.

Can anyone explain?

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.