Combining Mutliple Query Result Columns Into Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Combining Mutliple Query Result Columns Into Array

Post by millsy007 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I have a function that checks for duplicate names, currently it works off one column:

Code: Select all

$query = "
SELECT   seat
FROM     journey
WHERE    shuttle_id = '$id'
AND        seat LIKE '$name%'
";
 
$qry_result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($qry_result);
   if ($num_rows > 0) {   // if it exists, then put all similar names into an array
      
      while($row = mysql_fetch_array($qry_result))
      { 
         $similar_names[] = $row[seat];
 
      }
         // check in the similar names array  if your name exists, if so, adds 1 to suffix, then check again until you find your final suffix.
         $suffix = 1;
         while (in_array($name.$suffix, $similar_names))
         $suffix++;
         $name = $name.$suffix;
         
         $finalname = $name;
      }
   else
      {
      //$name is already unique. no suffix needed then.
      $final_name = $name;
      }
However I have multiple seats so is there a way that I could check against all the seats so my query would be:

Code: Select all

$query = "
SELECT   seat1, seat2, seat3
FROM     journey
WHERE    journey.shuttle_id = '$id'
AND        seat1 LIKE '$name%'
";
 
and then somehow combine the result into the array that I check against:
 
$similar_names[] = $row[seat] //And $row[seat2] And $row[seat3]?

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Combining Mutliple Query Result Columns Into Array

Post by BomBas »

First of all, your query is using WHERE statement, so most likely it will pull out only 1 row.

After that, you will have to add a loop so you can get all of your results.

Code: Select all

 
$array = array();
$query = '...';
while( $row = mysql_fetch_assoc($query) )
{
$array[ $row['id'] ] = $row['name'];
}
 
print_r($array);
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: Combining Mutliple Query Result Columns Into Array

Post by millsy007 »

Hi Thanks, I am confused about where in my code I would need to put a loop.

Are you suggesting I query one seat at a time and then build the array in that way?

Or do one query and then add a loop to add each of the columns to the array?
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Combining Mutliple Query Result Columns Into Array

Post by BomBas »

millsy007 wrote:Hi Thanks, I am confused about where in my code I would need to put a loop.

Are you suggesting I query one seat at a time and then build the array in that way?

Or do one query and then add a loop to add each of the columns to the array?
Look at my code, excatly what you need.

Here's a little explanation:

Code: Select all

 
 // Defining the name of our array's variable
 $array = array();
 // Setting up the query
 $query = '...';
 // Looping through
 while( $row = mysql_fetch_assoc($query) )
 {
 // Making key and value for each row that we fetched
 $array[ $row['id'] ] = $row['name'];
 }
  
 // Printing out our array(Or you can make another loop for the array)
 print_r($array);
 
Post Reply