Jump to content

Combining "If" statements


DBookatay

Recommended Posts

Instead of having the 2 following if statements:

	if($row['make']); switch($row['make']){
	case 'Mercedes-Benz': $row[make] = 'Mercedes'; break;
	case 'Chevrolet': $row[make] = 'Chevy'; break;
	case 'Oldsmobile': $row[make] = 'Olds'; break;
	case 'Volkswagen': $row[make] = 'VW'; break;
}
if($row['veh4']); switch($row['veh4']){
	case 'Mercedes-Benz': $row[veh4] = 'Mercedes'; break;
	case 'Chevrolet': $row[veh4] = 'Chevy'; break;
	case 'Oldsmobile': $row[veh4] = 'Olds'; break;
	case 'Volkswagen': $row[veh4] = 'VW'; break;
}

is there a way that I can combine them?

Link to comment
https://forums.phpfreaks.com/topic/66829-combining-if-statements/
Share on other sites

Here's a better solution, in my opinion, as it doesn't require you to have two different conversion lists that are identical.

 

<?php

$makeArray = (
    'Mercedes-Benz' => 'Mercedes',
    'Chevrolet' => 'Chevy',
    'Oldsmobile' => 'Olds',
    'Volkswagen' => 'VW');

if  ($row['make'] && isset($makeArray[$row['make']])) { $row['make'] = $makeArray[$row['make']]; }
if  ($row['veh4'] && isset($makeArray[$row['veh4']])) { $row['veh4'] = $makeArray[$row['veh4']]; }

?>

 

YOu could also make a function

<?php

$makeArray = (
    'Mercedes-Benz' => 'Mercedes',
    'Chevrolet' => 'Chevy',
    'Oldsmobile' => 'Olds',
    'Volkswagen' => 'VW');

function getMake($make) {
  if ($make && isset($makeArray[$make])) { $make = $makeArray[$make]; }
  return $make;
}


$row['make'] = getMake($row['make']);
$row['veh4'] = getMake($row['veh4']);

?>

All the non html code:

<?
			<?php 
				$class1 = "b"; 
				$class2 = "w"; 
				$row_count = 0; 

// Search Clauses //////////////////////////////////////////////////////////////////////////////////


// Vehicle Info
				if($_POST['vehYer']) {$where[] = "year = '{$_POST['vehYer']}'";}
				if($_POST['vehMke']) {$where[] = "make = '{$_POST['vehMke']}'";}
				if($_POST['vehMdl']) {$where[] = "model = '{$_POST['vehMdl']}'";}
				if($_POST['vehTrans']) {$where[] = "trans_type = '{$_POST['vehTrans']}'";}
				if($_POST['vin']) {$where[] = "vin = '{$_POST['vin']}'";}
				if($_POST['prc_Low']) {$where[] = "price_asking <= '{$_POST['prc_Low']}'";}
				if($_POST['prc_High']) {$where[] = "price_asking <= '{$_POST['prc_High']}'";}
				if($_GET['status']) {$where[] = "status = '{$_GET['status']}'";}
				if($_GET['cat']) {$where[] = "category = '{$_GET['cat']}'";}

// Customer Info
				if($_POST['pri_nmF']) {$where[] = "pri_nmF LIKE '%{$_POST['pri_nmF']}%'";}
				if($_POST['pri_nmL']) {$where[] = "pri_nmL LIKE '%{$_POST['pri_nmL']}%'";}

// Deal Info
				if($_GET['salesman']) {$where[] = "salesman = '{$_GET['salesman']}'";}
				if($_GET['dealtype']) {$where[] = "dealtype = '{$_GET['dealtype']}'";}
				if($_GET['account']) {$where[] = "status = '{$_GET['account']}'";}


// Search Contacts
				if($_GET['letter']) {$where[] = "name like '{$_GET['letter']}%'";}


////////////////////////////////////////////////////////////////////////////////////////////////////

				$where[] = "status != 'Returned'";
				if(!empty($where)) {$where = 'where ' . implode(' and ', $where);}

                	$query = "SELECT * FROM $table $where ORDER BY $order";
				$result = mysql_query($query); if($result) {
				$numrows = mysql_num_rows($result); 
				while($row = mysql_fetch_array($result)){ 

                	$class = ($row_count % 2) ? $class1 : $class2; 

				if($row['status']); switch($row['status']){
					case 'Active': $span = ''; $stock = "<a href=\"http://www.carcityofdanbury.com/inventory_view.php?stock={$row['stock']}\" target=\"_blank\">{$row['stock']}</a>"; break;
					case 'Pending': $span = 'Pink'; $stock = "{$row['stock']}"; break;
					case 'Sold': $span = 'Green'; $stock = "{$row['stock']}"; break;
					case 'Lot2': $span = 'Red'; $stock = "{$row['stock']}"; break;
				}


// REMOVE ONCE FORM_CONTENT.PHP WORKS PROPERLY



$makeArray = (
    'Mercedes-Benz' => 'Mercedes',
    'Chevrolet' => 'Chevy',
    'Oldsmobile' => 'Olds',
    'Volkswagen' => 'VW');

if  ($row['make'] && isset($makeArray[$row['make']])) { $row['make'] = $makeArray[$row['make']]; }
if  ($row['veh4'] && isset($makeArray[$row['veh4']])) { $row['veh4'] = $makeArray[$row['veh4']]; }




if($row['model']); switch($row['model']){
	case 'Grand Cherokee': $row[model] = 'G. Cherokee';	break;
	case 'Cutlass Supreme': $row[model] = 'Cutlass';	break;
}
if($row['veh5']); switch($row['veh5']){
	case 'Grand Cherokee': $row[model] = 'G. Cherokee';	break;
	case 'Cutlass Supreme': $row[model] = 'Cutlass';	break;
}

if($row['trim']); switch($row['trim']){
	case 'N / A': $row[trim] = ''; break;
	case 'Limited':	$row[trim] = 'Lmtd'; break;
	case 'Laramie SLT':	$row[trim] = 'SLT'; break;
	case 'Cheyenne SLT': $row[trim] = ''; break;
	case 'Cheyenne': $row[trim] = ''; break;
	case 'Super Duty': $row[trim] = 'SD'; break;
	case 'Sport': $row[trim] = ''; break;
	case 'XLT Super Duty': $row[trim] = 'XLT'; break;
	case 'Signature Series': $row[trim] = ''; break;
	case 'Silverdao': $row[trim] = ''; break;
	case 'Outback':	$row[trim] = ''; break;
	case 'Eddie Bauer':	$row[trim] = ''; break;
	case 'Presidental':	$row[trim] = ''; break;
	case 'Kompressor':	$row[trim] = ''; break;
}
if($row['body']); switch($row['body']){
	case 'SUV': $row[body] = ''; break;
	case 'Sedan': $row[body] = ''; break;
	case 'Coupe': $row[body] = ''; break;
	case 'Hatchback': $row[body] = 'Hatch'; break;
	case 'Convertible': $row[body] = 'Conv.'; break;
	case 'Cargo Van': $row[body] = 'Cargo'; break;
	case 'Mini Van': $row[body] = ''; break;
	case 'Reg. Cab': $row[body] = ''; break;
	case 'Ext. Cab': $row[body] = 'XCab'; break;
	case 'Quad Cab': $row[body] = '4dr'; break;
}
    $vehicle = $row['year'].' '.$row['make'].' '.$row['model'].' '.$row['trim'].' '.$row['body'];
if ($row['vin']) {$vin_num = ($row['vin']);} else {$vin_num = " ";} $vin_num = trim($vin_num); $vin_trim = substr($vin_num,strlen($vin_num)-6,6);	$vin = "<a href=\"javascript:openWin('include/page_files/enlarge.php?category={$_GET['category']}&body=VIN&stock=$row[stock]','600','120','no','no');\">$vin_trim</a>";

if(isset($row['dealtype'])); switch($row['dealtype']){
	case 'AL': $deal = 'AL'; break;
	case 'Citi': $deal = 'Citi'; break;
	case 'UCF': $deal = 'UCF'; break;
	case 'CF': $deal = 'CF'; break;
	case 'Cash': $deal = '$$'; break;
	case 'IH': $deal = 'IH'; break;
	case 'Other': $deal = 'Other'; break;
	case '': $deal = $NOF; break;
}

if(isset($row['salesman'])); switch($row['salesman']){
	case 'Brad': $slsman = 'BG'; break;
	case 'BG': $slsman = 'BG'; break;
	case 'Doug': $slsman = 'DD'; break;
	case 'DD': $slsman = 'DD'; break;
	case 'Charles': $slsman = 'CG'; break;
	case 'CG': $slsman = 'CG'; break;
	case 'Christine': $slsman = 'CC'; break;
	case 'CC': $slsman = 'CC'; break;
	case 'Jennifer': $slsman = 'JL'; break;
	case 'JL': $slsman = 'JL'; break;
	case 'Jerry': $slsman = 'JF'; break;
	case 'JF': $slsman = 'JF'; break;
	case 'Joe': $slsman = 'JC'; break;
	case 'JC': $slsman = 'JC'; break;
	case 'Dan': $slsman = 'DF'; break;
	case 'DF': $slsman = 'DF'; break;
	case 'Auction': $slsman = 'Auction'; break;
	case '': $slsman = $NOF; break;
}

// --------------------------------------------------------------------- //

				if ($row['mileage']) {$mileage = $row[mileage]; $miles = floor($mileage/1000);} else {$miles = $NOF;}
				if ($row['color_exterior']) {$color = ($row['color_exterior']);} else {$color = $NOF;}
				if($row['trans_type']); switch($row['trans_type']){case 'Automatic': $trans = 'A'; break; case 'Manual': $trans = 'M'; break;}
				if ($row['price_asking']) {$asking = "$" . number_format($row['price_asking']);} else {$asking = $NOF;}
				$cost = $row[pur_price] + $row[pur_del_cost] + $row[pur_reprs]; if ($cost == "0") {$cost = $NOF;} else {$cost = "$" . number_format($cost);}
				if ($row['sold_dte1']) {$dte1 = $row['sold_dte1'];} else {$dte1 = $NOF;} if ($row['sold_dte2']) {$dte2 = $row['sold_dte2'];} else {$dte2 = $NOF;} $date = $dte1 . " / " . $dte2 . " / " . $row['sold_dte3'];

				if(isset($row['saletype'])); switch($row['saletype']){
					case 'Single': $customer = $row['pri_nmL'].', '.$row['pri_nmF']; break;
					case 'Co': $customer = $row['pri_nmL'].' & '.$row['co_nmL']; break;
					case 'Business': $customer = $row['biz_nm']; break;
					case '': $customer = $row['pri_nmL'].', '.$row['pri_nmF']; break;
				}

// Application
				$id = $row['id'];
				if ($row['ap_nmM']) {$primary = $row['ap_nmF'] . ' ' . $row['ap_nmM'] . ' ' . $row['ap_nmL'];} else {$primary = $row['ap_nmF'] . ' ' . $row['ap_nmL'];}
			    if ($row['veh3']) {$veh = $row['veh3'] . ' ' . $row['veh4'] . ' ' . $row['veh5'];} else {$veh = 'None Specifed';}
				$submitted = substr($row['submitted'], 5, 2) . '/ ' . substr($row['submitted'], 8, 2) . '/ ' . substr($row['submitted'], 2, 2);
				if ($row['apScore']) {$crdScre = '<span class="red"> ('. $row['apScore'] .')</span>';} else {$crdScre = '   '.$NOF;}
				$type = $row['category'];
				if($row['Citi']); switch($row['Citi']){case 'Approved': $Citi = "<img src=\"images/Icons/Apps/yes_Citi.gif\" />"; break; case 'Declined': $Citi = "<img src=\"images/Icons/Apps/no_Citi.gif\" />"; break; case '': $Citi = "<img src=\"images/Icons/Apps/pending_Citi.gif\" />"; break; case 'Needs Co': $Citi = "<img src=\"images/Icons/Apps/co_Citi.gif\" />"; break;}
				if($row['UCF']); switch($row['UCF']){case 'Approved': $UCF = "<img src=\"images/Icons/Apps/yes_UCF.gif\" />"; break; case 'Declined': $UCF = "<img src=\"images/Icons/Apps/no_UCF.gif\" />"; break; case '': $UCF = "<img src=\"images/Icons/Apps/pending_UCF.gif\" />"; break; case 'Needs Co': $UCF = "<img src=\"images/Icons/Apps/co_UCF.gif\" />"; break;} if ($row['ap_add3'] == "NY") {$UCF = "<img src=\"images/Icons/Apps/no_UCF.gif\" />";} else {$UCF = $UCF;}
				if($row['AL']); switch($row['AL']){case 'Approved': $AL = "<img src=\"images/Icons/Apps/yes_AL.gif\" />"; break; case 'Declined': $AL = "<img src=\"images/Icons/Apps/no_AL.gif\" />"; break; case '': $AL = "<img src=\"images/Icons/Apps/pending_AL.gif\" />"; break;}

// In House
				$acnt_nme = $row['pri_nmL'].', '.$row['pri_nmF'];
				if ($row['pymtLastDte'] == "0000-00-00") {$pymt_dte = ' ';} else {$pymt_dte = substr($row['pymtLastDte'], 5, 2) . '/ ' . substr($row['pymtLastDte'], 8, 2) . '/ ' . substr($row['pymtLastDte'], 2, 2);}





				if(isset($_GET['category'])); switch($_GET['category']) {

echo $page_body;

?>

LOL, I guess it would be helpful if I used "array" when creating an array! That was my fault. Now that I see all of your code however, I would definitely suggest taking ALL of your "conversions" and creating arrays of them in the manner I described. Then put all of those arrays into an included conversion file. Then you can simply call a function for every value you need to convert. This will make your code much more manageable and cleaner:

 

 

Here is an example using some of the lists you have:

 

using the function I have provided your conversion arrays do not need to include converting a value to itself (e.g. 'DD'=> 'DD'). The function will first determine if the value exists as a key in the array, if so it returns the corresponding value. Ff that fails and the value is null it will return the $null value passed to the function if one was given. If neither of those two scenarios is true then the original value is returned.

conversions.php

<?php

$makeArray = array (
    'Mercedes-Benz' => 'Mercedes',
    'Chevrolet' => 'Chevy',
    'Oldsmobile' => 'Olds',
    'Volkswagen' => 'VW');

$salesmanArray = array (
    'Brad'=> 'BG',
    'BG'=> 'BG', //Not needed
    'Doug'=> 'DD',
    'DD'=> 'DD', //Not needed
    'Charles'=> 'CG',
    'CG'=> 'CG', //Not needed
    'Christine'=> 'CC',
    'CC'=> 'CC', //Not needed
    'Jennifer'=> 'JL',
    'JL'=> 'JL', //Not needed
    'Jerry'=> 'JF',
    'JF'=> 'JF', //Not needed
    'Joe'=> 'JC',
    'JC'=> 'JC', //Not needed
    'Dan'=> 'DF',
    'DF'=> 'DF', //Not needed
    'Auction'=> 'Auction'); //Not needed

$dealtypeArray = array (
    'AL'=> 'AL', //Not needed
    'Citi'=> 'Citi', //Not needed
    'UCF'=> 'UCF', //Not needed
    'CF'=> 'CF', //Not needed
    'Cash'=> '$$',
    'IH'=> 'IH', //Not needed
    'Other'=> 'Other'); //Not needed

?>

 

Test page

<?php

include ("conversions.php");

//Set some values for testing
$row['make'] = "Chevrolet";
$row['veh4'] = "Volkswagen";
$row['dealtype'] = "Cash";
$row['salesman'] = "";
$NOF = "N/A";


function abbreviate($value, $array, $null='') {
  if (isset($array[$value])) {
    $value = $array[$value];
  } else if ($value=='') {
    $value = $null;
  }
  return $value;
}

echo "Make: " . abbreviate($row['make'], $makeArray) . "<br>";
echo "Veh4: " . abbreviate($row['veh4'], $makeArray) . "<br>";
echo "Deal Type: " . abbreviate($row['dealtype'], $dealtypeArray, $NOF) . "<br>";
echo "Salesman: " . abbreviate($row['salesman'], $salesmanArray, $NOF) . "<br>";

// Output:
// Make: Chevy
// Veh4: VW
// Deal Type: $$
// Salesman: N/A

?>

 

Also, instead of doing something like this:

 

$row['make'] = abbreviate($row['make'], $makeArray);

 

and using that variable when creating your HTML, I would suggest just using that function when creating your HTML. Like this:

 

$page_body .= "The make you have chosen is " . abbreviate($row['make'], $makeArray);

 

Hope this helps you.

One other suggestion would be to make the keys in the arrays all upper case (or lower case).

 

Then you can change the function to this:

 

  if (isset($array[strtoupper($value)])) {

 

That will add a little more flexibility to the function so "Chevrolet" and "chevrolet" result in the same output: "Chevy"

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.