Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Arrow Dynamic Row Updating

    Bonjour,

    Been hunting the internet to find a solution myself, but instead thought I'd just ask on here. After all, if you don't ask, you don't get.

    Basically, I am creating a internal web based e-mail generator for the company I work for, that will send an e-mail to 'handover' certain 'tasks' to the next team on the next shift - It will be used internal only, so please excuse some of the sloppy coding but we only need it to 'work' - Doesn't really matter how!

    Anyway, the when someone goes through the form, they are given the options to add 'tasks' to the e-mail, these 'tasks' gets stored into a MySQL database, with the 'DONE' value of '0', ready for when the next person goes to generate the 'handover' - When the next person goes to generate the 'handover', they get a list of the 'tasks', that were previously inserted, by the previous person. Like so:



    They are given the option to mark the task as done, or not.

    Now here's the part where I get stuck...

    The subsequent 'handover' queries the MySQL database to see which 'tasks', have a 'DONE' value of '0' (not done). It then lists them. Here is the code:

    Code:
    $toq2= mysql_query("SELECT * FROM tasks WHERE done='0'");
    $toq2p = mysql_fetch_array( $toq2 );
    $toq2r = mysql_num_rows( $toq2 );
    if ($toq2r >= 1)
    {
    echo "<table border='0'><tr><td></td><td></td><td></td><td><img src='/assets/tick.png' width='20px' height='20px' border='0' /></td>";
    $toq4= mysql_query("SELECT * FROM tasks WHERE done='0'");
    while($toq4p = mysql_fetch_array( $toq4 )) {
    echo "<font color='black' size='2'><tr><td><font size='2'><strong>{$toq4p['system']}</strong></font> - </td><td><font size='2'><em>{$toq4p['region']}</em></font> - </td><td><font size='2'>{$toq4p['description']}</font></td><td><input type='checkbox' name='todo1[]' value='1'></td></font></tr>"; }
    echo "</table><br />";
    }
    else
    {
    echo "<h3><font color='red'>NOTHING TO HANDOVER</font></h3>";
    }
    Problem I have, is that because the amount of tasks is never consistent, I can't have a set query for the amount of values, therefore I need to find a way to dynamically set each row to update the 'done' value in the MySQL table to '1' if the box is ticked and leave it as '0', if the box isn't ticked.

    I hope I've explained myself well enough and appreciate any help that anyone can provide.

    Many thanks,

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are these tasks created as needed, or are they regular tasks scheduled day by day?

  • #3
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Are these tasks created as needed, or are they regular tasks scheduled day by day?
    As needed. Like so:



    When inserted from here, the value of 'done' is set to '0' - From the task list (picture in original post), if the person ticks the checkbox, I want it to be able to update that value of 'done' to '1'.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    So not in a schedule then?
    You already have the form almost correct. Using a checkbox name that's an array will allow you to retrieve the checked items out of it. So you have it named todo1[]. The value is incorrect though, make that whatever the primary key is for the record.
    When you submit it, you can simply implode that and put it into an UPDATE. What you do exactly with that will depend on what the datatype of the primary key is.

  • Users who have thanked Fou-Lu for this post:

    lewishowe (11-12-2012)

  • #5
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    So not in a schedule then?
    You already have the form almost correct. Using a checkbox name that's an array will allow you to retrieve the checked items out of it. So you have it named todo1[]. The value is incorrect though, make that whatever the primary key is for the record.
    When you submit it, you can simply implode that and put it into an UPDATE. What you do exactly with that will depend on what the datatype of the primary key is.
    I do understand what you're saying, and please excuse my ignorance, but I don't do a lot of coding, this is the first bit of coding I've done in a while! - Could you possibly demonstrate basically how I would go about doing what you've said?

    I do have a rough idea (!), but it would take me a while to 'perfect' it from scratch, so if you could provide a 'base' to what I need to do, that would be great.

    Massive thanks for all your help so far.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sure. I don't know what your task primary key is, so I'll just make one up that I'll call taskid. If its composite, this can still be done, but you'll need to pass both to the value (typically done easiest with serialization).
    PHP Code:
    print '<form method="post" action="">';
    $toqmysql_query("SELECT * FROM tasks WHERE done=0");
    while (
    $row mysql_fetch_assoc($toq))
    {
        
    printf('<label for="%d"><span class="labelsystem">%s</span><span class="labelregion">%s</span><span class="labeldescription">%s</span></label>' PHP_EOL$row['taskid'], $row['system'], $row['region'], $row['description']);
        
    printf('<input id="%d" type="checkbox" name="todo[]" value="%d" />' PHP_EOL$row['taskid'], $row['taskid']);
    }
    print 
    '</form>'
    Which should give you similar to what you have in the first screenshot. You can use CSS for the markup of that instead; I removed the - from them as I don't like them. You should be able to use the CSS pseudo classes :before and :after to add them if you want (won't work with IE < 6.0 of course).

    Then when you process them, you simply retrieve from the todo array:
    PHP Code:
    if (isset($_POST['todo']) && is_array($_POST['todo']))
    {
        
    // This is where we write validation and verification.  I just assume a flat integer
        
    function isIntString($in)
        {
            
    $bResult false;
            
    $int = (int)$in;
            if ((string)
    $int == $in)
            {
                
    $bResult true;
            }
            return 
    $bResult;
        }

        
    $aTodo array_filter($_POST['todo'], 'isIntString');
        
        if (!empty(
    $aTodo))
        {
            
    // here we would handle SQL if it needs to be escaped (integers do not).
            
    $sQry 'UPDATE tasks SET done = 1 WHERE taskid IN (' implode(', '$aTodo) . ')';
            
    // and go from here to update
        
    }

    Like that.

  • Users who have thanked Fou-Lu for this post:

    lewishowe (11-14-2012)

  • #7
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, so it's all implemented - Good guess at the primary key, it was indeed 'taskid' - I've not changed any of the core parts of the code, apart from changes to table names in the queries etc..

    Trying not to sound too stupid here - But does this below code have the ability to work 'out-of-the-box'? - If so, I've tried it and it doesn't work..

    I may be missing something, or merely I need to add some things in myself.

    Once again, excuse my lack of knowledge - As I said, this is the first piece of coding I've done in a while, and even so, I've never come across such functions in PHP.

    Any further help would be greatly appreciated, and I am very appreciative of all your help so far.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, it won't work by itself. I didn't write in anything to do with the SQL execution, just the string it would use. Your original post indicates that you have used the query functions for mysql in the past, so simply wrap that $sQry in a mysql_query call. You may want to print it out first to verify that it is indeed correct.

  • #9
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No, it won't work by itself. I didn't write in anything to do with the SQL execution, just the string it would use. Your original post indicates that you have used the query functions for mysql in the past, so simply wrap that $sQry in a mysql_query call. You may want to print it out first to verify that it is indeed correct.
    Feel a little silly now for not noticing it was in a string.. (duh!) - Thanks for that, I shall give it a try!

  • #10
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, wrapped the query:

    PHP Code:
    mysql_query("UPDATE othertodo SET done = 1 WHERE taskid IN (' . implode(', ', $aTodo) . ')"); 
    Query seems to run fine - No errors - Everything else works (queries above and below it). But it doesn't update the MySQL at all..

    It's as if the criteria isn't being met...

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Then it likely isn't.
    Pull that query back out into a variable and print it. It should show you what it is providing to the taskid in the IN clause. I also assumed it was an integer, but that may not be the case.

  • #12
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Okay, for example - If I tick 2 boxes out of the 3 outstanding, like so:



    It generates this as the query.

    Code:
    UPDATE othertodo SET done = 1 WHERE taskid IN (1, 3)
    The 1 and 3, corresponding to the taskid of the boxes that I ticked.
    Last edited by lewishowe; 11-16-2012 at 09:04 AM.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Then that should be fine, assuming that taskid is an actual integer and not a string, and done is an integer and not a string.
    Use a die on the query to verify your columns mysql_query($sQry) or die(mysql_error());. That'll kill the script with an error output if it fails to parse the SQL properly. Given the previous queries you have here, my suspicion is that the table othertodo may not exist.

  • Users who have thanked Fou-Lu for this post:

    lewishowe (11-16-2012)

  • #14
    New Coder
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Bizarrely, adding the die mysql error, fixed it. Not sure what difference this should've made in the first place, nonetheless, it's fixed.

    Thanks for all your help!

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Or die won't fix it. Its only purpose is to toss an error if something doesn't work and fail the script completely instead of continuing.
    If it works after the modification, this indicates to me that you were either running an old script if you forgot to upload it, or that it was cached to an old script.


  •  

    Tags for this Thread

    Posting Permissions

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