Not repeating 50 lines code every time

hi all

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.
}

vineet

Write a function at one place with those 50 lines and pass the $choice as a parameter to that function

You should read some programming books on structuring your code :slight_smile:

hi Debm

can you provide any sample code for idea ?

vineet

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.
} 

Thanks spikeZ

your solutions was easy to understand. Now i will be implement this solution in my whole site and make my site code short and organised.

vineet

Off Topic:

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 :slight_smile:
As you can see, I don’t like double quotes either (:

hi Scallio

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.


( isset($extra[$choice])? (' '.$extra['choice']) : '' )

sorry i m not much experienced to understood this shorthand code.

vineet

That’s what’s called a ternary operator.
Basically, it’s an if/else statement in one line.

result = condition ? result if true : result if false;

More here: http://www.totallyphp.co.uk/using-if-else-ternary-operators

hi Immerse

thanks for the explanation.

one thing more i would like to know,

why is there space between quotation marks before $extra[‘choice’]) but there is no space between quotation marks after $extra[‘choice’]


( isset($extra[$choice])? (' '.$extra['choice']) : '' )  

vineet

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 :slight_smile:
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 :slight_smile:

Hi Scallio

thanks for clearing my doubts.

vineet