• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to figure out how to use db->where... does my code make sense?

#1
[eluser]dallen33[/eluser]
Code:
<?    if ($ads_query->num_rows() > 0): ?>

<?    foreach($ads_query->result() as $ads_row): ?>

<div style="border:1px solid black; padding:10px; margin-bottom:10px;">
    <p>&lt;?=$ads_row->run_date?&gt;</p>
    <p>&lt;?=$ads_row->notes?&gt;</p>
</div>

&lt;? endforeach; ?&gt;

&lt;? endif; ?&gt;

The code above works fine. I have a table called "ads" and a table called "clients". In "ads", a record can reference a client name by having the client ID. The "clients" table has the client name and ID. Simple stuff.

So how do I code this wisely? I want this record's client ID to show up as the client name. Before CodeIgniter, I'd do it like this:
Code:
$client_rslt = $mysqli->query("SELECT * FROM clients WHERE id = '" . $ad_row->client . "'");
$client_row = $client_rslt->fetch_object();

And then I'd show the result like this:
Code:
&lt;?=$client_row->client?&gt;

What's the best way of doing this in my code now that i'm using CodeIgniter. I've tried a few things but they seem clumsy. I'm very much a newb, so any help would be great Smile

#2
[eluser]dallen33[/eluser]
And this is in my controller:
Code:
function insertion()
    {
        $data['title'] = "Insertion Order";
        $this->db->where('id', $this->uri->segment(3));
        $data['ads_query'] = $this->db->get('ads');
        $data['clients_query'] = $this->db->get('clients');
    
        $this->load->view('/booking/insertion', $data);
    }

#3
[eluser]dallen33[/eluser]
Alright, I think I figured it out, but let me know if I can do this more efficiently.
Code:
&lt;?    if ($ads_query->num_rows() > 0): ?&gt;

    &lt;?    foreach($ads_query->result() as $ads_row): ?&gt;
    
    &lt;?    $clients_query = $this->db->get_where('clients', array('id' => $ads_row->client));
        $clients_row = $clients_query->row();
    ?&gt;
    
        <div style="border:1px solid black; padding:10px; margin-bottom:10px;">
            <p>&lt;?=$clients_row->name?&gt;</p>
            <p>&lt;?=$ads_row->run_date?&gt;</p>
            <p>&lt;?=$ads_row->notes?&gt;</p>
        </div>
    
    &lt;?    endforeach; ?&gt;

&lt;?    endif; ?&gt;

Seems to work...

#4
[eluser]Jon L[/eluser]
Code:
$where = array('id' => $this->uri->segment(3));
$query = $this->db->get_where('ads', $where);
$data['ads_query'] = $query->result_object(); # Should be an array of objects
$query = $this->db->get_where('clients', $where);
$data['clients_query'] = $query->result_object(); # Should be an array of objects

EDIT: however you actually implement is up to you, my example is intended for the controller (though ideally, wouldn't it go into the model, as I believe models are meant for handling the data... bah, dang MVC...), hopefully this helps

#5
[eluser]Jon L[/eluser]
You can also use
Code:
$this->db->row_object();

I'm getting these examples from system/database/DB_result.php

#6
[eluser]dallen33[/eluser]
Can't believe I forgot about Models. Thanks, I really appreciate that code. I'm going to look into doing it in a Model.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.