Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Background:
Okay, here is an interesting problem that has been causing me to pull my hair out for the better part of 2 days. =)
I have a PHP page, of dynamically populated form fields basically, that displays things for different items.
For example, each row on this page displays make, model, color, etc. for a car. It pulls this information from a database and displays the information for all the cars at once.
The information is displayed as populated form fields so that the info can be changed for any or all of the cars.
I used to have a checkbox for each car, so that if you wanted to update the car in the database, you would check the checkbox. Then only the checked cars would be updated. This is obviously a pain in the a** if you need to update a bunch of them at once. You just want to change whatever information you need to change and update the whole thing.
Problem:What I want to be able to do is determine what cars have been updated, and place a timestamp (i.e. last updated on xx/xx at xx:xx am or pm using the current time). I have the time/date stuff down just fine, but I can't seem to apply it to only the cars that have been updated. When I update one, it wants to change the Last Updated values for all the cars.
Any help would be GREATLY appreciated!
Thanks,
-=Bryan=-

I can think of three options - neither one particularly pretty.
You could do it with javascript - detect a change in any of the fields and change the value of a hidden field that acts like the old checkbox. It's javascript, so it won't always be enabled. Make sure you don't just set the hidden field on change (what if the user changes it, then decides to change it back?), set it on form submit where you can compare each textbox's default value against its value. This gets even uglier with other kinds of inputs - textareas, selects, checkboxes, etc. You may have to have more hidden inputs indicating the original values of those fields.
Option two is to do it in PHP - Before the update, grab all the old values, compare them against the POST data, and update only the ones that have changed. This one is not particularly pretty because of all the server-side overhead.
Option three is a combination of one and two - Send out hidden input fields for each writeable field that has the writeable field's original value, then in the PHP page compare original values to new values and update accordingly. This would be faster than two (no database query) and more reliable than three.
Both one and three would run into timing issues if more than one person is using the system at a time.
-SN

Hi SN, and thanks for the great help as always.
At least for the time being, I'm not overly concerned with the server overhead or multiple people using the site at once, as it's very small scale right now.
Option 3 (the combo) sounds very interesting. I might look into that.
I actually tried a version of Option 2, but couldn't get it to work. It just updated everything, and I'm not sure why.
All the updated form data is being put into an array and then updated that way. So what I did was read the original data from the database into an array as well, then compared the data in the two arrays to see what was updated. But it didn't work.
Bizarre, I'll look into a little more and let you know if it works out.
Thanks again!!!!
Bryan

/snip
So what I did was read the original data from the database into an array as well, then compared the data in the two arrays to see what was updated
/snipyou sure you separate the cars correctly? like:
$car['ford1']['color'] = 'red';
$car['ford1']['make'] = 'Ford';
$car['honda1']['color'] = 'blue';
and so on..do you use session? if so, make $_SESSION['car'] = $car;
then I think it'll be easier to compare with loops
on the form's target page:
populate a new array based on the submitted values something like:
$carNew['ford1']['color'] = $_POST['fordcolor'];
so that the new array is similar with the 1st array, only the array name is different.
and so on...now you loop, something like:
// create a container for changed cars
$changed = array();foreach ($_SESSION['car'] as $key => $value){
// this is the comparation
if ($_SESSION['car'][$key] != $carNew[$key]){// record the car name
$changed[] = $key;}
}
now $changed is an array of changed carnames...
did that help?

SN, thank you again for the help. I stuck with Option 2 and it works great now!
Laler, thank you much for the help too. Your info did help. I'm not sure what the heck I was doing the past couple days, but the array for the existing info in the database I guess is the thing that wasn't working properly. I re-coded it today and it's working great now.
Again, thank you for the help gentlemen. It's indeed appreciated!
Bryan

![]() |
Gif alpha transparency
|
Javascript to save last p...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |