1

I have two tables like below:-

1. document_master                    
------------------                             
document_id   |  document_name      
------------------                             
1             | BirthCertifcate                 
2             | AadharCard      


2. student_document_detail                  
------------------                             
student_document_id |  student_id  | document_id | file_name | IsActive
------------------                             
1                   |     55       | 1           | 55_b.jpg    |    1  
2                   |     55       | 2           | 55_a.jpg    |    1
3                   |     70       | 2           |             |    0
4                   |     70       | 2           | 56_b.jpg    |    1

I have a form like below:- enter image description here

here is my function to create form:-

public function get_student_document_details(){
    if (!$this->input->is_ajax_request()) {
        exit('No direct script access allowed');
    }


    //here i want to check if document already in table or not on this basis make status checkbox checked or unchecked
    $student_document_records = $this->mdl_student_document_upload->get_student_document_list($student_id);

    //get the document master list
    $records = $this->mdl_student_document_upload->get_document_master_list();
    $document_master_list = '';
    $sr_no = 1;
    foreach($records as $row){
        $document_master_list .='<tr>
                                    <td>'.$sr_no.'</td>
                                    <td width="100"><span style="margin-top:0;" class="btn btn-default btn-file btn-xs">
                                                    Upload <input name="student_document" id="student_document" type="file">
                                                </span></td>
                                    <td width="100"><button  class="btn btn-xs btn-danger">Remove</button></td>
                                    <td>
                                    <input type="hidden" name="document_name" id="document_name" value="'.$row->DocumentName.'"/>
                                    <input type="hidden" name="document_master_id" id="document_master_id" value="'.$row->DocumentMasterID.'"/>
                                    '.$row->DocumentName.'
                                    </td>
                                    <td><input type="checkbox" name="status" id="status"/></td>
                                </tr>';
        $sr_no++;
    }
    die(json_encode($document_master_list));
}

My questions are:-

  1. if document exists in student_document_detail table then make status checkbox checked.
  2. if not or IsActive field 0 then make checkbox unchecked.

  3. How to generate document upload form based on these conditions. Any help appreciated!. Thanks in advance.

5 Answers 5

2

Try

if($row->IsActive==1)
{
    echo '<td><input type="checkbox" checked="checked" name="status" id="status"/></td>';
}
else
{
    echo '<td><input type="checkbox" name="status" id="status"/></td>;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hassaan can i check this condition in foreach loop which one I have used for get document name details alongwith other input field like file to upload document because my form is generated from document_master table
@RammeharSharma yes you can.
for this I need to create another query for student_document_detail in foreach loop is it good if I have 100000 records in this table because query performed with (student_id and document_id ) these fields are unique
@RammeharSharma It is simple if/else condition. This does not matter how many records you want to show. But it is recommended to using paging instead of displaying all 100K records on one page.
1

Do something like this.

<input type="checkbox" name="status" id="status" checked="<?php if($row->IsActive == 1){<text>true</text>}else{<text>false</text>}  ?>"/>

Comments

1

Try This

<td><input type="checkbox" <?php if($row->IsActive==1) { echo "checked" } ?> name="status" id="status"/></td>

Comments

1

you can try this one to check weather it is active or not

<td><input type="checkbox" <?php echo $row->IsActive==1) ? "checked": '' ;?> name="status" id="status"/></td>

note

I have used short if else . you can know more about this if not familiar if else short

Comments

-1

Try This May it help you

if($row->IsActive==1) {
  <td><input type="checkbox" checked="checked" name="status" id="status"/></td>
 }
else{
  <td><input type="checkbox" name="status" id="status"/></td>
}

2 Comments

You need to choose either single or double quotes and be consistent.
@Script47 XHTML requires checked='checked'

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.