SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Sydney
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy How to log data before and after editing text fields in a repeat region

    I have developed a simple cms website where I have data displayed in repeat region table displaying 6 text fields. I want to log the before and after data readings. The repeat region can show up to 400 rows of text fields. Would I use a sesson to parse the data.

    Points highlighting the main processes would point me in the right direction.

    Thanks in advance.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Can I just check I have understood your question?

    You have up to 2,400 values - you send them to the user, or some process, then you want to display those old and possibly changed values again.

    Meantime you want to log the before and after values or do you mean just the changes?

    Log them, semi permanently?

  3. #3
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Sydney
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have figured out the logging side of things and yes a possible 2400 text fields. My problem now is that it logs all fields whether edited or not. I need a script that checks the old data and compares it to the new data and processes the new data only. I've got some kind of OR statement happening but not working. If any of this makes sense... Only really need to check 4 fields. thanks.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So could this question be boiled down to this;

    Code:
    <form>
    
    a1 <input type=text id=field[a1] name=field[a1] value="thing1" /><br />
    a2 <input type=text id=field[a2] name=field[a2] value="thing2" /><br />
    a3 <input type=text id=field[a3] name=field[a3] value="thing3" /><br />
    
    b1 <input type=text id=field[b1] name=field[b1] value="another thing1" /><br />
    b2 <input type=text id=field[b3] name=field[b2] value="another thing2" /><br />
    b3 <input type=text id=field[b3] name=field[b3] value="another thing3" /><br />
    
    <input type=submit>
    </form>
    How do I detect the new value if a user changes just one entry?

    Is that oversimplifying too much?

    Given that there could be 2400 of these, have you considered capturing the change using Javascript?

    Are you getting all the field names from a database?

  5. #5
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Sydney
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am sending new and old data to my processor script and it updates or inserts into an auditing table after checking whether it's unique ID exists or not - this works. Except, I only want it to store changed data in this audit table so we can see the before and after readings, it currently puts everything in. I would know where to start with Javascript..
    I have a conditional statement like this -
    [php]
    if ((!($oldlab==$nlabour)) || (!($old_paint1==$nlayer1)) || (!($old_paint2==$nlayer2)) || (!($old_paint3==$nlayer3))) { do insert or update } ...
    [php]

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That conditional statement seems very brittle, do you maintain in by hand?

    Perhaps if you boiled down your problem to a micro application, with say just 3 values, and you showed us

    a) exactly how your form elements are displayed (dont include font tags, tables or any other stuff like <head> and <body> tags, just a minimum form.

    b) how you are currently handling those 3 elements in your postback form.

    c) the table schema for those 3 elements, doesn't have to be complex.

    That way we can talk about code.

  7. #7
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Sydney
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My form is working OK, but it looks something like this
    <form name="frm1" method="post" action="update.php">
    <input name="newlabor" type="text" value="[php] echo $oldlabor; [php]" />
    <input name="oldlabor" type="hidden" value="[php] echo $oldlabor; [php]" />
    <input name="newlapaint" type="text" value="[php] echo $oldapaint; [php]" />
    <input name="oldapaint" type="hidden" value="[php] echo $oldapaint; [php]" />
    <input name="newbpaint" type="text" value="[php] echo $oldbpaint; [php]" />
    <input name="oldbpaint" type="hidden" value="[php] echo $oldbpaint; [php]" />
    <input name="update" type="submit" value="Update" />
    </ form>
    I want the processor script to compare newlabor against oldlabor from the original data source, if it is different i.e. must have been edited then insert both values into the audit table. I can do this for one data set comparison i.e. labor. But I want it to check all text fields for any change, if there is change then update/insert into audit table else nothing i.e. no change. This update/insert I have got working - it's the bit in the middle i.e. determining if there has been any change amongst the data row values... hope this helps fine tune my dilemma. At the moment it updates/inserts all the data from the original data source (new and old values) into my audit table - as you can imagine fills it up pretty fast with a lot of useless data....

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    OK, I think I see where you are at now.

    Regardless of whether using hidden form elements is the right way to go, which was I think the thrust of your original question, this is how you could go about just identifying the changed values.

    This is your form:

    PHP Code:
    <input name="newapaint" type="text" value="<?php  echo $oldapaint?>" />
    <input name="oldapaint" type="hidden" value="<?php echo $oldapaint?>" />
    <input name="newbpaint" type="text" value="<?php  echo $oldbpaint?>" />
    <input name="oldbpaint" type="hidden" value="<?php echo $oldbpaint?>" />
    update.php
    PHP Code:
    if( $_POST['oldapaint'] !== $_POST['oldapaint'] ) 
     
    $updates['apaint'] = $_POST['newapaint'] ;

    if( 
    $_POST['boldpaint'] !== $_POST['oldbpaint'] ) 
     
    $updates['bpaint'] = $_POST['newbpaint'] ; 
    etc

    So you create an array of updates which would look like this:

    $updates['apaint'] = "red" ;
    $updates['bpaint'] = "yellow" ;

    And then make a sql query generator

    Which creates the equivalent sql statement to this:

    "insert into yourtable ( apaint, bpaint) values ( 'red', 'yellow' )" ;

    If you could make an sql query generator then is that the kind of query you are after?

    // TODO
    1 Make a proof of concept query generator with hard-coded variable names, as above
    2 Somehow tee-up your database column names with your form element names so that you can just get PHP to automatically go through your $_POST array and make that create the sql statement on the fly without all this hand coding of variable names.

    and then...

    3 Maybe figure out other ways of doing it
    4 Identify which is best

    Do you want to go that way?

    ps to highlight PHP code on here wrap the code in these bbtags [ php ] [ /php ] <- without the spaces

  9. #9
    SitePoint Member
    Join Date
    Oct 2009
    Location
    Sydney
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups
    I sort of have what you're saying already in place and it's updating/inserting all the array variables but I want to take it one step further. I want it to look at the post variables and the hidden post variables (original and new values) coming in and determine whether there has been any change (i.e. look at oldapaint and newapaint if they are the same value (integer values) do nothing, if different insert or update). I can do this for a one variable comparison, no worries.

    But to add to the complication I want it to look at (say) 4 variables and compare them against their original values, if there is change in any one value of the 4 do an update/insert if nothing has changed don't do anything with those values (in that row, go to the next row and compare). That's why I was trying to use the OR in my statement above. Maybe I need some kind of AND OR script? Hope this helps paint a clearer picture.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Imagine you have 3 form fields, apaint, bpaint, cpaint

    So you detect which ones have actually changed by doing this:

    PHP Code:
    if( $_POST['oldapaint'] !== $_POST['oldapaint'] ) 
     
    $updates['apaint'] = $_POST['newapaint'] ;

    if( 
    $_POST['oldbpaint'] !== $_POST['oldbpaint'] ) 
     
    $updates['bpaint'] = $_POST['newbpaint'] ; 

    if( 
    $_POST['oldcpaint'] !== $_POST['oldcpaint'] ) 
     
    $updates['cpaint'] = $_POST['newcpaint'] ; 
    So now $updates either has nothing in it , because nothing changed, or it has some values in it - and only the values that have altered.

    PHP Code:
    if ( isset( $updates ) ){

    // go on and do the inserts into your database 

    So then you can do this to create your update sql.

    PHP Code:

    // here we imagine that $updates only contains 2 values out of the 3

    $updates = array( 
    'apaint' => 12
    ,'bpaint' => 7
    );

    $sql "insert into mytable values (" ;

    $fields array_keys$updates) ;

    $sql .= implode("," ,$fields ) ;

    $sql .= ") values (" ;

    $sql .= implode("," $updates );

    $sql .= ");" ;

    echo 
    $sql 
    prints: insert into mytable values (apaint,bpaint) values (12,7);

    The last bit of code, the sql generator, will work if you have between 1 - x,000 values in $updates, and will only work if the values are indeed integers.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •