Welcome Guest, Not a member yet? Register   Sign In
Does anyone know what's wrong with my code
#1

[eluser]Corbee[/eluser]
Hi,

I'm currently a newbie trying to learn ci, but it appears that my code won't work.
Well it does work, but it didn't display the thing that needs to be displayed.

Here is what I'm trying to achieve, I have a table(table A) where each rows has a
link that says see items, the item has a separate table(table B), but it has a reference id to Table A. For Example:

Table A

tableAID info1 info2

Table B

tableBID infoA infoB tableAID

I didn't get an error, but the output displayed are all gibberish

here is my model
Code:
function getTableB(id)
    {
    $data = array();
    $options = array('tableAID' => $id);
    $q=$this->db->get('TableB',$options);
        if ($q->num_rows() > 0)
        {
            $data = $q->row_array();
        }
    $q->free_result();
    return $data;

Controller
Code:
function viewitems($id)
    {
        $data['title'] = "Manage Data";
        $data['main'] = 'admin_tableb_home';
        $data['TableB'] = $this->MTable->getTableB($id);
        $this->load->view('dashboard',$data);
    }

View
Code:
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;?php
if ($this->session->flashdata('message')){
echo "<div class='message'> ".$this->session->flashdata('message')." </div>";
}
if (count($TableB)){

        foreach ($tableB as $key => $list)
        {
            echo $list['tableBID']."\n";
            echo $list['InfoA']."\n";
            echo $list['InfoB']."\n";
        }

}
?&gt;

does anyone know what went wrong?
Thanks
#2

[eluser]caustic[/eluser]
Probably, something like this:
Code:
function getTableB ($id) {
    $data = array();
    $this->db->where('tableAID', $id);
    $this->db->limit($entries_per_page);    // better add some entries limit to SQL
                                            // query to minimise memory consumption
    $data = $this->db->get('TableB');
    $data = $data->result();
    return $data;
}
P.S.: always check variables, before send it to model or check it in model !)
#3

[eluser]Corbee[/eluser]
Thanks, I tried your code, unfortunately an error occured at views

Code:
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;?php
if ($this->session->flashdata('message')){
echo "<div class='message'> ".$this->session->flashdata('message')." </div>";
}
if (count($TableB)){

        foreach ($tableB as $key => $list)
        {
            echo $list['tableBID']."\n"; //<---- Error starts at this line
            echo $list['InfoA']."\n";
            echo $list['InfoB']."\n";
        }

}
?&gt;
#4

[eluser]caustic[/eluser]
Code:
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;?php
if ($this->session->flashdata('message')){
echo "<div class='message'> ".$this->session->flashdata('message')." </div>";
}
if (count($TableB)){

        foreach ($TableB as $entry)     // Wrong variable name. Your begins
                                        // from capital letter.
        {
            echo $entry->TableBID."\n"; // Wrong variable name too. Also, CI Active
                                        // Record returns an array of objects, where
                                        // variables of object is names of SQL DB columns.
                                        // read, how to user Active Record.
            echo $entry->InfoA."\n";
            echo $entry->InfoB."\n";
        }

}
?&gt;
in my opinion using CamelStyleInVariablesAndConstants is not a good idea for PHP (only for Pascal or Delphy probably). better use lower_case_naming_style, it will decrease syntax errors.
#5

[eluser]Corbee[/eluser]
Thanks for the advice, unfortunately, I got another error this time it says


Quote:Cannot use object of type stdClass as array in C:\ampp\htdocs\insurancec\admin\application\views\admin_home.php on line 19
which is the same line
#6

[eluser]caustic[/eluser]
sorry:
Code:
<h1>&lt;?php echo $title;?&gt;</h1>
&lt;?php
if ($this->session->flashdata('message')){
echo "<div class='message'> ".$this->session->flashdata('message')." </div>";
}
if (count($TableB)){
        foreach ($TableB as $entry) {
            echo $entry->tableBID."\n";
            echo $entry->infoA."\n";
            echo $entry->infoB."\n";
        }
}
?&gt;
this should work correctly.
#7

[eluser]n0xie[/eluser]
never mind...




Theme © iAndrew 2016 - Forum software by © MyBB