Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    52
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining Query Result Rows Into Array

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

    Code:
    $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:
    $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]?

    Could I perhaps use the array_combine?

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I think to accomplish this you may need to check the values of the seat fields outside of mysql.

    Maybe
    PHP Code:
    $query "SELECT * FROM journey WHERE shuttle_id = '$id'";
     
    $qry_result mysql_query($query) or die(mysql_error());

    while (
    $row mysql_fetch_array($qry_resultMYSQL_NUM)){
        for(
    $i=0$i<count($row); $i++){
            
    $pos stripos($row[$i],$name);
            if(
    $pos && $pos <= 1){
                
    $similar_names[] = $row[$i];
            }
        }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •