Switch Inside Foreach Loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Switch Inside Foreach Loop

Post 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;
   }
  
  }
}
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

I don't believe the 'case' is picking up ausapapersummary.manufacturerName
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
Post Reply