Jump to content

Recommended Posts

Hello!

 

Im new in this forum and i hope you can help me with my question.

 

Basically i have 2 tables, one called categories and the other one empresas.

 

categories has ID and Category(category names), empresas has many more but im only interested in ID_CATEGORY(linked id from Categories).

 

What my php does, is that it prints all of the data from empresas, but when i want to sort them by category, it sorts by the ID from Categories, and i need to sort  them using the category names.

 

This is the code i have for sorting it.

 


//obtain the sorting order
if (isset($_GET['order'])){
 $order = $_GET['order'];
}else{
 $order = "name";
}

$query  = "SELECT * FROM empresas ORDER BY $order LIMIT $offset, $rowsPerPage";

 

nevermind the $offset, and $rowsPerPage, its just for paging.

 

So can you help me with this?

Link to comment
https://forums.phpfreaks.com/topic/138254-sorting-by-values-from-linked-table/
Share on other sites

First of all, if you are using LIMIT, you must be using MySQL...not MSSQL. So, can a mod please move this?

 

To answer your question, you need to join the tables:

 

SELECT * FROM empresas e LEFT JOIN categories c ON e.id_category = c.id ORDER BY c.category

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.