![]() |
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: :
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? :
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. |
Re: Simple "foreach" problem
What is the Error that you get? PHP has a great Error messages
|
Re: Simple "foreach" problem
Quote:
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 : :
everything will become clear when I post entire code: :
Thanks. |
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.
|
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: :
but I found out that if you have many fields to assign to variables you can use this code instead: :
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 |
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$-$$myhostnamehence why I'm not using formatting.... |
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. :
:
Field1 Field2 Field3 |
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.
|
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: :
|
| 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