Jump to content

[SOLVED] Where and how for IF?


Shamrox

Recommended Posts

Here's the sql statement

mysql_select_db($database_spectrum, $spectrum);
$select = 'SELECT `spec_course`.`ctitle`, `spec_course`.`course_num`, `spec_client`.`name`,  `spec_contacts`.`lastname`, `spec_contacts`.`firstname`,  `spec_registrar`.* ';  																			$from  = ' FROM spec_registrar';
$from .= '	INNER JOIN `spec_course` ON `spec_registrar`.`coursecode` = `spec_course`.`course_num`';
$from .= '	INNER JOIN `spec_students` ON `spec_registrar`.`studentid` = `spec_students`.`studentid`';
$from .= '	INNER JOIN `spec_client` ON `spec_registrar`.`clientid` = `spec_client`.`clientid`';
$from .= ' INNER JOIN `spec_contacts` ON `spec_students`.`contactid` = `spec_contacts`.`contactid`';
$where = ' WHERE 1=1';
$orderby = ' ORDER BY `spec_registrar`.`orderdate` ASC';
$clientid = $_POST['clientid'];
if ($clientid != '') {
$where .= " AND `spec_registrar`.`clientid`='$clientid'";
}
$orderdateA = $_POST['orderdateA'];
if ($orderdateA != '') {
$where .= " AND `spec_registrar`.`orderdate` BETWEEN '$orderdateA'";
}
$orderdateB = $_POST['orderdateB'];
if ($orderdateB !='') {
$where .= " AND '$orderdateB'";
}

 

Here's where it's presented

 <?php 
$orders = @mysql_query($select . $from . $where . $orderby);
if (!$orders) {
echo '</table>';
exit();
}
while ($order = mysql_fetch_array($orders)) {
$orderdate = $order['orderdate'];
$lastname = $order['lastname'];
$firstname = $order['firstname'];
$course_num = $order['course_num'];
$ctitle = substr($order['ctitle'], 0,30);
$vendorname = $order['vendorname'];
$startdate = $order['startdate'];
$enddate = $order['enddate'];
$courselength = $order['courselength'];
$purchaseorder = $order['purchase_order'];
$costcenter = $order['cost_center'];
$listprice = $order['vendorlistprice'];
$salesprice = $order['sale_price'];
$savings = $order['vendorlistprice']-$order['sale_price'];
$cogs = $order['cogs'];
$profit = $order['sale_price']-$order['cogs'];
$status = $order['status'];
echo "<tr>\n";
echo "<td nowrap='nowrap'>$orderdate</td>\n";
echo "<td nowrap='nowrap'>$lastname, $firstname</td>\n";
echo "<td nowrap='nowrap'>$course_num</td>\n";
echo "<td nowrap='nowrap'>$ctitle</td>\n";
echo "<td nowrap='nowrap'>$vendorname</td>\n";
echo "<td nowrap='nowrap'>$startdate</td>\n";
echo "<td nowrap='nowrap'>$enddate</td>\n";
echo "<td nowrap='nowrap'>$courselength</td>\n";
echo "<td nowrap='nowrap'>$purchaseorder</td>\n";
echo "<td nowrap='nowrap'>$costcenter</td>\n";
echo "<td nowrap='nowrap'>\$$listprice</td>\n";
echo "<td nowrap='nowrap'>\$$salesprice</td>\n";
echo "<td nowrap='nowrap'>\$$savings</td>\n";
echo "<td nowrap='nowrap'>\$$cogs</td>\n";
echo "<td nowrap='nowrap'>\$$profit</td>\n";
echo "<td nowrap='nowrap'>$status</td>\n";
echo "</tr>\n";
$total_listprice += $listprice;
$total_salesprice += $salesprice;
$total_savings += $savings;
$total_cogs += $cogs;
$total_profit += $profit;      //etc
}

 

Question: I have a column called Savings that shows on the report that is generated above. Savings is based on the listprice - the saleprice. Well, in cases where the saleprice is $0, it shows a 'savings' of the entire list price. I'd like to somehow work in a conditional statement that would say, IF the saleprice = $0, then the savings would be $0, otherwise do the normal calculation of listprice - saleprice. I'm not sure where this should be done and how the code should be crafted. Thanks in advance.

Link to comment
Share on other sites

Yeah, you could use an if statment, or you can use the ternary operator. Replace this line:

$savings = $order['vendorlistprice']-$order['sale_price'];

 

With:

$savings = ($order['sale_price']==0)? 0 :$order['vendorlistprice']-$order['sale_price'];

 

Edit: Perhaps i ought to explain how it works. The ternary operator takes the following form:

 

condition ? value if true : value if false

 

Therefore, in the baove example, we first test to see if the sale price is 0. If it is, we assign 0 to $savings. If its not, we perform the normal calculation and assign this value to $savings.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.