Welcome Guest, Not a member yet? Register   Sign In
Help getting info from database
#1

[eluser]Unknown[/eluser]
Hello, I've been having trouble getting information to the view from a database.
This is the situation:

There are a total of three databases. The first is a database for a student that records all their personal information. The second is a database for classes the student can register for. This database contains all the information on that particular class. The third database is used to associate a student with a particular class. It takes the student id and the class id and puts it into a 'studentid' and 'classid' field.

I can fetch the studentid without a problem, I can then use that to find which classes in the associated database are registered to that student. The problem occurs when I try to get each classid pass it to a query that finds the id in the class database and then passes that information to the view.

Typically I can only get one result to show up.
Do I have to add each to an array? I'm not sure how to get it to work.

I'm new to php and especially the MVC model, so please, any help would be appreciated.

Thank you.

Controller
Code:
function reviewclasses()
    {
        $userid = $this->session->userdata('userid');
        $query = $this->db->query("SELECT classid FROM tmp_student_classes WHERE studentid='$userid'");
            foreach($query->result() as $rows)
            {
                $classid = $rows->classid;
                $data['query'] = $this->db->query("SELECT * FROM classes WHERE id='$classid'");    
            }
        //$data['query'] = $this->db->query("SELECT * FROM classes WHERE id='$classid'");    
        $this->load->view('reviewclasses_view', $data);
    }

View
Code:
<html>
<head>
    <link rel="stylesheet" href="<?=base_url()?>styles.css" type="text/css" charset="utf-8">
    <title>Test Project</title>
</head>
<body>
<div id="main">
    <div id="middle">
        
        <h1>: Review Classes</h1>
        &lt;?php if ($query->num_rows()==0)
            echo('You are not enrolled in any classes at the moment.');
        else
            foreach($query->result() as $row):  ?&gt;
                <dl>
                <div style="float: left; margin-right:10px;"><dt>Class:</dt><dd>&lt;?=$row->name?&gt;</dd></div>
                <div style="float: left; margin-right:10px;"><dt>Min Seats:</dt><dd>&lt;?=$row->minseat?&gt;</dd> </div>
                <div style="float: left; margin-right:10px;"><dt>Max Seats:</dt><dd>&lt;?=$row->maxseat?&gt;</dd></div>
                <div style="float: left; margin-right:10px;"><dt>Price:</dt><dd>$&lt;?=$row->price?&gt;</dd></div>
                <div style="float: left; margin-right:10px;"><dt>Min Deposit:</dt><dd>$&lt;?=$row->mindep?&gt;</dd></div>
                <div style="float: left; margin-right:10px;"><dt>Register By:</dt><dd>&lt;?=$row->rgbym?&gt;/&lt;?=$row->rgbyd?&gt;/&lt;?=$row->rgbyy?&gt;</dd></div>
                </dl>
                <br/><br/><br/>
            &lt;?php endforeach;?&gt;

    
    </div>
</div>
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Herb[/eluser]
Let me see if this is right?

You want to get a list of classes enrolled in then list the data on the class.

In your controller you are looping over a query that extracts one class at a time and overwrites $data['query'] on each iteration. Therefore, you only pass the result of the last query.

Try building a multi-dimentional array: $data[$classid]['query_result_field']. Then do nested foreach in the view to extract the data.




Theme © iAndrew 2016 - Forum software by © MyBB