njdirt Posted January 3, 2008 Share Posted January 3, 2008 Hi, I'm trying to finish a bit of code to select and order products by brand, and then by model. The brands that it lists are viariable, and therefore the first section of the big "while" statement works with parts of an array, which may have to iterate 2, 5, or a thousand times. After all of the listed brands have been pulled, I want to pull every other brand in the db that wasn't listed. An "Other" category. Here is the code I have thus far. Please le me know if this is unclear. Thanks! The wildcard '%' I used returns an error in the 'miscellaneous' section. <?php //check the models carried, create tables for them and one for miscellaneous bikes $query = "SELECT makes_carried FROM $table1 WHERE id='1'"; $result = mysql_db_query ($dbname, $query, $link); $row = mysql_fetch_array($result); //handle the string, create array $make_array = explode(",", $row[makes_carried]); //count how many makes are listed $list_number = count($make_array); $make_iteration = "0"; while ($make_iteration <= $list_number){ echo "<tr><Td align=\"left\" colspan=\"5\"><b>$make_array[$make_iteration]</b></td></tr>"; //get models per make and list by model $query2 = "SELECT * FROM $table9 WHERE make='$make_array[$make_iteration]' ORDER BY model asc"; $result2 = mysql_db_query ($dbname, $query2, $link); while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td> </td><td>$row2[model]</td><td>$row2[frame_size]</td><td>$row2[color]</td><td></td>"; } $make_iteration++; } //print miscellaneous echo "<tr><Td align=\"left\" colspan=\"5\"><b>Miscellaneous</b></td></tr>"; //get models per make and list by model $query2 = "SELECT * FROM $table9 WHERE make NOT LIKE '$make_array[%]' ORDER BY model asc"; $result2 = mysql_db_query ($dbname, $query2, $link); while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td>$row2[make]</td><td>$row2[model]</td><td>$row2[frame_size]</td><td>$row2[color]</td><td></td>"; } ?> -Mark Quote Link to comment https://forums.phpfreaks.com/topic/84345-solved-php-mysql-select-remainder-of-database/ Share on other sites More sharing options...
Barand Posted January 3, 2008 Share Posted January 3, 2008 Assuming makes_carried column looks like "1,4,7,9" try <?php $query = "SELECT makes_carried FROM $table1 WHERE id='1'"; $res = mysql_query($query); // // change makes to look like " 1','4','7','9 " in case they are string values // $makes_carried = str_replace(",", "','", mysql_result ($res, 0)); $query2 = "SELECT * FROM $table9 WHERE make IN ('$makes_carried') ORDER BY model"; while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td> </td><td>{$row2['model']}</td><td>{$row2['frame_size']}</td><td>{$row2['color']}</td><td></tr>"; } echo "<tr><Td align=\"left\" colspan=\"5\"><b>Miscellaneous</b></td></tr>"; $query2 = "SELECT * FROM $table9 WHERE make NOT IN ('$makes_carried') ORDER BY model"; while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td> </td><td>{$row2['model']}</td><td>{$row2['frame_size']}</td><td>{$row2['color']}</td><td></tr>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84345-solved-php-mysql-select-remainder-of-database/#findComment-429641 Share on other sites More sharing options...
njdirt Posted January 3, 2008 Author Share Posted January 3, 2008 Hi, thanks for the quick reply. 'makes carried' looks like Trek,Gary Fisher, Klein. $make_array breaks this string into an array, with $make_array[0] = Trek. Then $list_number finds out how many names are in the array, so that it can run the loop one time for each name. After these have all run, I want the final Misc. loop to retrieve all of the entries that do not have brand names found in $make_array[]. I feel like if I could find the proper way to say "SELECT * WHERE make != $make_array[%]" then it would work, since the make in the db will be GT, Schwinn, or anything else besides the ones listed as $make_array[0,1,2...]. As I said, the sql wildcard % is not working to do this. Must it be in PHP since $make_array[] is a php call to an array? * as a wildcard doesn't work either. I hope this as clarified, and thanks again! -Mark Quote Link to comment https://forums.phpfreaks.com/topic/84345-solved-php-mysql-select-remainder-of-database/#findComment-429668 Share on other sites More sharing options...
Barand Posted January 3, 2008 Share Posted January 3, 2008 'makes carried' looks like Trek,Gary Fisher, Klein. The queries I posted should work with string makes also. That's why I added the 's Quote Link to comment https://forums.phpfreaks.com/topic/84345-solved-php-mysql-select-remainder-of-database/#findComment-429677 Share on other sites More sharing options...
njdirt Posted January 3, 2008 Author Share Posted January 3, 2008 Thanks very much! I tweaked a few tiny things, but you're right, the code works as I asked. Here is the final code... <?php //check the models carried, create tables for them and one for miscellaneous bikes $query = "SELECT makes_carried FROM $table1 WHERE id='1'"; $result = mysql_db_query ($dbname, $query, $link); $res = mysql_db_query ($dbname, $query, $link); // // change makes to look like " 1','4','7','9 " in case they are string values // $makes_carried = str_replace(",", "','", mysql_result ($res, 0)); $row = mysql_fetch_array($result); //handle the string, create array $make_array = explode(",", $row[makes_carried]); //count how many makes are listed $list_number = count($make_array); $make_iteration = "0"; while ($make_iteration <= $list_number){ echo "<tr><Td colspan=\"5\"><h2>$make_array[$make_iteration]</h2></td></tr>"; //get models per make and list by model $query2 = "SELECT * FROM $table9 WHERE make IN ('$make_array[$make_iteration]') ORDER BY model"; $result2 = mysql_db_query ($dbname, $query2, $link); while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td> </td><td>{$row2['model']}</td><td>{$row2['frame_size']}</td><td>{$row2['color']}</td><td></tr>"; } $make_iteration++; } //print miscellaneous echo "<tr><Td colspan=\"5\"><h2>Miscellaneous</h2></td></tr>"; //get models per make and list by model $query2 = "SELECT * FROM $table9 WHERE make NOT IN ('$makes_carried') ORDER BY model"; $result2 = mysql_db_query ($dbname, $query2, $link); while ($row2 = mysql_fetch_array($result2)){ echo "<tr><td> </td><td>{$row2['model']}</td><td>{$row2['frame_size']}</td><td>{$row2['color']}</td><td></tr>"; } ?> Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/84345-solved-php-mysql-select-remainder-of-database/#findComment-429696 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.