Welcome Guest, Not a member yet? Register   Sign In
If data exists in the field print, else don't
#1

[eluser]zebake[/eluser]
Hello, new to CI and have limited php experience. I am attempting to write a student directory database application for my son's school. I have watched the tutorials and read up on the user guide. I have setup a mysql database and thanks to the video tutorials, I am able to retrieve and display the information fine. The problem I have run into is the occasional situation where a child may have two separate households (divorced parents). In this event, I need to print the information for both households. I have setup the database with the extra / alternate fields and populated them with info. How do I print the alternate contact info only if it exists, and not print anything otherwise?

This is my model:
Code:
<?php
class Student_directory_model extends Model {

    function Student_directory_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function getData()
        {
            //Query the data table for every record and row
            $query = $this->db->get('students');
            
            if ($query->num_rows() == 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }
}
?>

This is my view:
Code:
<html>  
<head>  
    <title><?=$page_title?></title>  
</head>  
<body>  
    <h1>&lt;?=$page_title?&gt;</h1>
    <ul>
        &lt;?php foreach($result as $row):?&gt;  
            <li>
                <ul>
                    <li>&lt;?=$row->student_lname?&gt;</li>
                    <li>&lt;?=$row->student_fname?&gt;</li>
                    <li>&lt;?=$row->phone?&gt;</li>
                    <li>&lt;?=$row->parents?&gt;</li>
                    <li>&lt;?=$row->street?&gt;</li>
                    <li>&lt;?=$row->city?&gt;</li>
                    <li>&lt;?=$row->state?&gt;</li>
                    <li>&lt;?=$row->zip?&gt;</li>
                    <li>&lt;?=$row->email1?&gt;</li>
                    <li>&lt;?=$row->cell?&gt;</li>
                </ul>
            </li>
            &lt;!-- li BELOW TO BE SHOWN ONLY WHEN NEEDED --&gt;
            <li>
                <ul>
                    <li>&lt;?=$row->parents_alt?&gt;</li>
                    <li>&lt;?=$row->phone_alt?&gt;</li>
                    <li>&lt;?=$row->street_alt?&gt;</li>
                    <li>&lt;?=$row->city_alt?&gt;</li>
                    <li>&lt;?=$row->state_alt?&gt;</li>
                    <li>&lt;?=$row->zip_alt?&gt;</li>
                    <li>&lt;?=$row->email1_alt?&gt;</li>
                    <li>&lt;?=$row->cell_alt?&gt;</li>
                </ul>
            </li>
        &lt;?php endforeach;?&gt;
    </ul>
&lt;/body&gt;  
&lt;/html&gt;

And this is my controller:
Code:
&lt;?php
    class Student_directory extends Controller{
        function Student_directory()
        {
            parent::Controller();
            
            $this->load->scaffolding('students');
       }
        
        function index()
        {
            $this->load->model('student_directory_model');

            $data['result'] = $this->student_directory_model->getData();
            $data['page_title'] = "Student Directory";

            $this->load->view('student_directory_view',$data);
        }
    }
?&gt;

Thanks in advance for any direction or assistance you might can provide!


Messages In This Thread
If data exists in the field print, else don't - by El Forum - 07-01-2009, 05:00 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 05:28 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 06:15 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 06:24 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 06:36 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 06:52 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 09:00 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 09:12 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 09:13 PM
If data exists in the field print, else don't - by El Forum - 07-01-2009, 09:50 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 03:20 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 03:29 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 03:40 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 05:51 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 08:02 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 08:16 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 08:22 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 08:31 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 08:34 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 09:16 AM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 01:07 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 01:22 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 01:23 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 03:29 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 03:37 PM
If data exists in the field print, else don't - by El Forum - 07-02-2009, 05:09 PM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 12:44 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 12:55 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 03:58 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 08:01 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 08:16 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 09:00 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 09:33 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 09:49 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 10:01 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 10:08 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 11:01 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 11:08 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 11:38 AM
If data exists in the field print, else don't - by El Forum - 07-03-2009, 11:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB