Jump to content

I need a way to puch selected data to the beginning of an array.


gibigbig

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.