Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Simple "foreach" problem (http://www.programmingforums.org/showthread.php?t=14535)

davil Nov 20th, 2007 8:04 AM

Simple "foreach" problem
 
Ok so I'm getting into PHP and I have a decent sized MySQL/PHP database system working fine but I'm having problems with the foreach command...

When I get the info from a MySQL database I usually use the following command to assign a variable to each value:

:

  1. foreach ($row as $key => $value) {$$key = $value;}


This works perfectly but I was wondering is there a way to check something like $datestamp vs $new_datestamp and $timestamp vs $new_timestamp by doing something like this code below?

:

  1. foreach ($row as $key => $value) {
  2. $$key = $value;
  3. if ($new_$$key==$$key){echo "$new_$$key is same as old $$key, which is $value\n";
  4. }



I know this doesn't work above but I'm presuming it's something to do with my syntax... I've had a look at PHP.net tutorials and done a bit of googling but I'm lost on this simplest of all problems...

I could do it the long way with like 20 conditions but I want to get more efficient in my programming.

PhilBon Nov 20th, 2007 8:31 AM

Re: Simple "foreach" problem
 
What is the Error that you get? PHP has a great Error messages

davil Nov 20th, 2007 8:46 AM

Re: Simple "foreach" problem
 
Quote:

Originally Posted by PhilBon (Post 137205)
What is the Error that you get? PHP has a great Error messages

parse error, unexpected '$' in W:\www\it2\aida\newinfo.php on line 47
but then I could have told you that just by looking at the code above that it wasn't right. I was just trying to get across what kind of code I meant... what it should do more or less... I thought if I put in my interpretation somebody could see where I went wrong and correct it for me....

I have tried a few variations like :
:

  1. if ($new_$$key==$$key){echo "$new_$$key is equal to $$key\n";}
  2. or
  3. if ($new_$key==$$key){echo "$new_$key is equal to $$key\n";}

and a few others and I know it's something stupid I'm doing wrong and somebody can help... anyway just to make it clear - i'm trying to check a load of variables against their 'new' counterparts...

everything will become clear when I post entire code:
:

  1. <pre>
  2. <?php
  3. require ("..\config.php");
  4.  
  5.  
  6. $repl="$-$$";
  7. $spr="*";
  8. $data = explode($repl,$HTTP_SERVER_VARS['PATH_INFO']);
  9.  
  10.  
  11. $new_datestamp = $data[1];
  12. $new_datestamp=str_replace($repl, "/", $new_datestamp);
  13. $new_timestamp = str_replace($spr," ", $data[2]);
  14. $new_hostname = str_replace($spr," ", $data[3]);
  15. $new_serial1 = str_replace($spr," ", $data[4]);
  16. $new_serial2 = str_replace($spr," ", $data[5]);
  17. $new_serial3 = str_replace($spr," ", $data[6])$serial3 = str_replace(".","", $serial3);
  18. $new_primaryip = str_replace($spr," ", $data[7]);
  19. $new_primarymac = str_replace($spr," ", $data[8]);
  20. $new_lastuser = str_replace($spr," ", $data[9]);
  21. $new_make = str_replace($spr," ", $data[10]);
  22. $new_model = str_replace($spr," ", $data[11]);
  23. $new_chassis_type = str_replace($spr," ", $data[12]);
  24. $new_cpu_info = str_replace($spr," ", $data[13]);
  25. $new_cpu_info2 = str_replace($spr," ", $data[14]);
  26. $new_hdd = str_replace($spr," ", $data[15]);
  27. $new_optical = str_replace($spr," ", $data[16]);
  28. $new_ram = str_replace($spr," ", $data[17]);
  29. $new_os = str_replace($spr," ", $data[18]);
  30. $new_sp = str_replace($spr," ", $data[19]);
  31. $new_iever = str_replace($spr," ", $data[20]);
  32. $new_av = str_replace($spr," ", $data[21]);
  33. $new_avver = str_replace($spr," ", $data[22]);
  34. $new_avdat = str_replace($spr," ", $data[23]);
  35. $new_monitor = str_replace($spr," ", $data[24]);
  36. $new_chipset = str_replace($spr," ", $data[25]);
  37. $new_audio = str_replace($spr," ", $data[26]);
  38. $new_cproxy = str_replace($spr," ", $data[27]);
  39. $new_lproxy = str_replace($spr," ", $data[28]);
  40.  
  41. $sqlone = "SELECT * FROM `aidahardware` where `hostname` = '$new_hostname' LIMIT 1";
  42. $result = mysql_query($sqlone) or die(mysql_error());
  43. $row = mysql_fetch_array($result);
  44.  
  45. foreach ($row as $key => $value) {
  46.         $$key = $value;
  47.         if ($new_$$key==$$key){echo "$new_$$key is equal to $$key\n";}
  48. }
  49. ?>



Thanks.

DaWei Nov 20th, 2007 9:14 AM

Re: Simple "foreach" problem
 
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.

davil Nov 20th, 2007 9:26 AM

Re: Simple "foreach" problem
 
Quote:

Originally Posted by DaWei (Post 137209)
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:

:

  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:
:

  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 Nov 20th, 2007 9:33 AM

Re: Simple "foreach" problem
 
one more thing.... I'm using curl from a command line like this to send the info to the server:

:

curl -0 http://localhost/it2/newinfo.php/$-$$2007-11-08$-$$09:23$-$$myhostname
$-$$myserial1$-$$myserial2$-$$myserial3$-$$192.168.50.50$-$$00-13-72-00-00-00
$-$$myusername$-$$Dell*Inc.$-$$OptiPlex*GX520$-$$Desktop*Case
$-$$Unknown;*2800*MHz*(3.5*x*800)$-$$Intel(R)*Pentium(R)*D*CPU*
$-$$C:*(NTFS);12001*MB*(724*MB*free);;D:*(NTFS);64228*MB*(2936*MB*free);;
$-$$BF7597N*DHU507V*SCSI*CdRom*Device;;TSSTcorp*CDRW/DVD*TSH492B;;
$-$$1014*MB$-$$Microsoft*Windows*XP*Professional$-$$Service*Pack*2
$-$$6.0.2900.2180$-$$McAfee*VirusScan*Enterprise$-$$8.0.0.912
$-$$2007-11-06$-$$Plug*and*Play*Monitor*%5BNoDB%5D**(C53854AL0HRL)
$-$$Unknown$-$$SoundMAX*Integrated*Digital*Audio*%5BNoDB%5D;;
$-$$10.100.1.14:80$-$$10.100.1.14:80


hence why I'm not using formatting....

DaWei Nov 20th, 2007 10:47 AM

Re: Simple "foreach" problem
 
I'm sure you want to do more than just display the information with whatever the keys might be, and as they change from time to time, but this key-adaptive approach works for me.

Note that the tables are just 2D arrays. The foreach ($table as $row; mimics the $row = mysql_fetch_array($result);

Click "Toggle Plain Text" to see the lines unwrapped.
:

  1. <?php
  2. $table1 = array (
  3.           array ("Field1"=>"Value11", "Field2"=>"Value12", "Field3"=>"Value13"),
  4.           array ("Field1"=>"Value21", "Field2"=>"Value22", "Field3"=>"Value23"),
  5.           array ("Field1"=>"Value31", "Field2"=>"Value32", "Field3"=>"Value33"));
  6. $table2 = array (
  7.           array ("NewName1"=>"NewValue11", "NewName2"=>"NewValue12", "NewName3"=>"NewValue13"),
  8.           array ("NewName1"=>"NewValue21", "NewName2"=>"NewValue22", "NewName3"=>"NewValue23"),
  9.           array ("NewName1"=>"NewValue31", "NewName2"=>"NewValue32", "NewName3"=>"NewValue33"));
  10.  
  11. function showTable ($table)
  12. {
  13.         $columnHeads = array_keys ($table[0]);
  14.         echo "<pre>";
  15.         foreach ($columnHeads as $head)
  16.                 echo $head."\t\t";
  17.         echo "<br>";
  18.         foreach ($columnHeads as $head)
  19.         {
  20.                 for ($i = 0; $i < strlen ($head); $i++)
  21.                         echo "-";
  22.                 echo "\t\t";
  23.         }
  24.         echo "<br>";
  25.         foreach ($table as $row)
  26.         {
  27.                 foreach ($columnHeads as $head) echo $row [$head]."\t\t";
  28.                 echo "<br>";
  29.         }
  30.         echo "<br>";
  31.         echo "</pre>";
  32. }
  33.  
  34. showTable ($table1);
  35. showTable ($table2);
  36.  
  37. ?>

Output:
:

Field1                Field2                Field3               
------                ------                ------               
Value11                Value12                Value13               
Value21                Value22                Value23               
Value31                Value32                Value33               

NewName1                NewName2                NewName3               
--------                --------                --------               
NewValue11                NewValue12                NewValue13               
NewValue21                NewValue22                NewValue23               
NewValue31                NewValue32                NewValue33


Sane Nov 20th, 2007 12:37 PM

Re: Simple "foreach" problem
 
I haven't put enough time into reading this, but it sounds like what you're trying to accomplish with the if statement could easily be put into the mysql query. That's what the mysql is there for after all.

davil Nov 21st, 2007 4:42 AM

Re: Simple "foreach" problem
 
Thanks dawei for your code it really helped me through a tight spot... I modified it a bit to suit my needs and here is the final code. I can't believe I never thought of using arrays like that.... I really have a lot to learn about program design... I'd love to go to college to study computer science but I can't afford it. ah well. maybe next year...

anyway here's the final code more or less.... I will be adding a lot of bells and whistles but it does what I need it for the minute so here goes:

:

  1. <pre>
  2. <?php
  3. //connect to my MySQL database etc.
  4. require("..\config.php");
  5. $repl="$-$$";
  6. $spr="*";
  7.  
  8. $data = explode($repl,$HTTP_SERVER_VARS['PATH_INFO']);
  9.  
  10. $new = array();
  11. $fields = array('','datestamp','timestamp','hostname','serial1','serial2','serial3','primaryip','primarymac','lastuser','make','model',
  12.                       'chassis_type','cpu_info','cpu_info2','hdd','optical','ram','os','sp','iever','av','avver','avdat','monitor','chipset','audio',
  13.                       'currproxy','lanproxy');
  14.  
  15. //  I want the program to only watch these fields for changes.. I will program that
  16. //  in later - I could have done all this with IF but I knew that this would change
  17. //  dynamically and I wanted the program to be more versatile.
  18. $watchedfields = array ('ram','primarymac','serial1','serial2','serial3');
  19.  
  20. //remove MB from amount of ram, i.e. 1024*MB becomes simply 1024
  21. $data[17] = str_replace('*MB','',$data[17]);
  22.  
  23. //remove periods from serial3
  24. $new['serial3'] = str_replace(".","", $new['serial3']);
  25.  
  26. $new['datestamp'] = $data[1];
  27. //leave this for now as it is another thing entirely that's not too important to what I'm doing currently
  28. //$new['datestamp']=str_replace($repl, "/", $new['datestamp']);
  29.  
  30.  
  31. for ($i=2;$i<count($fields);$i++)
  32.     $new[$fields[$i]] = str_replace($spr,' ',$data[$i]);
  33.  
  34. $sqlone = "SELECT * FROM `aidahardware` where `hostname` = '" . $new['hostname'] . "' LIMIT 1";
  35. $result = mysql_query($sqlone) or die("Problem with the query: $sqlone<br>" . mysql_error());
  36. $row = mysql_fetch_assoc($result);
  37.  
  38. foreach ($fields as $dmy => $fld) {
  39.     if ($new[$fld] != $row[$fld])
  40.             {echo $fld." has changed,old value: [" . $row[$fld] . "] new value: [" . $new[$fld]. "]\n";}
  41. }
  42.  
  43.  
  44. ?>



All times are GMT -5. The time now is 10:15 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC