Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts

    IF, ELSEIF help needed.

    There has to be a better way to do this (not to mention that I'm getting an error).

    I am trying to get some pricing info from our DB in this fashion:
    If there is no ListPrice (that part being previous the snippet below) check for SalesHistory.
    If no sales history, then look for quote history.
    If no quote history, then look for a stockline suggested sellPrice.

    Here's what I've got right now:
    PHP Code:
    ...
    while (
    $row MySQL_fetch_assoc($results))
    {    
    // assign values to vars
        
    $pID $row["partID"];
        
        
    // Define components for use in Query
        
    $select ="partID";
        
    $from ="SOItem";
        
    $where ="SOItem.partID=$pID";
        
    $resultqryFunc($select$from$where);
        if(
    $result)
        {    
    // get min sales history x 1.2
            
    $select ="MIN(SOItem.unitAmount), MIN(SOItem.unitAmount)*2";
            
    $from ="SOItem";
            
    $where ="SOItem.partID=$pID";
            
    $resultqryFunc($select$from$where);
            
            
    $value=MySQL_fetch_assoc($results);
            echo 
    "<pre>"print_r($value); echo "</pre>";
            
        }else{
    // go into quote history loop (lowest qt $)
            
    $select ="partID)";
            
    $from ="QuoteItem";
            
    $where ="QuoteItem.partID=$pID";
            
    $resultqryFunc($select$from$where);
            if(
    $result)
            {    
    $select ="MIN(QuoteItem.price), max(QuoteItem.price)";
                
    $from ="QuoteItem";
                
    $where ="QuoteItem.partID=$pID";
                
    $resultqryFunc($select$from$where);
                
                
    $value=MySQL_fetch_assoc($results);
                echo 
    "<pre>"print_r($value); echo "</pre>";
            }
        }else{
    // go to the stockline for pricing (highest s.sellPrice)
            
    $select ="";
            
    $from ="";
            
    $where ="";
            
    $resultqryFunc($select$from$where);
            if(
    $result)
            {    
    $select ="MAX(Stockline.sellPrice), min(Stockline.sellPrice)";
                
    $from ="Stockline";
                
    $where ="Stockline.partID=$pID";
                
    $resultqryFunc($select$from$where);
                
                
    $value=MySQL_fetch_assoc($results);
                echo 
    "<pre>"print_r($value); echo "</pre>";
            }
        } 
    Is there a better way to do this rather than running a complete query under the else, then running another query in the if($result)?

    Thanks-a-bunch,
    ~ Mo

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Change the values, (assign the variables for your query), which may alter within the if/else statements and then do the query work and resulting output after the if/else clauses.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    424
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Can you provide a quick rough example?
    I'm not quite visualizing your suggestion.

    Thanks.
    ~ Mo

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Just a basic indicator of what I'm thinking. It may not fit your needs exactly.

    Code:
        $type = ''; // Add this at the top of the code block.
    
        }else{// go into quote history loop (lowest qt $)
            $type = '1';
            $select ="partID)";
            $from ="QuoteItem";
            $where ="QuoteItem.partID=$pID";
        }else{// go to the stockline for pricing (highest s.sellPrice)
            $type = '2';
            $select ="";
            $from ="";
            $where ="";
        }
    
        if ($type != '')
        {
            $result= qryFunc($select, $from, $where);
    
            if (mysql_num_rows($result))
            {
                if ($type == '1')
                {
                    [SQL code here]
                }
                else if ($type == '2')
                {
                    [SQL code here]
                }
            }
    
            $value=MySQL_fetch_assoc($results);
            echo "<pre>"; print_r($value); echo "</pre>"; 
        }
    Not a perfect example, but separates the code somewhat and has a bit less repetition. Just see what bits you are repeating frequently and try and put those parts outside of the if/else statements. My logic is a bit rusty at the moment, (I haven't touched a line of code for several weeks), but should give you an idea of what I mean.
    Last edited by MattF; 09-22-2009 at 08:19 PM.


  •  

    Posting Permissions

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