gamerzfuse Posted May 9, 2009 Share Posted May 9, 2009 I need to adapt the following code (which displays all vehicles with the same model as the displayed vehicle) for two separate purposes: a) Exclude the vehicle that is currently being viewed. The Key value is their stock number, I would like to somehow do SELECT * FROM vehicles WHERE model=$model "EXCLUDING stock=$stock" b) I need to use this code to display a SEPARATE box that shows vehicles with prices within $2000 of the currently displayed vehicle. ie: SELECT * FROM vehicles WHERE price="BETWEEN ($price - 2000) ($price + 2000)" <?php //Make first query to database $query = "SELECT * FROM vehicles"; $result = mysql_query($query) or die ("Couldn’t execute query."); //Set Variables $vehiclemodel = $vehicles->model->ViewValue; $show_price = number_format($vehicles->price->ViewValue); $query = "SELECT * FROM vehicles WHERE model='$vehiclemodel'"; $result = mysql_query($query) or die ("Couldn't execute query."); // Display results in a table echo "<tr><td colspan='5'></td></tr>"; while ($row = mysql_fetch_array($result)) { extract($row); //Table Contents echo "<tr class='dataright'>\n <td colspan='5' class='dataleft'><a href='view.php?stock=$stock'>$year $model</a></td>\n <td>$odometer km </td>\n <td align='right'>$$show_price.00</td>\n </tr>\n"; echo "<tr><td colspan='3'></td></tr>\n"; } ?> Any help would be much appreciated as I am very close to finalizing this project, I'm a bit of a new comer to PHP but I'm learning quick. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/ Share on other sites More sharing options...
RussellReal Posted May 9, 2009 Share Posted May 9, 2009 SELECT * FROM `vehicles` WHERE `model` = '{$model}' AND `stock` != '{$stock}' SELECT * FROM `vehicles` WHERE `price` >= ($price - 2000) AND `price` <= ($price + 2000) both should work Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/#findComment-830363 Share on other sites More sharing options...
gamerzfuse Posted May 9, 2009 Author Share Posted May 9, 2009 Thanks so much mate! I'll be back if it doesn't, but they both look very logical. Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/#findComment-830385 Share on other sites More sharing options...
gamerzfuse Posted May 9, 2009 Author Share Posted May 9, 2009 One problem: <?php //Make first query to database $query = "SELECT * FROM vehicles"; $result = mysql_query($query) or die ("Couldn’t execute query."); //Set Variables $vehiclemodel = $vehicles->model->ViewValue; $query = "SELECT * FROM vehicles WHERE model='$vehiclemodel'"; $result = mysql_query($query) or die ("Couldn't execute query."); // Display results in a table echo "<tr><td colspan='5'></td></tr>"; while ($row = mysql_fetch_array($result)) { extract($row); //Table Contents $show_price = number_format($price); echo "<tr class='dataright'>\n <td colspan='5' class='dataleft'><a href='view.php?stock=$stock'> $year $model</a></td>\n <td>$odometer km </td>\n <td align='right'>$$show_price.00 </td>\n </tr>\n"; echo "<tr><td colspan='3'></td></tr>\n"; } ?> <!--end right column Make--> <!--start right column Model--> <tr> <td colspan="7" class="pcdrb">Other <?php echo $vehicles->make->ViewValue ?>s</td> </tr> <?php //Make first query to database $query = "SELECT * FROM vehicles"; $result = mysql_query($query) or die ("Couldn’t execute query."); //Set Variables $query = "SELECT * FROM vehicles WHERE make= '$make' AND stock != '$stock'"; $result = mysql_query($query) or die ("Couldn't execute query."); // Display results in a table echo "<tr><td colspan='5'></td></tr>"; while ($row = mysql_fetch_array($result)) { extract($row); //Table Contents $show_price = number_format($price); echo "<tr class='dataright'>\n <td colspan='5' class='dataleft'><a href='view.php?stock=$stock'> $year $model</a></td>\n <td>$odometer km </td>\n <td align='right'>$$show_price.00 </td>\n </tr>\n"; echo "<tr><td colspan='3'></td></tr>\n"; } ?> <!--end right column Model--> <!--start right column Other +-2000--> <tr> <td colspan="7" class="pcdrb">Other <?php echo $vehicles->type->ViewValue ?>s ± $2,000.00</td> </tr> <?php //Make first query to database $query = "SELECT * FROM vehicles"; $result = mysql_query($query) or die ("Couldn’t execute query."); //Set Variables $query = "SELECT * FROM `vehicles` WHERE `price` >= ($price - 2000) AND `price` <= ($price + 2000)"; $result = mysql_query($query) or die ("Couldn't execute query."); // Display results in a table echo "<tr><td colspan='5'></td></tr>"; while ($row = mysql_fetch_array($result)) { extract($row); //Table Contents $show_price = number_format($price); echo "<tr class='dataright'>\n <td colspan='5' class='dataleft'><a href='view.php?stock=$stock'> $year $model</a></td>\n <td>$odometer km </td>\n <td align='right'>$$show_price.00 </td>\n </tr>\n"; echo "<tr><td colspan='3'></td></tr>\n"; } ?> I have these three separate tables. Pricing one (last one) works great. Make (middle one) works great. As soon as I try to add the code SELECT * FROM `vehicles` WHERE `model` = '{$model}' AND `stock` != '{$stock}' to the first one, which is the Similiar Models table, all three of them break and my checkboxes later in the page also all show no results. Is using this statement for make and model both somehow causing a problem? Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/#findComment-830392 Share on other sites More sharing options...
RussellReal Posted May 9, 2009 Share Posted May 9, 2009 in the sql use $vehiclemodel instead of $model I'm assuming anyway Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/#findComment-830421 Share on other sites More sharing options...
gamerzfuse Posted May 9, 2009 Author Share Posted May 9, 2009 in the sql use $vehiclemodel instead of $model I'm assuming anyway Meaning I should be changing it in the database itself? The $model has worked everywhere else.. I'll look into it. The IE issue you helped me with is more pressing at this time. Quote Link to comment https://forums.phpfreaks.com/topic/157493-displaying-values-from-database-with-exceptionwithin-range/#findComment-830432 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.