1

My function receives a array as parameter:

array(6) {
  [0]=>
  string(7) "usuario"
  [1]=>
  string(4) "john"
  [2]=>
  string(5) "senha"
  [3]=>
  string(40) "7c4a8d09ca3762af61e59520943dc26494f8941b"
  [4]=>
  string(9) "pessoa_id"
  [5]=>
  string(1) "2"
}

What I need:

SELECT * FROM (`funcionarios`) WHERE `usuario` = 'john' AND `senha` = '7c4a8d09ca3762af61e59520943dc26494f8941b' AND `pessoa_id` = '2'

I need to create a WHERE with it, I'm using CodeIgniter and I came to this stupid but working solution:

foreach ($params as $x) {
    if ($pos % 2 == 0)
        $chave = $x;
    else
        $valor = $x;
    $pos++;
    if ($pos % 2 == 0)
        $this->db->where($chave, $valor);
    }

I'm trying to find something more developer-friendly because there will be another person using this code.

What is the best way to do this?

1
  • If you have any control over the array that gets passed in, do yourself a favor and turn it into an associative array. Commented Mar 9, 2012 at 20:08

5 Answers 5

2

I think this it the best way if you can't change that array. IF you can, do so now, because it's really inelegant

$data = array(
   "usuario" => "john",
   "senha" => "7c4a8d09ca3762af61e59520943dc26494f8941b",
   "pessoa_id" => 2
);

foreach($params as $x => $y) {
      $this->db->where($x, $y);
}
Sign up to request clarification or add additional context in comments.

3 Comments

yeah..I know..I'm reusing some code..if I change it, I'll have to change a lot of files...but if this solution I gave is the best...I'll change all =)
@Gerep yeah, absolutely worth it.
@Gerep you shouldn't be reusing code. That's what are functions for
2

If you can change the format of the array, just use an associative array. For example:

array(

    "usuario" => "john" //etc.

);

Then you can take advantage of the key function in PHP to avoid your even index checking in the loop, or even change your loop to:

foreach ($params as $key => $value)

2 Comments

Yeah...I can do that but as I said to @Martin, I'll a lot of work doing that..but if changing the array is the best solution I'll do it
@Gerep I agree with Martin. completely - use an associative array format and change it ASAP.
1
$query = "SELECT * FROM `funcionarios` WHERE ";
$wheres = array();

foreach ($paramas as $key => $val) {
    $wheres[] = "`$key`='$val'";
}

$query .= implode(' AND ', $wheres); // $query is now "SELECT * FROM `funcionarios` WHERE `myKey`='myValue' AND `otherkey`='otherval'";

That should work

Comments

1

Generate a new proper associative array from the original. Then it's much easier.

$arr = array("key1", "value1", "key2", "value2", "key3", "value3");
$newArr = array();
for ($i = 0; $i < count($arr); $i += 2) {
    $newArr[$arr[$i]] = $arr[$i + 1];
}

Then using foreach you can build your query.

3 Comments

Thanks but I'll use Martin and Evan solution, thanks anyway ;)
@Gerep I assumed you couldn't change the array, but if you can by all means do.
I can change but it will be a lot of work..so I was trying to avoid it.
0

If you aren't in control of the input structure, convert the indexed array into an associative array which where() will happily consume.

$this->db->where(
    array_column(array_chunk($params, 2), 1, 0)
);

If you are able to format your array as an associative array earlier, then just feed $params directly into where().

$this->db->where($params);

It is not necessary to make iterated calls of the where() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.