View Single Post
Old Nov 20th, 2007, 9:26 AM   #5
davil
Newbie
 
Join Date: Nov 2007
Posts: 27
Rep Power: 0 davil is on a distinguished road
Re: Simple "foreach" problem

Quote:
Originally Posted by DaWei View Post
Perhaps you could explain what you're trying to accomplish with the use of a variable variable ($$key). Personally, I haven't grasped what the overall problem is, from your post.
My apologies... I really have done a bad job of explaining it....
from the top, I have info coming in via the URL data - 'PATH_INFO' - that's info about the current state of a PC for example.. I want to check the current info against the last logged info for said hostname (on a MySQL database).

In the past, when I pulled down info from a MySQL database I used the following code:

php Syntax (Toggle Plain Text)
  1. $sql = "SELECT * FROM `aidahardware` where `hostname` = 'WHATEVER' LIMIT 1"; // or $sql could be any query but this is just to give you an idea
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $row = mysql_fetch_array($result);
  4.  
  5. $field1=$row['field1'];
  6. $field2=$row['field2'];
  7. $field3=$row['field3'];
  8. $field4=$row['field4'];
and so on

but I found out that if you have many fields to assign to variables you can use this code instead:
php Syntax (Toggle Plain Text)
  1. $sql = "SELECT * FROM `aidahardware` where `hostname` = 'WHATEVER' LIMIT 1"; // or $sql could be any query but this is just to give you an idea
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $row = mysql_fetch_array($result);
  4. foreach ($row as $key => $value) {$$key = $value;}

so I thought if while I was going through that foreach - type loop I could also check each field's value against the newer values then I would be onto a winner... I can do it very easily with multiple "if" conditions afterwards but I just wondered was there an easier way... I can almost see it in my head already but I just can't put my finger on what I'm doing wrong.

So to summarise, the reason I'm using a variable variable is for quicker coding... and no need to change code when I add a new field to my project

Thanks for all your help
davil is offline   Reply With Quote