php - Codeigniter join 2 tables data with new column for id value -


I'm new to Codeigniter and PHP , just learn I Searched for it but did not find the correct answer. I have a table in which there is some data, call it post and in this table, I have category_id in each post. I have categories There is one more table and I have 2 columns in this table: id and name .

I want to join two tables which I will get the result post as well as another column called category_name categories. Name . .

  function getPostsWithByCategoryID ($ numberOfRows, $ start, $ categoryId)  

How much time was it so far? {$ This-> Db- & gt; Choose ('*'); Order - ('id', 'character') from $ '-' ('post') -> - & gt; Range ($ numberOfRows, $ start); $ This- & gt; Db- & gt; Where ('category_id', $ categoryID); $ This- & gt; Db- & gt; Join ('Categories', 'posts.category_id == categories.id'); $ Query = $ this- & gt; Db- & gt; get receive (); Return $ query- & gt; Result_are (); }

Thanks in advance.

=========================================== =============================

EDIT:

Adrian Farsius of the North After trying:

  function getPostsWithByCategoryID ($ numberOfRows, $ start, $ categoryId) {$ this- & gt; Db- & gt; ('*, Category; name AS category_name'); Order - ('id', 'character') from $ '-' ('post') -> - & gt; Range ($ numberOfRows, $ start); $ This- & gt; Db- & gt; Where ('category_id', $ categoryID); $ This- & gt; Db- & gt; Join ('Categories', 'posts.category_id = categories.id'); $ Query = $ this- & gt; Db- & gt; get receive (); Return $ query- & gt; Result_are (); }   

I am getting this error:

  Error number: 1054: Unknown column in 'field list' category.name 'SELECT *, ` Category 'name' from AS Category_name (`post ') Include` categories' on' post '.' Category_id` = `Categories````````` where` NO 'on the basis of category_ID`` ID Code>  

====

Edit 2:

So I found some mistakes in the code and decided them, tried again, but then Also made me an error: (using the $ categoryId instead of the $ categoryID and select category.name instead of the correct name of the table categories which categories.name

  error number: 1052: column 'ID' is indistinguishable in the order segment SELECT *, `categories`. `` `` `` `` `` `` `` `` `` `` `` `` `` ` Include 'category' id```` categories```````` where`category_id` = '3' / pre>  

What it wants to do:

  function getPostsWithByCategoryID ($ numberOfRows, $ start, $ categoryId) Select {$ this-> db-> ('*, $ '-' ('posts') -> gt; by order - ('posts.id', 'desc') - & gt; range from $ ($ numberOfRows, $ start) $ $ - & gt; db- & gt; $ This- & gt; Db- & gt; Where ('category_id', $ categoryId); $ This- & gt; Db- & gt; Join ('Categories', 'posts.category_id = categories.id'); $ Query = $ this- & gt; Db- & gt; get receive (); Return $ query- & gt; Result_are (); }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -