SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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 ?

    PHP Code:
    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

  2. #2
    SitePoint Member
    Join Date
    Aug 2011
    Location
    India
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Debm452 View Post
    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
    hi Debm

    can you provide any sample code for idea ?

    vineet

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    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
    Code:
    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.
    }
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

    Quote Originally Posted by spikeZ View Post
    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
    Code:
    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.
    }

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    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

    PHP Code:
    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

    PHP Code:
    $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
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post

    PHP Code:
    $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
    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.

    PHP Code:
    ( isset($extra[$choice])? (' '.$extra['choice']) : '' 
    sorry i m not much experienced to understood this shorthand code.

    vineet

  8. #8
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    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...nary-operators

  9. #9
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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']
    PHP Code:
    ( isset($extra[$choice])? (' '.$extra['choice']) : '' 
    vineet

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,891
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    say we leave out the space, and have $dealer_id is 10, and $extra is "order by price asc", the query would become

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

    Code:
    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 Code:
    <?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
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  11. #11
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    619
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Scallio

    thanks for clearing my doubts.

    vineet


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •