What is the proper way to iterate through multidimensional Array of POST parameters ? Example from Chrome Form Data Console below:
order[0][priority][]:1
order[0][priority][]:4
order[0][priority][]:2
order[0][priority][]:3
order[0][serviceId][]:28
order[0][serviceId][]:31
order[0][serviceId][]:29
order[0][serviceId][]:30
categoryId:5
I want to Update database table inside loop using above POST parameters.
$wpdb->query("
UPDATE $table SET order=$priority
WHERE category_id=$categoryId
AND service_id=$serviceId
");
First occurence of order[priority] is compact with first occurence of order[serviceId].
The question is how to create foreach to run UPDATE query?
Thanks !
As described in your example I think you can iterate both arrays as the same time. If "priority" and "servicesId" have the same size you can do this:
$order = $_POST['order'];
$categoryId = $_POST['categoryId'];
$size = count($order[0]["priority"]);
foreach ($i=0; $i<$size; $i++) {
$priority = $order[0]["priority"][$i];
$serviceId = $order[0]["serviceId"][$i];
$wpdb->query("
UPDATE $table SET order=$priority
WHERE category_id=$categoryId
AND service_id=$serviceId
");
}
Hope it helps!
Related
I want to use the WhereIn method in Eloquent but it now works like the below function.
<?php
$product = DB::table('product')
->join('supplier','supp_code','=','prod_supplier_code')
->select('product.*','supplier.supp_margin')
->where('prod_seo_title','=',$id)
->first();
$decArr = explode(',',$product->prod_decoration_type);
for($i=0; $i<count($decArr); $i++)
{
if($i==0)
$inString = "'".$decArr[$i]."','";
elseif ($i<(count($decArr))-1)
$inString .= $decArr[$i]."','";
else
$inString .= $decArr[$i]."'";
}
$proDeco = DB::table('decoration')->whereIn('deco_print_type', [$inString])->get();
?>
But In the query, it's displaying like this.
select * from `decoration` where `deco_print_type` in ('\'100109C9\',\'100110B9\',\'100144C9\',\'100186C9\'');
I can't understand why these slashes are coming. Please help me to fix this issue.
The whereIn() method will accept an array of all the items. In your example you are passing an array with one element with all the values already concatenated. Based on your example you only need to pass $decArr
DB::table('decoration')->whereIn('deco_print_type', $decArr)->get();
I am trying to create an ecommerce site. I have products that have different attributes (e.g. colour) and each of these needs to have their own model number and price etc,.
I have generated a form to gather this information and save it. However, I want to be able to save this info as a $_SESSION variable while users are adding products, so that they can come back to the price section and the form will be pre-populated with what they previously entered even though they haven't actually saved the product to the DB yet.
To do this I have a string that I treat as an array of items stored as a $_SESSION variable in PHP in the following format:
'item-test,100,20,20,20,20,£,1,item-test,100,20,20,20,20,£,2'
I parse this into an actual array I can deal with like so (when it gets to actually saving the product my SQL query is inside this foreach() loop):
if(isset($_SESSION['price_array'])){
$price_array = $_SESSION['price_array'];
$result = explode("item-",$price_array);
foreach($result as $item){
if(isset($item) && $item!=""){
$itemValue = explode(",",$item);
$product_model_no = $itemValue[0];
$product_value = $itemValue[1];
$product_discount = $itemValue[2];
$product_margin = $itemValue[3];
$product_shipping_domestic = $itemValue[4];
$product_shipping_other = $itemValue[5];
$product_currency = $itemValue[6];
$product_attribute = $itemValue[7];
}
}
}
So to generate this form I've another loop that goes through all of the possible attributes (not all products might come in all colours so only the ones stored in the $_SESSION apply). If I try to pre-populate this as it is my variables above only have the values for the last item in the array.
However if I nest this inside the other loop it will get the correct data but it will generate the form a number of times depending on how many items are in the array, with each iteration of the form having the values for that item in the array.
I know this is very convoluted to try explain and I can't really provide all my code because it is very complex and most of it is generated in PHP from other information in other locations.
Edit
The basics of how the form is being generated inside the other loop is as follows:
$params = [$attribute];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
foreach ($attributeResult as $value) {
for ($i = 1; $i <= 15; $i++) {
//generate form here
if($i == $product_attribute){
// pre-populate form here
}
}
}
i am using cakephp. and want to display all data in database in dropdown list. but it returns only last item. there is some problem in loop. my code is below.
foreach ($origions as $or);
$id = $or["origions"]["id"];
$orgn = $or["origions"]["origion"];
$options = array($id=>$orgn);
echo $this->Form->select('origions.origion', $options);
this display only last record in dropdown list. please help what to do that all data in table display over here.
Change it to,
$options = array();
foreach ($origions as $or){
$id = $or["origions"]["id"];
$orgn = $or["origions"]["origion"];
$options[$id] = $orgn;
}
echo $this->Form->select('origions.origion', $options);
For selected,
echo $this->Form->select('origions.origion', $options, array('value' => 'your_default_id'));
Reference.
OTHER WAY (more optimized code)...
You can set Your data $this->set from the controller.
From controller You can combine / extract / merge / sort / etc... multidimensional arrays like:
$result = Set::combine($origions, '{n}.origions.id', '{n}.origions.origion');
Read more about Cakephp SET::array functions http://book.cakephp.org/2.0/en/core-utility-libraries/set.html
I have a site developed in codeigniter where I want to retrieve comment of a specific tee.
I have a table tee like that:
- id
- user_id
- name
- created
- modified
And the table tee_comments like that:
- id
- user_id
- tee_id
- created
- modified
I have done this query:
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->from('tee');
$this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
$this->db->order_by("tee.created", "desc");
$query = $this->db->get();
With this query I retrieve two rows of tee because I have two comments in that tee.
My goal is to retrieve only one row where inside there is an array of comment like:
tee{
id,
name,
created,
modified
comment{
[0]
id,
tee_id,
comment,
created,
modified
[1]
id,
tee_id,
comment,
created,
modified
}
}
I have tried into the join:
- left
- right
- left outer
- right outer
But doesn't solve the problem, is there a way to do that?
Thanks
I love CodeIgniter! I use it constantly! You have 2 really simple options here:
One way would be to use limit.
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->join('tee_comments', 'tee_comments.tee_id = tee.id','left outer');
$this->db->order_by("tee.created", "desc");
$query = $this->db->limit(1)->get('tee');
Another way is to get first item in results Array
$query = $this->db->get();
$results = $query->result(); // gets return as an array
$row = $results[0]; // will be first row in results array
Keep in mind tho, $row will return as a object(stdClass) meaning you'll have to retrieve things from it like $row->column_name.
A handy little snippet I like to use after a call is below. It makes the row Object's Array's instead.
$results = $db->get('tee')->result(); // snippet comes after you have result array
// snippet here
foreach ($r as $k => $v) { $results[$k] = array(); foreach ($v as $kk => $vv) { $results[$k][$kk] = $vv != "NULL" ? trim($vv) : ""; } }
Use $this->db->limit(1) to retrieve a single record.
According to the accepted answer on this question, you may need to put the limit statement before the select:
$this->db->limit(1);
$this->db->select('*,tee.id as id,tee.created as created, tee_comments.id as tee_comments_id, tee_comments.created as tee_comments_created, tee_comments.modified as tee_comments_modified');
$this->db->from('tee');
Option one:
for ($i = 0; $i < count($records); $i++)
{
$data = $records[$i];
// work with date.
if($i == 1)
break;
}
Option two:
just assign the first row to var.
$row = $records['tee']['comment'][0];
I'm in a bit of pickle but the answer is probably pretty simple.
So I have my POST variable:
Array ( [accept] => accept,29 [accept1] => accept,30 [submit] => Save Selections )
That's just a print_r of $_POST.
Basically what I want to do is get the first variable, remove the 'accept,' part and store the number next to it in a variable, run some MySQL queries using that variable (containing that number) then move onto the next one; removing the 'accept,' storing the number next to it and running a query using that stored number. It want to do this for as many times is necessary to go through all the elements containing 'accept,' then a number.
Any help would be appreciated.
I was playing around with some ideas and have this code. It obviously doesn't work but perhaps I could fix and build on it?
while($i <= $elements)
{
while($x == 1)
{
$id = explode(',', next($_POST));
echo $id;
$x = 0;
}
$i++;
}
Im not sure this would fix your problem but I hope this helps and gives you an idea:
$i = 0;
while($i <= count($your_array))
{
$your_var = substr($your_array[$i], 7, 2); //get the number from your accept,29
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
UPDATE - Im not sure if this would work
foreach($your_post_array as $items)
{
$your_var = substr($items, 7, 2);
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
Maybe you should try to echo first the variables to make sure echo $your_var = substr($items, 7, 2);
I would use a foreach loop to loop through $_POST, check if the key starts with accept (stripos?), explode the value if it does and get the second value of the found array and store that somewhere for later use.
By the way, if possible I would change the front-end so that you have just one variable (an array) in $_POST you have to loop through.