millsy007 Posted March 4, 2009 Share Posted March 4, 2009 I have a function that checks for duplicate names, currently it works off one column: $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: $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]? Quote Link to comment https://forums.phpfreaks.com/topic/147988-combining-query-result-rows-into-array/ Share on other sites More sharing options...
millsy007 Posted March 6, 2009 Author Share Posted March 6, 2009 Could I perhaps use the array_combine: $array1[] = $row[seat1]; $array2[] = $row[seat2]; $array3[] = $row[seat3]; $similar_names[] = array_merge($array1, $array2, $array3); This way would all of the columns be merged into one array I could then use? Quote Link to comment https://forums.phpfreaks.com/topic/147988-combining-query-result-rows-into-array/#findComment-777785 Share on other sites More sharing options...
sasa Posted March 6, 2009 Share Posted March 6, 2009 just do while($row = mysql_fetch_array($qry_result)) { $similar_names[] = $row['seat1']; $similar_names[] = $row['seat2']; $similar_names[] = $row['seat3']; } Quote Link to comment https://forums.phpfreaks.com/topic/147988-combining-query-result-rows-into-array/#findComment-777890 Share on other sites More sharing options...
millsy007 Posted March 6, 2009 Author Share Posted March 6, 2009 I do $query = " SELECT seat1, seat2, seat3, seat4, seat5, seat6, seat7, seat8 FROM journey WHERE journey.shuttle_id = '$id' AND seat1 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[seat1]; $similar_names[] = $row[seat2]; $similar_names[] = $row[seat3]; $similar_names[] = $row[seat4]; $similar_names[] = $row[seat5]; $similar_names[] = $row[seat6]; $similar_names[] = $row[seat7]; $similar_names[] = $row[seat8]; } But it is still only checking the data from the: $similar_names[] = $row[seat1]; Part of the code Quote Link to comment https://forums.phpfreaks.com/topic/147988-combining-query-result-rows-into-array/#findComment-778108 Share on other sites More sharing options...
kickstart Posted March 6, 2009 Share Posted March 6, 2009 Hi Think the basic problem is a database design issue. Having a seperate column for each seat is very much against basic design ideas. You really should split the different seats off onto a seperate table. However if you do have a fixed number of columns then $query = " SELECT seat1 FROM journey WHERE journey.shuttle_id = '$id' AND seat1 LIKE '$name%' UNION ALL SELECT seat2 FROM journey WHERE journey.shuttle_id = '$id' AND seat2 LIKE '$name%' UNION ALL SELECT seat3 FROM journey WHERE journey.shuttle_id = '$id' AND seat3 LIKE '$name%' "; You could add a few more SELECT statements to this if you have more columns. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/147988-combining-query-result-rows-into-array/#findComment-778134 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.