Welcome Guest, Not a member yet? Register   Sign In
single result from multiple
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

[quote author="industryslave" date="1245012391"]
So my question is: Is this the correct/best way to go about this?
[/quote]

I'm quite sure it's not.

[quote author="industryslave" date="1245012391"]
Is their a more efficient way?
[/quote]

Perhaps. I'm not saying my way is the best way, but I really believe it's an improvement.

Why not have a next and prev method in your controller, that accepts the ID of the product, then you can simply grab a single next result, or previous result. It's less queries, and less complication in my humble opinion.

Code:
function next($product_id="")
{
    $product_id = (int)$product_id;

    # This should be in a model method, but for simplicity, I've put it here.

    # Get the category ID
    $this->db->select('cat_id');
    $this->db->where('id', $product_id);
    $cat_id = $this->db->get('products');
    $cat_id = $cat_id->row_array();
    $cat_id = $cat_id['id'];

    # Get the ID of the next product.
    $this->db->select('id');
    $this->db->where('cat_id', $cat_id);
    $this->db->where('id >', $product_id);
    $this->limit(1);
    $pid = $this->db->get('products');
    $pid = $pid->row_array();
    $pid = $pid['id'];

    redirect('view_product/'.$pid);
}

It might even be possible to do it in a single query, but I'm not sure how. You'll need to add some error checking/handling in there too, but hopefully you get the gist.

[quote author="industryslave" date="1245012391"]
Is "I can't believe it's not butter" really not butter? Smile
[/quote]

"I Can't Believe It's Not Butter" is to butter, as Amy Winehouse is to fluffy puppies.

[quote author="industryslave" date="1245012391"]
Any thoughts or impressions would be greatly appreciated.
[/quote]

Perhaps, but what's on my mind is a bit too explicit to share in a public place.

Thanks[/quote]


Messages In This Thread
single result from multiple - by El Forum - 06-14-2009, 09:46 AM
single result from multiple - by El Forum - 06-14-2009, 10:21 AM
single result from multiple - by El Forum - 06-14-2009, 07:39 PM
single result from multiple - by El Forum - 06-15-2009, 11:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB