gibigbig Posted September 9, 2010 Share Posted September 9, 2010 This is my code: $episodes = mysql_query("SELECT * FROM episode_links WHERE episode_id = '".$episode_id."' AND moderated='1' ORDER BY host ASC"); while ($episode = mysql_fetch_assoc($episodes)) { // code here } and in my host table, I have values like this: Fileserve.com MegaUpload.com Rapidshare.com Torrent And I would like a way to sort this so that only the Fileserve links appear on the top ( the first few results), then all the others. can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/ Share on other sites More sharing options...
Psycho Posted September 9, 2010 Share Posted September 9, 2010 Not sure what you mean by "host table" since the name of the table is "episode_links". Do the records in the "episode_links" table have a foreign key back to a hosts table with the values specified? If so, please show the fields from both tables and the ones that are associated. Also, you state you want the fileserver records to display first, do you have any preference on how the rest are ordered according to their host? FYI: This has nothing to do with arrays. This is a database question. Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109206 Share on other sites More sharing options...
Psycho Posted September 9, 2010 Share Posted September 9, 2010 Here is an example of how you could change your query to get what you want. It is only an example since I don't understand your database structure, names, etc. SELECT el.*, IF(h.hostname='Fileserve.com', 0, 1) as primary_sort FROM episode_links as el JOIN hosts as h on el.hostID = h.hostID WHERE el.episode_id = '$episode_id' AND el.moderated='1' ORDER BY primary_sort ASC, el.host ASC Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109209 Share on other sites More sharing options...
gibigbig Posted September 9, 2010 Author Share Posted September 9, 2010 Here is an example of how you could change your query to get what you want. It is only an example since I don't understand your database structure, names, etc. SELECT el.*, IF(h.hostname='Fileserve.com', 0, 1) as primary_sort FROM episode_links as el JOIN hosts as h on el.hostID = h.hostID WHERE el.episode_id = '$episode_id' AND el.moderated='1' ORDER BY primary_sort ASC, el.host ASC this seems like what (the primary sort thing) I want, can you please comment on this code line by line so I can follow what it means. it would help greatly Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109211 Share on other sites More sharing options...
Psycho Posted September 9, 2010 Share Posted September 9, 2010 Again, I don't even know if this is correct for your situation. I asked you to provide details on your database to make this more efficient. After rereading your query it looks like you have a "host" field in the "episode_links" table. If that field actually holds the name of the host then the query would be much simpler: SELECT *, IF(host='Fileserve.com', 0, 1) as primary_sort FROM episode_links WHERE episode_id = '$episode_id' ORDER BY primary_sort ASC, el.host ASC Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109214 Share on other sites More sharing options...
gibigbig Posted September 9, 2010 Author Share Posted September 9, 2010 http://img194.imageshack.us/img194/6889/86504462.png This is my exact database structure for that table. i simply want all matches with the host "fileserve.com" to be displayed first, then all the other matches. Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109219 Share on other sites More sharing options...
Psycho Posted September 9, 2010 Share Posted September 9, 2010 Then try the last query I posted. Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109259 Share on other sites More sharing options...
gibigbig Posted September 9, 2010 Author Share Posted September 9, 2010 didnt work man Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109404 Share on other sites More sharing options...
Psycho Posted September 10, 2010 Share Posted September 10, 2010 didnt work man Well, that's helpful. Do you care to tell me "how" it didn't work such as any errors you received or how the results were not what you expected? Or am I supposed to debug the problem with my clarivoiant abilities? Quote Link to comment https://forums.phpfreaks.com/topic/212967-i-need-a-way-to-puch-selected-data-to-the-beginning-of-an-array/#findComment-1109482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.