0

I want to create a table for documents and allow users to select whether the document is required or not in relation to a task. I need it to check the current status of the document to see if it is already required and if it is mark the tick box as checked. There are currently 4 documents in the database and only 2 associations to 2 of the documents for this particular task.

When there is no association of a document to a task there will be no entry in the database to say that it is not associated it just simple will not be in that table.

The code I have currently will show checkboxes against those 2 documents which have associations however do not show any tick boxes at all for the ones that have no association. I would like it to still show the 3 check boxes, so that the user can change its association, but as default it should check not required. Currently It just shows no tick boxes at all. Below is the PHP snip. Please advise if you would like a screenshot or the DB dumps.

Any help would be greatly appreciated. I'm hoping its just been a long day and I am forgetting something simple.

    <table border=1>
      <tr>
        <td align=center><p>Here you can associate documents to Jobs and Tasks to.</p> </td>
      </tr>
    </table>';



$order2 = "SELECT * FROM documents";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {

$nice_name = $row2['nice_name'];
$doc_id = $row2['id'];

}

echo '
<h3>Documents</h3>

    <table border=1>
      <tr><th>Document Name</th><th>Document Type</th><th>Required</th><th>Optional</th><th>Not Required</th></tr>
    ';



$order2 = "SELECT * FROM documents ORDER BY type_id";
$result2 = mysql_query($order2);
while ($row2 = mysql_fetch_array($result2)) {

$nice_name = $row2['nice_name'];
$doc_id = $row2['id'];
$doc_type_id = $row2['type_id'];

echo '

      <tr>
        <td>'.$nice_name.'</td>';

$order3 = "SELECT * FROM document_types WHERE id = '$doc_type_id'";
$result3 = mysql_query($order3);
while ($row3 = mysql_fetch_array($result3)) {

$type_name = $row3['type_name'];

    echo '
        <td>'.$type_name.'</td>';

$order4 = "SELECT * FROM document_assoc WHERE doc_id = '$doc_id'";
$result4 = mysql_query($order4);
while ($row4 = mysql_fetch_array($result4)) {

$requirement = $row4['requirement'];

    echo '

        <td><input type="checkbox" name="req" value="2" '; if ($requirement == 2) { echo ' checked '; } echo '></td>
        <td><input type="checkbox" name="opt" value="1" '; if ($requirement == 1) { echo ' checked '; } echo '></td>
        <td><input type="checkbox" name="not" value="0" '; if ($requirement < 1) { echo ' checked '; } echo '></td>';
    }

    }

    echo '
      </tr>';
    }
    echo '
    </table>';

So far I have just tried tweaking the requirement = with the final tick box. But as it is staying the same whether i use NULL ect - im guessing im missing something.

Kind Regards,

n00bstacker

3

1 Answer 1

1

You can use the following if statement to identify the situation and respond appropriately:

            $order4 = "SELECT * FROM document_assoc WHERE doc_id = '$doc_id'";
                $result4 = mysql_query($order4);
                $_results = mysql_fetch_array($result4);
                if (!$_results) {
                       <<<< OUTPUT THE CHECKBOXES/INPUTS YOU NEED >>>>
                } else {
                   foreach ($_results as $result) {

                $requirement = $result['requirement'];

echo '

    <td><input type="checkbox" name="req" value="2" '; if ($requirement == 2) { echo ' checked '; } echo '></td>
    <td><input type="checkbox" name="opt" value="1" '; if ($requirement == 1) { echo ' checked '; } echo '></td>
    <td><input type="checkbox" name="not" value="0" '; if ($requirement < 1) { echo ' checked '; } echo '></td>';
}

}

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alberto! Your answer got me there. Just for completeness your answer had 2 errors on it - "row4" on the $requirement line and the } closing the foreach needed to be above the last echo in your answer. Many Many Thanks!!
All I did was copy & pasted your work and added in a few lines...you may want to correct it above in your example, as well...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.