PHP Code Ignitor

The following PHP Query works as it should. I am querying for a title, an image and a category ID.  I must join two tables to accomplish this.
The common denominator between the two tables is the entry ID.  However, in the second table the entry ID can have numerous category entries.

I need to get the last cat_id that the query returns.  Right now, it will return three or four cat_ids per for_each.  I just want the last one.

		$laser_query = ee()->db->select('t1.field_id_1091 AS title, t1.field_id_1141 AS image, t2.cat_id AS cat_id')
		->from('exp_channel_data t1')
		->where('t1.field_id_3731', $cw)
		->join('exp_category_posts t2', 't2.entry_id = t1.entry_id')
		foreach($laser_query->result_array() as $row){
   			echo $row['title'].'<br />';
   			echo $row['image'].'<br />';
   			echo 'CAT ID:'.$row['cat_id'].'<br /><br />';

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What defines the "last one" sounds like you need to join to a sub query.

Personally I would create a view to return what you want rather than complicated CI code to achieve the same result.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.