Tuesday, October 12, 2010

solving the packaging problem in PHP

When, I was developing a shipping software based on Fedex ans UPS APIs, I got an issue with packagaging. It was, the packing the products in an optimised way. there are few methods to solve this issues in mathematics. Here's a program which can be used to solve the packaging problem.



   // setting up Max value for each contatiner

    DEFINE('MAX_VALUE',40);

   //   different set of array values for testing

    //$input = array(18,13,33,7,26,39,16,5);
    //$input = array(15,28,19,21,4,36,12,15);

    $input = array(1,2,3,4,5,30,6,7,8,9,10,1,2,3,4,5,30,6,7,8,9,10);

    //$input = array(15,15,15,15,15,10,10,25);

    $expected_containers = ceil(array_sum($input)/MAX_VALUE);
    $no_of_items = count($input);
    rsort($input);

    for($i = 0; $i < $no_of_items; $i++)
   {
        if(isset($input[$i]))
       {
            $remainder = MAX_VALUE - $input[$i];
            for($j = $i + 1; $j <= $no_of_items; $j++)
           {
                if(isset($input[$j]) && $input[$j] <= $remainder)
                {
                    if(!isset($container[$i]))
                   {
                        $container[$i] = array($input[$i],$input[$j]);
                    }
                     else
                    {
                        array_push($container[$i],$input[$j]);
                    }
                    $remainder = $remainder - $input[$j];
                    unset($input[$j]);
                }
                if(!isset($container[$i]))
                {
                    $container[$i] = array($input[$i]);
                }
            }
        }
    }

    echo "Expected containers ".$expected_containers."
";
    echo "Actual containers ".count($container)."
";
    echo '
';

    print_r($container);

    foreach($container as $val)
   {
 $productsum = array_sum($val);
 echo  "
".$productsum;
   } 

   echo '
';
?>




seems helpful someone, who is developing a shipping software.

@credits Praveesh

Friday, September 10, 2010

Handling the multiple checkboxes with Jquery for Check All, Uncheck All and get values from selected values V2

Handling the multiple checkboxes with jquery for Check All, Uncheck All
and get values from selected values. V2 For new versions of Jquery.


include jquery

<script language="JavaScript" src="youserveraddres/js/jquery.js"></script>


your html

<input name="phpqa[]" type="checkbox" value="1">1
<input name="phpqa[]" type="checkbox" value="2">2
<input name="phpqa[]" type="checkbox" value="3">3
<input name="phpqa[]" type="checkbox" value="4">4
<input name="phpqa[]" type="checkbox" value="5">5
<input name="phpqa[]" type="checkbox" value="6">6
<input name="phpqa[]" type="checkbox" value="7">7
<input name="phpqa[]" type="checkbox" value="8">8

<a href="javascript:void();" onclick="markAll()">Check All</a>
<a href="javascript:void();" onclick="unMarkAll()">Uncheck All</a>
<a href="javascript:void();" onclick="getSelectedVals()">Get Checked Values</a>


// function for mark all / check all

function markAll(){
$("input[name='phpqa[]']").each(function() {
this.checked = true;
});
}


// function for uncheck all

function unMarkAll(){
$("input[name='phpqa[]']").each(function() {
this.checked = false;
});
}


//fetching selected values / checked values

function getSelectedVals(){
$("input[name='phpqa[]']").each(function() {
if ($(this).attr('checked'))
{
alert($(this).val());
}
});
}