PHP Loop / Edit Checkbox problem

Discussion in 'PHP' started by koolsamule, Jun 7, 2010.

  1. #1
    Hi Chaps,

    Outline:
    I have a table of Jobs and a table of Tasks. The Tasks are linked to the Jobs via FOREIGN KEY and is working fine.
    There can be say 10 Tasks linked to a Job, in this example, there are 4 already linked.
    I have a Query that selects the FK_task_id field from a table:
    rsJob:
    SELECT FK_task_id FROM tbl_job LEFT OUTER JOIN tbl_task_item ON tbl_task_item.FK_job_id=tbl_job.job_id;
    PHP:
    Producing:
    The second Query lists all the Tasks:
    rsTask:
    SELECT task_id, task_title FROM tbl_task;
    PHP:
    Producing:
    What I'm trying to do is that lists the total 10 Tasks, and check the checkboxes that are already linked.
    The PHP code I have at the moment:
    <?php do { ?>
           <?php echo $row_rsTask['task_title']; ?> - <input type="checkbox" name="task[<?php echo $row_rsTask['task_id']; ?>]" value="<?php echo $row_rsTask['task_id']; ?>"  <?php if (!(strcmp(htmlentities($row_rsJob['FK_task_id'], ENT_COMPAT, 'utf-8'),htmlentities($row_rsTask['task_id'], ENT_COMPAT, 'utf-8')))) {echo "checked=\"checked\"";} ?> /></br>
            <?php } while ($row_rsTask = mysql_fetch_assoc($rsTask)); ?>
    PHP:
    The problem is that only the first record from rsJob is checked (1 - Prep), where 1 - Prep, 2 - Trans, 4 - TypeF and 10 - Ship should all be ticked.
    Has anyone got any ideas on how to display all 10 Tasks and also get all 4 checkboxes to check in this Loop?
     
    koolsamule, Jun 7, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    Here is my code suggestion. There are loads of different ways to do it.
    Code is not tested so errors might show up.

    
    <?php
    
    
        $sql = 'SELECT `FK_task_id` FROM `tbl_job` LEFT OUTER JOIN `tbl_task_item` ON `tbl_task_item.FK_job_id`=`tbl_job.job_id`';
        $query = mysql_query($sql);
        
        $done = array();
        
        while ($result = mysql_fetch_assoc($query)) {
            
            array_push($done,$result['FK_task_id']);    
            
        }
        
            
        $sql2 = 'SELECT `task_id`, `task_title` FROM `tbl_task` ORDER BY `task_id` ASC';
        $query2 = mysql_query($sql2);
    
        while ($result2 = mysql_fetch_assoc($query2)) {
        
            echo '<p>'.$result2['task_title'].' - <input type="checkbox" name="task['.$result2['task_id'].']" value="task['.$result2['task_id'].']" ';
            if (array_search($result['task_id'],$done)) {
                echo ' checked="checked"';
            }
            echo ' /> </p>';
        
        }
    
    
    ?>
    
    
    
    PHP:
     
    stephan2307, Jun 7, 2010 IP
  3. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi dude, thanks for the code,
    I changed
    if (array_search($result2['task_id'],$done)) {
                echo ' checked="checked"';
            }
    PHP:
    which nearly does the trick. It checked tasks 2,4,10, but not 1 ! Don't know why this is as I echoed FK_task_id in $done and it showed 1,2,4,10 ?????
     
    koolsamule, Jun 7, 2010 IP
  4. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ahh, changed array_search to in_array and that seems to have done the trick. Cheers for the code!
     
    koolsamule, Jun 7, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    no problem.
     
    stephan2307, Jun 7, 2010 IP