Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:08:52+00:00 2026-06-13T15:08:52+00:00

This code is used to get a specific list of ID’s from one table,

  • 0

This code is used to get a specific list of ID’s from one table, then use those ID’s to get the information from another table. Once I get all the information from the 2nd table, I am attempting to sort the data alphabetically based on a field in the 2nd table.

Example, I am getting the name based on a correlating ID and then want to display the entire result in alphabetical order by name (artist_name).

Here is the code I have. When I execute this without the sort(), it works fine but is not in alphabetical order. When I add the sort() in the 2nd while statement, the page looks the same but the name and other data do not display. The source code in the browser shows that the results are being accounted for but the sort must be preventing the variables or information from being displayed for some reason.

I haven’t used a sort function before and I tried looking at some examples but couldn’t really find something specific to my situation. Any and all help would be greatly appreciated. I have already looked at the PHP manual for sort so no need to send me a link to it 😉

<?php $counter = 0;
    $artistInfo = mysql_query("SELECT DISTINCT event_url_tbl.artist_id FROM event_url_tbl WHERE (SELECT cat_id FROM artist_tbl WHERE artist_tbl.artist_id = event_url_tbl.artist_id) = 1");
    while ($aID = mysql_fetch_array($artistInfo))
        {
            $getArtistInfo = mysql_query("SELECT * FROM artist_tbl WHERE artist_id = '" . $aID['artist_id'] . "'");
            while($artist = mysql_fetch_array($getArtistInfo))
                { 
                    sort($artist);?>
                    <a class="navlink" href="<?=HOST?><?=$artist['page_slug']?>/index.html">
                        <?=$artist['artist_name']?>
                    </a><br />
                <?php }
        }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:08:53+00:00Added an answer on June 13, 2026 at 3:08 pm

    Your best bet, as a commenter mentioned, is to use an ORDER BY clause in SQL.

    SELECT * 
    FROM artist_tbl 
    WHERE artist_id = XXX 
    ORDER BY artist_name ASC
    

    The other commenter who suggested using PDO or mysqli is also correct, but that’s a different issue.

    To answer your specific question about sorting, according to the manual,

    Blockquote Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys.

    This means all of your array keys (‘page_slug’, ‘artist_name’, etc) are wiped out. So when you try to refer to them later, there is no data there.

    Were you to use this method, you would want to use asort to sort an associative array.

    However, you don’t want to use sort here. What you’re sorting is the variables for one row of data (one individual artists), not all of your artists. So if you think of each artist row as an index card full of data (name, id#, page slug, etc) all you’re doing is moving those items around on the card. You’re not reorganizing your card catalog.

    Using an order by clause in the SQL statement (and rewriting in PDO) is your best bet.

    Here is how I would rewrite it. I have to take some guesses at the SQL because I’m not 100% sure of your database structure and what you’re specifically trying to accomplish, but I think this would work.

    $query_str = "
        SELECT 
            artist.name,
            artist.page_slug
        FROM 
            artist_tbl AS artist
            INNER JOIN event_tbl AS event
                ON event.artist_id = artist.artist_id
        WHERE
            artist.cat_id = 1
        ORDER BY
            artist.name ASC";
    $db_obj = new PDO (/*Connection stuff*/);
    $artists_sql = $db_obj->prepare ($query_str);
    $artists_sql->execute ();
    while ($artist = $artists_sql->fetch (PDO::FETCH_ASSOC)) {
        $return_str .= "<a class='navlink' href='{HOST}
                    {$artist['page_slug']}/index.html'>
                    {$artist['artist_name']}</a><br/>";
        }
    echo $return_str;
    

    In all honesty, I would probably create an artist class with a display_link method and use PDO’s fetchObject method to instantiate the artists, but that’s getting ahead of ourselves here.

    For now I stuck with procedural code. I don’t usually like to mix my HTML and PHP so I assign everything to a return string and echo it out at the end. But this is close to what you had, using one SQL query (in PDO – seriously worth starting to use if you’re creating new code) that should give you a list of artists sorted by name and their associated page_slugs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following code to get bitmap from url. This function is used
the xml <publication_date media_type=print> <month>1</month> <year>2011</year> </publication_date> I have used this code to get
this code in my plugin used to work just fine: jQuery('#embedded_obj', context).get(0).getVersion(); and the
I get this error when I run the code below. I have normally used
This code used to work. Then, maybe I changed something, somewhere (or if I
This code used to return my local ip address as 192.xxx.x.xxx but now it
This code used to work but doesnt any more. i used a breakpoint, and
...at least to me. This code used to work fine. I'm pretty sure nothing
I have used this code (kind of tutorial) at http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5 ... In this code,
I've used this code for a while now for a few sites I've built

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.