Jump to content

I know there's a better way to do this...


JHolovacs

Recommended Posts

I have a search form that people can add criteria to through a multidimensional array called $critera.  Its form looks like this:

 

array(3) {
  [0]=>
  array(5) {
    ["type"]=>
    string(11) "part number"
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(11) "part number"
    ["comparison"]=>
    string(2) "IN"
    ["value"]=>
    string(1) "1"
  }
  [1]=>
  array(5) {
    ["type"]=>
    string(11) "part number"
    ["id"]=>
    string(1) "8"
    ["name"]=>
    string(11) "part number"
    ["comparison"]=>
    string(2) "IN"
    ["value"]=>
    string(1) "8"
  }
  [2]=>
  array(5) {
    ["type"]=>
    string( "property"
    ["id"]=>
    int(
    ["name"]=>
    string(4) "cost"
    ["comparison"]=>
    string(1) ">"
    ["value"]=>
    string(4) "3.99"
  }
}

 

There is a checkbox next to each criterion to allow the user to multi-delete, basically

$checked_criteria[x] = "x"

kinda format.  I wrote this script to find the checked criteria and remove them:

 

//check for removed criteria
if (isset($_REQUEST['remove_criteria'])){
$checked_criteria = $_REQUEST['checked_criteria'];
$new_criteria = array();
foreach($criteria as $key => $criterion) {
	//loop through the array of criteria, add to new criteria array
	//skip over criteria marked for delete
	$remove = 0;
	foreach ($checked_criteria as $checked_criterion) {
		if ($checked_criterion == $key) {
			$remove++;			
		}
	}

	if ($remove == 0) {
		$new_criteria[$key] = $criterion;		
	}
}
$criteria = $new_criteria;
}

 

This works; it just seems to be the wrong(and slow) way to do it.  Can someone offer a more elegant and efficient solution?

 

Link to comment
https://forums.phpfreaks.com/topic/138759-i-know-theres-a-better-way-to-do-this/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.