1

I have an array like this

Array ( [12313] => 1 [12312] => 1 ) 1

The array keys are the items that exist in the warehouse and values are the number of items.

I would like to check if the items and amounts actually exist in the warehouse database using php. I thought maybe I could use a foreach for each item check but I don't know how to use foreach. I would like to make a query like:

$query=$this->db->query("SELECT COUNT(*) as amount FROM warehouse where item=12313");
$check=$query->row_array();
$amt=$check['amount'];
if($amt>0){
   return true;
}
else{
   return false;
}
3
  • are those solution really works for you? make value of 12312=>0 and check what happens Commented Jan 12, 2015 at 19:04
  • all the solution checks only the first item has minimum one data Commented Jan 12, 2015 at 19:28
  • I will replace inner codes with working code. The code in the question is just a description. The main solution i would like to get is how to use key and value of array in foreachloop.:) Commented Jan 14, 2015 at 6:35

4 Answers 4

3
$array=array ( '12313' => 1,'12312' => 1 );
foreach($array as $k=>$val)
{
  $query=$this->db->query("SELECT COUNT(*) as amount FROM warehouse where item='".$k."'");
  $check=$query->row_array();
  $amt=$check['amount'];
  if($amt>0){
    return true;
  }
  else{
   return false;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your query should be encapsulated with Double quotes....And wrap $k with single quote bcz if it will work for both string as well as int.... and while selecting metion amount not all *
why the foreach loop?it will query only for 12312 and return true or false.will not do anything for 12312
1

Are you looking for something like:

$items = array(
        12313 => 1,
        12312 => 1,
    );

foreach ( $items as $key => $value ) : //iterate over array
    $query = "SELECT COUNT(*) as amount FROM warehouse where item = $key";
    $check = $query->row_array();
    $amt = $check['amount'];
    if( $amt>0 ) {
        return true;
    }
    else {
        return false;
    }

endforeach;

Comments

0

try this

$arr = array("[12313]" => "1" ,"[12312]" => "2");
foreach ($arr as  $key=>$value) {
$query="SELECT COUNT(*) as amount FROM warehouse where item='".$key."'";
$check=$query->row_array();
$amt=$check['amount'];
if($amt>0){
return true;
}
else{
return false;
}
}

1 Comment

@user3789191 let me know if it is working for you :)
0
  $array = array('12313' => 1, '12312' => 1);
            foreach ($array as $key => $value) {

                $this->db->select('COUNT(*) as amount');
                $this->db->from('warehouse');
                $this->db->where('item', $key);
                $result = $this->db->get()->row_array();

                return ($result['amount'] >= $value) ? TRUE : FALSE; //match no of items
             // return ($result['amount'] > 0) ? TRUE : FALSE; //OR if just check the count
            }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.