Page 1 of 1

[RESOLVD] Is there a way to shorten this code up

Posted: Mon Nov 28, 2005 2:31 am
by rbpd5015
I am looking for a way to shorten a small portion of my code. I have include this entire function so you can see where I was going with it. I will however show 1st the section I hope to shorten.

Thanks in Advance,
Matt

Below is one of 11 (Just for this function if statements I am making) can I make all 11 in 1?

Code: Select all

$ploss = mysql_result($result,0,"ploss");
		if ($ploss > 0)
			{
				$ploss = $loss-$ploss;
		
			}
			else
			{
				$ploss = $loss;
	
			}

Code: Select all

function sql_insertPlayerDefenseStats($id, $year, $table)
{
	$db_table = MYSQL_DEFENSE;
	$weeklydb_table = MYSQL_GBGPASSING;

	if (!sql_emptyStats($db_table, $id, $year))
		return errorPrint("The call to sql_emptyStats() in sql_insertPlayerDefenseStats($id, $year, $table) failed");

	$tot =	$table[2];	$loss =	$table[3];	$sack = $table[4];	$ff =	$table[5];
	$frec =	$table[6];	$yds =	$table[7];	$td = $table[8];	$int =	$table[9];
	$ret =	$table[10];	$def =	$table[12];	$saft = $table[13]; $wk = $_POST["wk"];

	$query = "SELECT sum(tot) as ptot, sum(loss) as ploss, sum(sack) as psack, sum(ff) as pff, sum(frec) as pfrec, sum(yds) as pyds, sum(td) as ptd, sum(int) as pint, sum(ret) as pret, sum(def) as pdef, sum(saft) as psaft FROM $weeklydb_table WHERE id = $id AND year = $year AND wk < $wk ";
	$result = @mysql_query($query);
	 if ($result == false)
	 	{
	 	echo "No Data";
		}
		$ptot = mysql_result($result,0,"ptot");
		if ($ptot > 0)
			{
				$ptot = $tot-$ptot;
		
			}
			else
			{
				$ptot = $tot;
	
			}
			
# end here
		$ploss = mysql_result($result,0,"ploss");
		if ($ploss > 0)
			{
				$ploss = $loss-$ploss;
		
			}
			else
			{
				$ploss = $loss;
	
			}
			
# end here
		$psack = mysql_result($result,0,"psack");
		if ($psack > 0)
			{
				$psack = $sack-$psack;
		
			}
			else
			{
				$psack = $sack;
	
			}
			
# end here
		$pff = mysql_result($result,0,"pff");
		if ($pff > 0)
			{
				$pff = $ff-$pff;
		
			}
			else
			{
				$pff = $ff;
	
			}
			
# end here
		$pfrec = mysql_result($result,0,"pfrec");
		if ($pfrec > 0)
			{
				$pfrec = $frec-$pfrec;
		
			}
			else
			{
				$pfrec = $frec;
	
			}
			
# end here
		$pyds = mysql_result($result,0,"pyds");
		if ($pyds > 0)
			{
				$pyds = $yds-$pyds;
		
			}
			else
			{
				$pyds = $yds;
	
			}
			
# end here
		$ptd = mysql_result($result,0,"ptd");
		if ($ptd > 0)
			{
				$ptd = $td-$ptd;
		
			}
			else
			{
				$ptd = $td;
	
			}
			
# end here
		$pint = mysql_result($result,0,"pint");
		if ($pint > 0)
			{
				$pint = $int-$pint;
		
			}
			else
			{
				$pint = $int;
	
			}
			
# end here
		$pret = mysql_result($result,0,"pret");
		if ($pret > 0)
			{
				$pret = $ret-$pret;
		
			}
			else
			{
				$pret = $ret;
	
			}
			
# end here
		$pdef = mysql_result($result,0,"pdef");
		if ($pdef > 0)
			{
				$pdef = $def-$pdef;
		
			}
			else
			{
				$pdef = $def;
	
			}
			
# end here
		$psaft = mysql_result($result,0,"psaft");
		if ($psaft > 0)
			{
				$psaft = $saft-$psaft;
		
			}
			else
			{
				$psaft = $saft;
	
			}
			
# end here
	$query = "INSERT INTO $weeklydb_table "
	   ."(`id`, `year`, tkl, loss, sack, ff, frec, yds, td, `int`, ret, def, saft) "
	   ."SELECT id, `year`, $ptot, $ploss, $psack, $pff, $pfrec, $pyds, $ptd, $pint, $pret, $pdef, $psaft "
	   ."FROM $db_table "
	   ."WHERE id = $id AND year = $year ";  

	   
	 $result = @mysql_query($query);

	 if ($result == false)
	  return errorPrint("The following query in sql_insertPlayerWeeklyDefensiveStats($id, $year, $table); failed: <p><tt>$query</tt></p>");
	
	 return true;

}

Posted: Mon Nov 28, 2005 4:38 am
by n00b Saibot
you can write

Code: Select all

if ($ploss > 0) 
{ 
 $ploss = $loss-$ploss; 
} 
else 
{ 
 $ploss = $loss; 
}
as a simple one-liner

Code: Select all

$ploss = ($ploss > 0)?$loss-$ploss:$loss;
that itself should shorten it a lot :)