i m using the below code to display result according to $choice selected. The code displays all results fine.
But at present i m having 3 choices of prices (High, Low, Medium), for which i m repeating my “50 lines code” 3 times which is inside while loop.
As this “50 lines code” is same every time, but only the “select” query is different.
so i dont want to repeat this “50 lines code”. Whats is the solution ?
if($choice=='high')
{
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price desc";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// results will be displayed here which is approx 50 lines code.
}
if($choice=='low')
{
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price asc";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// results will be displayed here which is approx 50 lines code.
}
if($choice=='medium')
{
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id and catg = 'medium'";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// results will be displayed here which is approx 50 lines code.
}
Either write a function to output the code as Debm suggests or simply run the query/results once with a variable to hold the $sql
switch($choice) {
case 'high':
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price desc";
break;
case 'low':
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price asc";
break;
case 'medium':
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id and catg = 'medium'";
break;
default :
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price desc";
break;
}
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// results will be displayed here which is approx 50 lines code.
}
You can put multiple items together in switch, so instead of having “high” and “default” with the same action, you can just do
switch($choice) {
case 'low':
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price asc";
break;
case 'medium':
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id and catg = 'medium'";
break;
case 'high':
default :
$sql = "SELECT * FROM product_table where dealer_id=$dealer_id order by price desc";
break;
}
Anyway, I think I’d use this
$extra = array(
'low' => 'order by price asc',
'medium' => 'and catg = "medium"',
'high' => 'order by price desc'
);
$sql = 'SELECT * FROM product_table where dealer_id='.intval($dealer_id) . ( isset($extra[$choice])? (' '.$extra['choice']) : '' );
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// results will be displayed here which is approx 50 lines code.
}
But that’s just because I don’t like switch (too easy to forget a break leaving you debugging for hours on end because you don’t spot the omission …), and like arrays
As you can see, I don’t like double quotes either (:
Although you solution is much shorter and easy to manage but i would like to ask what is the below code line doing.
Also why do have space in between 2 colons before $extra after ? sign.
say we leave out the space, and have $dealer_id is 10, and $extra is “order by price asc”, the query would become
SELECT * FROM product_table where dealer_id=10order by price asc
^ note that there is no space here
This is not a valid query, since “10order” isn’t valid, and “by price asc” is also invalid, so MySQL won’t know what to do with this.
With the space added, you will get
SELECT * FROM product_table where dealer_id=10 order by price asc
^ now there is a space!
and all is well with the world
There is no space at the end because you don’t need a space at the end (that would just be a trailing space at the end of the query which doesn’t mean anything).
As an alternative you could do it like this
<?php
$sql = 'SELECT * FROM product_table where dealer_id='.intval($dealer_id) . ' ' . ( isset($extra[$choice])? $extra['choice'] : '' );
// ^ put the space here ... ^ ... instead of here
that way, it will add the space anyway, so either you get the contents of $extra[$choice] after that, or just have a trailing space, which is not a problem in itself, but I find it sloppy