Welcome Guest, Not a member yet? Register   Sign In
Trying to figure out how to use db->where... does my code make sense?

<?    if ($ads_query->num_rows() > 0): ?>

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

<div style="border:1px solid black; padding:10px; margin-bottom:10px;">

&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:
$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:

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

And this is in my controller:
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);

Alright, I think I figured it out, but let me know if I can do this more efficiently.
&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();
        <div style="border:1px solid black; padding:10px; margin-bottom:10px;">
    &lt;?    endforeach; ?&gt;

&lt;?    endif; ?&gt;

Seems to work...

[eluser]Jon L[/eluser]
$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

[eluser]Jon L[/eluser]
You can also use

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

Can't believe I forgot about Models. Thanks, I really appreciate that code. I'm going to look into doing it in a Model.

Theme © iAndrew 2016 - Forum software by © MyBB