![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2007
Posts: 26
Rep Power: 0
![]() |
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: php Syntax (Toggle Plain Text)
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? php Syntax (Toggle Plain Text)
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. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
Re: Simple "foreach" problem
What is the Error that you get? PHP has a great Error messages
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2007
Posts: 26
Rep Power: 0
![]() |
Re: Simple "foreach" problem
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 : php Syntax (Toggle Plain Text)
everything will become clear when I post entire code: php Syntax (Toggle Plain Text)
Thanks. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Nov 2007
Posts: 26
Rep Power: 0
![]() |
Re: Simple "foreach" problem
Quote:
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)
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)
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 |
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2007
Posts: 26
Rep Power: 0
![]() |
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.... |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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. PHP Syntax (Toggle Plain Text)
Field1 Field2 Field3 ------ ------ ------ Value11 Value12 Value13 Value21 Value22 Value23 Value31 Value32 Value33 NewName1 NewName2 NewName3 -------- -------- -------- NewValue11 NewValue12 NewValue13 NewValue21 NewValue22 NewValue23 NewValue31 NewValue32 NewValue33
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Nov 2007
Posts: 26
Rep Power: 0
![]() |
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: php Syntax (Toggle Plain Text)
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MySQl simple problem | paulchwd | Other Web Development Languages | 7 | Feb 27th, 2007 10:31 AM |
| Simple blackjack script problem. Need help. | jokr004 | C++ | 6 | Feb 10th, 2006 11:44 AM |
| EXTREMELY simple problem | d_heyzie | C++ | 15 | Feb 2nd, 2006 11:32 PM |
| Simple Perl / MySQL Problem.. pls help! | domquemo | Perl | 0 | Jan 11th, 2006 4:08 AM |
| repaint() problem in very simple program | dotred | Java | 1 | Mar 17th, 2005 5:17 AM |