...

View Full Version : IF, ELSEIF help needed.



mOrloff
09-22-2009, 06:20 PM
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:

...
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";
$result= qryFunc($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";
$result= qryFunc($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";
$result= qryFunc($select, $from, $where);
if($result)
{ $select ="MIN(QuoteItem.price), max(QuoteItem.price)";
$from ="QuoteItem";
$where ="QuoteItem.partID=$pID";
$result= qryFunc($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 ="";
$result= qryFunc($select, $from, $where);
if($result)
{ $select ="MAX(Stockline.sellPrice), min(Stockline.sellPrice)";
$from ="Stockline";
$where ="Stockline.partID=$pID";
$result= qryFunc($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

MattF
09-22-2009, 06:26 PM
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.

mOrloff
09-22-2009, 08:00 PM
Can you provide a quick rough example?
I'm not quite visualizing your suggestion.

Thanks.
~ Mo

MattF
09-22-2009, 08:14 PM
Just a basic indicator of what I'm thinking. It may not fit your needs exactly.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum