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?

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

		}
		else
		{
			$ploss = $loss;

		}
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;

}

    :eek: That's A LOT of redundancy!

    I re-worked your code flow using variable variables.
    Let me know if it works!

    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 tot, sum(loss) as loss, sum(sack) as sack, sum(ff) as ff, sum(frec) as frec, sum(yds) as yds, sum(td) as td, sum(int) as pint, sum(ret) as ret, sum(def) as def, sum(saft) as saft FROM $weeklydb_table WHERE id = $id AND year = $year AND wk < $wk ";
    if(!$result = @mysql_query($query)){
         echo "No Data";
    } else {
      $row = @mysql_fetch_array($result);
      foreach ($row as $key => $val) {
        $$key = ($val > 0 ? $$key - $val : $$key);
      }
    }
    
    $query = "INSERT INTO $weeklydb_table "
       ."(`id`, `year`, tkl, loss, sack, ff, frec, yds, td, `int`, ret, def, saft) "
       ."SELECT id, `year`, $tot, $loss, $sack, $ff, $frec, $yds, $td, $int, $ret, $def, $saft "
       ."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;
    
    }
    

      Unless $ptot, $ploss, and so on can be negative, then the whole if..else thing is redundant.

      if ($ptot > 0)
                  {
                      $ptot = $tot-$ptot;
      
              }
              else
              {
                  $ptot = $tot;
      
              }

      can be shortened to

      $ptot = $tot-$ptot;

      (since $tot-$ptot==$tot if $ptot==0).

      Your insert query doesn't actually select anything worthwhile from $db_table: the id and year fields are selected, but you already know what they are because you used them in the WHERE clause. All the other fields are just straight numbers. The only effect $db_table has therefore is to cause the query to fail if there were no records for that combination of id and year.

      It might be possible to simplify it down to a single update query; but it's a bit hard to say since without knowing more about what the code is supposed to be doing it's hard to see why you're taking a bunch of rows from a table, adding their fields, and putting the resulting sums back into the same table (incidentally, the insert query doesn't mention the 'wk' field - does this have a default value?)

      Incidentally, you might want to check that $_POST['wk'] really is a week number before sticking it in the query. Much Nastiness is possible if you don't.

        // @ Weedpacket : just a remark, i didnt want to offend u
        what if $ptot < 0 , then that would be wrong,
        i think u cannot shorten this ....

          Thanks guys I resolved this 🙂!!!!!!!

          Matt

            kakki wrote:

            // @ Weedpacket : just a remark, i didnt want to offend u
            what if $ptot < 0 , then that would be wrong,
            i think u cannot shorten this ....

            Quite all right. The first line of my post reads

            Weedpacket wrote:

            Unless $ptot, $ploss, and so on can be negative....

              Write a Reply...