Page 1 of 1

Switch Inside Foreach Loop

Posted: Wed Sep 14, 2005 7:50 am
by facets
Hi all,

I'm a little grief setting the statement inside the switch.
Basically if $statement = ausapapersummary.manufacturerName or ausapapersummary.cpl use the LIKE sql command
Else use standard sql command. ($statement = $field)

Can anyone shed some light?

Code: Select all

$fields = array(
    'ausapapersummary.paperCategoryId' => 'paperCategoryId',
    'ausapapersummary.manufacturerName' => 'manufacturerName',
    'ausapapersummary.cpl' => 'cpl',
    'ausapapersummary.stockId' => 'stockId',
    'ausapapersummary.adhesiveId' => 'adhesiveId',
    'ausapapersummary.linerId' => 'linerId',
    'ausapapersummary.supplierId' =>  'supplierId',
    'ausapapersummary.suitabilityFoil' => 'suitabilityFoil',
    'ausapapersummary.suitabilityYellowLight' => 'suitabilityYellowLight',
    'ausapapersummary.suitabilityLabel' => 'suitabilityLabel',
    'ausapapersummary.suitabilityOpacity' =>  'suitabilityOpacity',
    'ausapapersummary.suitabilityBronze' =>  'suitabilityBronze',
    'ausapapersummary.suitabilityScreen' =>  'suitabilityScreen',
    'ausapapersummary.suitabilityIceBucket' => 'suitabilityIceBucket',
);

foreach ($fields as $statement => $field) { if (!empty($_POST[$field]) && $_POST[$field] > 0) {     

    $action = isset($_GET['action']) ? $_GET['action'] : '';
    
    switch($action) {
    case "ausapapersummary.manufacturerName" : $query .= " AND ".$statement." LIKE '%".$_POST[$field]."%'"; break;
    case "ausapapersummary.cpl": $query .= " AND ".$statement." = ".$_POST[$field].""; break;
    default: $query .= " AND ".$statement." = ".$_POST[$field].""; break;
   }
  
  }
}

Posted: Wed Sep 14, 2005 8:18 am
by shiznatix
it looks fine to me, what exactally is not working for you there. maybe you could use a little more spacing to get things to be easlier to read

Posted: Wed Sep 14, 2005 8:20 am
by facets
I don't believe the 'case' is picking up ausapapersummary.manufacturerName

Posted: Wed Sep 14, 2005 8:54 am
by raghavan20

Code: Select all

foreach ($fields as $statement => $field) {
 if (!empty($_POST[$field]) && $_POST[$field] > 0) {      
    $action = isset($_GET['action']) ? $_GET['action'] : '';
where do you get these POST variables from???
what could be the possible values of $_POST[$field]?
can you show me the sample url you get...it would be better if you can show us samples of URL, with and without 'action' url variable.

if you are damn sure about the logic, first try whether it works with normal if...elseif..else block