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

  • Home
  • SEARCH
  • 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 935543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:10:22+00:00 2026-05-15T21:10:22+00:00

I have 2 tables in a mysql database : users and pets . In

  • 0

I have 2 tables in a mysql database : users and pets. In the users table there is a field that contains a semicolon separated list of the pets that a user has (pet_ids = 1;2;3;4;). These ids correspond to unique ids (keys) in the pets table which contain the pet name (1 = rex, 2 = goldie, 3 = squeak, 4 = bubble).

I want to present a list of these pets on a PHP page in alphabetical order but retain the id of the pet name to pass as a variable to another page when the name of the pet is clicked.

What is the most efficient way to achieve this.

So far the list I generate is not in alphabetical order but in order that the pets were added –

Functions for querying the database

function getUserPets($user_id) {    
  $query= "SELECT pet_ids FROM users WHERE id='$user_id'";  
  $result = @mysql_query ($query);
  if (!$result) {
      die('Invalid query: ' . mysql_error());
  }
  while($row = mysql_fetch_array($result)) {
          $the_pets = $row['pet_ids'];
  }
  return $the_pets; //1;2;3;4;
} 
function getPetName($id) {
$name = "";
    $query= "SELECT name FROM pets WHERE id='" . $id . "' ";
$result = @mysql_query ($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
    $name = $row['name'];
}
return urldecode($name);
}

The code for generating the list …

$petstring = getUserPets($userid);
$petsarray = explode(";", $petstring);
foreach ($petsarray as $pet_id) {
    echo("<li><h4><a href=\"pet.php?id=$pet_id\">" . writeListPetName($pet_id) . "</a></h4></li>");     
}

What is the most efficient way to get an alphabetized list – I need to keep the database structure the same.

  • 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-05-15T21:10:23+00:00Added an answer on May 15, 2026 at 9:10 pm

    If you have to work with separated lists for a set of ids, rather than a properly normalised table, at least use a single db query to return the set of pet names rather than separate queries for each individual pet.

    $petsarray = implode(',',explode(";", $petstring)); 
    
    function getPetName($ids) { 
       $name = ""; 
       $query= "SELECT id, name FROM pets WHERE id IN (" . $ids . ") ORDER BY name"; 
       $result = mysql_query ($query); 
       if (!$result) { 
          die('Invalid query: ' . mysql_error()); 
       } 
       while($row = mysql_fetch_array($result)) { 
          $names[$row['id']] = urldecode($row['name']); 
       } 
       return $names; 
    } 
    
    var_dump($names);
    

    And don’t use @ to suppress errors, trap for them properly

    P.S. Why do you need to urldecode() the pet names?

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

Sidebar

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.