[PHP] Re: Help with arrays
Thank you Michael... That did the trick :D thank you very very much, it took me three days... and now it seems so damn easy. vamp "Michael Nolan" <[EMAIL PROTECTED]> escribió en el mensaje news:[EMAIL PROTECTED] > Elliot J. Balanza wrote: > > I need to make a query to a MySQL database that only has two fields, name & > > value. That I can do. > > > > Then I need to store the values i receive in an array in the form of: > > > > $variable['name'] = value; > > > > But haven't been able to do it neither with a foreach nor with a do while... > > Can anyone please assist me? > > > > If you mean how do you store all the returned results in an array of > name/value pairs, this is how I'd do it: > > > while ($result = mysql_fetch_assoc($query)) { > $variable[$result['name']] = $result['value']; > } > > ?> > > Don't know what 'value' represents, but it will give you an array > something like this: > > print_r($variable); > > Array > ( > [michael] => nolan > [joe] => bloggs > [john] => smith > ) > > > HTH, > > Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Help with arrays
I have tried several combinations, including foreach, do whiles. This works when i submit the values one by one, but when i try to automatize it it wont work. Thanks mysql_select_db($database_MBTTIENDA, $MBTTIENDA); $query_prefsname = "SELECT Pref_name FROM mtmbt_prefs"; $prefsname = mysql_query($query_prefsname, $MBTTIENDA) or die(mysql_error()); $row_prefsname = mysql_fetch_assoc($prefsname); mysql_select_db($database_MBTTIENDA, $MBTTIENDA); $query_prefsvalue = "SELECT Pref_value FROM mtmbt_prefs"; $prefsvalue = mysql_query($query_prefsvalue, $MBTTIENDA) or die(mysql_error()); $row_prefsvalue = mysql_fetch_assoc($prefsvalue); // merging them into the new array $MTMBT foreach($row_prefsvalue as $i => $v) { $MTMBT[$row_prefsname[$i]] = $v; } // printing result foreach($MTMBT as $k => $v) { echo "\$MTMBT[$k] => $v."; } echo "the end lets see the results"; print_r($MTMBT); echo ""; echo $MTMBT['Titulo']; <[EMAIL PROTECTED]> escribió en el mensaje news:[EMAIL PROTECTED] > Hello Elliot, > > On 11 Mar 2004 at 23:55, Elliot J. Balanza wrote: > > > I need to make a query to a MySQL database that only has two fields, name & > > value. That I can do. > > > > Then I need to store the values i receive in an array in the form of: > > > > $variable['name'] = value; > > > > But haven't been able to do it neither with a foreach nor with a do while... > > Can anyone please assist me? > > Assuming you've read the introductory sections of the PHP manual and are not trying to > get other people to spend their time on your project so that you won't need to spend > yours, please show us your code so that we can help you spot the problem. > > Erik -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Help with arrays
Hello Elliot, On 11 Mar 2004 at 23:55, Elliot J. Balanza wrote: > I need to make a query to a MySQL database that only has two fields, name & > value. That I can do. > > Then I need to store the values i receive in an array in the form of: > > $variable['name'] = value; > > But haven't been able to do it neither with a foreach nor with a do while... > Can anyone please assist me? Assuming you've read the introductory sections of the PHP manual and are not trying to get other people to spend their time on your project so that you won't need to spend yours, please show us your code so that we can help you spot the problem. Erik -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Help with arrays
Elliot J. Balanza wrote: I need to make a query to a MySQL database that only has two fields, name & value. That I can do. Then I need to store the values i receive in an array in the form of: $variable['name'] = value; But haven't been able to do it neither with a foreach nor with a do while... Can anyone please assist me? If you mean how do you store all the returned results in an array of name/value pairs, this is how I'd do it: while ($result = mysql_fetch_assoc($query)) { $variable[$result['name']] = $result['value']; } ?> Don't know what 'value' represents, but it will give you an array something like this: print_r($variable); Array ( [michael] => nolan [joe] => bloggs [john] => smith ) HTH, Mike -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: help with arrays
>> This is a basic question but I'm a basic fellow. If I have an array >> >> $timespread = array("12am-01am"=>0); >> $timespread["01am-02am"]=0; >> $timespread["02am-03am"]=0; etc >> >> Using $time which is a number, I want to add 1 to the value of >> $timespread[$time] without changing the key so if $time =1 >> I want to have $timespread["01am-02am"]=1; >> >> Using $timespread[$time]+=1; doesn't work. > > Nope, you must say > > $timespread["01am-02am"] += 1 I think the problem here is your understanding on how array key's work. The following method would be far more appropriate: follow or have I lost ya? -- Dan Hardiker [[EMAIL PROTECTED]] ADAM Software & Systems Engineer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: help with arrays
Josh Edwards wrote: > This is a basic question but I'm a basic fellow. If I have an array > > $timespread = array("12am-01am"=>0); > $timespread["01am-02am"]=0; > $timespread["02am-03am"]=0; etc > > Using $time which is a number, I want to add 1 to the value of > $timespread[$time] without changing the key so if $time =1 > I want to have $timespread["01am-02am"]=1; > > Using $timespread[$time]+=1; doesn't work. Nope, you must say $timespread["01am-02am"] += 1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php