I got this:
(I am getting the current calender week and the starting day of the week and the ending day of the week)
# DB
$db = DBWrapper::getInstance();
$table = "staff";
$columns = array("id", "firstname", "lastname","categorie_bit","calender_week");
$whereDish = array('categorie_bit = :idDish');
$valuesDish = array('idDish' => 10);
$set = array(
'calender_week' => $newdate,
'start_week' => $startWeek,
'end_week' => $endWeek,
);
$whereWeek = array('id = :id');
echo '<br>'.$db->update($table, $set, $whereWeek, $valuesWeek).'<br>';
how can I make the where statement for the whole ids in the database.
It currently updates one row.
I could write something like this:
$valuesWeek_1 = array('id' => 1);
$valuesWeek_2 = array('id' => 2);
$valuesWeek_3 = array('id' => 3);
$valuesWeek_4 = array('id' => 4);
$valuesWeek_5 = array('id' => 5);
$valuesWeek_6 = array('id' => 6);
$valuesWeek_7 = array('id' => 7);
$valuesWeek_8 = array('id' => 8);
$valuesWeek_9 = array('id' => 9);
But that is not a good solution
you may try something like:
$whereDish = array('categorie_bit IN :idsDish');
$valuesDish = array('idsDish' => array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
Related
I have an array like the following:
$quantity = explode(",", $dataProducts["quantityArray"]);
$valuePrice = explode(",", $dataProducts["valuePriceArray"]);
$productsId = explode(",", $dataProducts["productsIdArray"]);
for($i=0;$i<count($productsId);$i++){
$products = array('id' => $productsId[$i],
'price' => $valuePrice[$i],
'quantity' => $quantity[$i]);
}
Suppose the vector is composed of 4 products with their id, prices and quantities. (Previously I check that the array is properly armed)
$products[0] = ['id' => 4, 'price' => 20, 'quantity' => 2]
$products[1] = ['id' => 10, 'price' => 100, 'quantity' => 5]
$products[2] = ['id' => 15, 'price' => 40, 'quantity' => 4]
$products[3] = ['id' => 20, 'price' => 50, 'quantity' => 3]
And I'm passing it as a parameter to the url of 'success'. But when the url is generated, only the first index of the array arrives.
$products= http_build_query($products);
#Configure the url of response for user
$preference->back_urls = array(
"success" => "{$url}/index.php?route=profile&data=".$products,
"failure" => "{$url}/index.php?route=error",
"pending" => "{$url}/index.php?ruta=pending"
);
Example of generated url, with only the first index of the array:
https://www.webpage.com/index.php?route=profile&data=id=4&price=20&quantity=2
What am I doing wrong?
In order to append an item to an array, use the following syntax: $array[] = $value
In your example:
for($i=0; $i<count($productsId); $i++){
$products[] = array(
'id' => $productsId[$i],
'price' => $valuePrice[$i],
'quantity' => $quantity[$i]
);
}
This:
$products[0] = ['id' => 4, 'price' => 20, 'quantity' => 2];
$products[1] = ['id' => 10, 'price' => 100, 'quantity' => 5];
$products[2] = ['id' => 15, 'price' => 40, 'quantity' => 4];
$products[3] = ['id' => 20, 'price' => 50, 'quantity' => 3];
$str = http_build_query($products);
echo $str . PHP_EOL;
Generates this:
0%5Bid%5D=4&0%5Bprice%5D=20&0%5Bquantity%5D=2&1%5Bid%5D=10&1%5Bprice%5D=100&1%5Bquantity%5D=5&2%5Bid%5D=15&2%5Bprice%5D=40&2%5Bquantity%5D=4&3%5Bid%5D=20&3%5Bprice%5D=50&3%5Bquantity%5D=3
If you are looking for output like this:
id=4&price=20&quantity=2&id=10&price=100&quantity=5&id=15&price=40&quantity=4&id=20&price=50&quantity=3
Then do this:
$str2 = '';
foreach($products as $product) {
$tmp = http_build_query($product);
if ( ! empty($str2) ) {
$str2 .= '&';
}
$str2 .= $tmp;
}
echo $str2 . "\n";
You could encode the entire array as JSON base 64 encoded.
$data = base64_encode(json_encode($products));
echo "http://example.com/?data=" . $data . PHP_EOL;
Then on the receiving end:
$products = json_decode(base64_decode($_GET['data']), true);
I am having to add more functionality in my codeigniter project. Here is the jsfiddle demo. I have an array like this:
$faculty = array(2, 3, 4);
and
$message = array('test1', 'test2', 'test3');
I'm also having an last insert id from another database:
$last_inserted_id = 1;
I want to combine the data to form an array like this:
$array = array(array('faculty' => 2, 'message' => 'test1', 'id' => 1),
array('faculty' => 3, 'message' => 'test2', 'id' => 1),
array('faculty' => 4, 'message' => 'test3', 'id' => 1));
Thanks for everyone for your suggestions and time.
I hope this will you:
$array = [];
$faculty = array(2, 3, 4);
$message = array('test1', 'test2', 'test3');
$id = 1;
for ($key = 0; $key < count($faculty); $key++) {
$array[$key]['faculty'] = $faculty[$key];
$array[$key]['message'] = $message[$key];
$array[$key]['id'] = $id;
}
print_r($array);
I have an array in php
$arr = array(
array('id' => 1, 'par' => 5),
array('id' => 2, 'par' => 5),
array('id' => 3, 'par' => 5),
array('id' => 4, 'par' => 7),
array('id' => 5, 'par' => 7),
array('id' => 6, 'par' => 7),
array('id' => 7, 'par' => 9),
array('id' => 8, 'par' => 9),
...
);
Can anybody know an effective algorithm to get a first indeks of an element which has property
$arr[x]['par'] == 7. How to get the first x from the array containing 2000 elements?
Thank you
I'm not sure if using an iterator is any faster than doing it by "hand", but you could use a RecursiveArrayIterator - http://php.net/manual/en/class.recursivearrayiterator.php
$arr = array(
array('id' => 1, 'par' => 5),
array('id' => 2, 'par' => 5),
array('id' => 3, 'par' => 5),
array('id' => 4, 'par' => 7),
array('id' => 5, 'par' => 7),
array('id' => 6, 'par' => 7),
array('id' => 7, 'par' => 9),
array('id' => 8, 'par' => 9),
);
$arrayIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach ($arrayIterator as $subKey=>$subValue) {
if ($subKey == 'par' && $subValue == 9) {
$validArray = iterator_to_array($arrayIterator->getSubIterator());
$id = $validArray['id']; // This is your return array
break;
}
}
But, to be honest, doing it by hand would probably be a lot easier to understand and debug, and for 2000 records - why bother with anything more complex than:
foreach($arr as $subArray) {
if ($subArray['par'] == 9) {
$id = $subArray['id'];
break;
}
}
If you were handling more records, or had the popularity of Facebook, then start to get serious. But sometimes keeping it simple is the best way.
I used a binary search
$par = 7;
$a = 0;$i = -1;
$b = count($arr);
while(true) {
if($par == $arr[$a]['par']) { $i = $a; break; }
if($par == $arr[$m]['par']) { $i = $m; break; }
if($par == $arr[$b]['par']) { $i = $b; break; }
if($a == $m || $m == $b) break;
if($arr[$a]['par'] < $par && $par < $arr[$m]['par']) {
$b = $m; $m = floor(($a+$b)/2);
}
if($arr[$m]['par'] < $parent && $parent < $arr[$b]['par']) {
$a = $m; $m = floor(($a+$b)/2);
}
}
That example was more slow, than a $i=0;while($i < $n && $arr[$i]['par'] != $par) $i++; Can be used the array_search instead?
In php, how do I get the total number of posts (16)?
array1("posts" => 2, "reactions" => 0)
array2("posts" => 4, "reactions" => 4)
array3("posts" => 7, "reactions" => 0)
array4("posts" => 3, "reactions" => 1)
You can use this function
array_merge_recursive
And I made an example for you
<?php
$a = array("posts" => 2, "reactions" => 0);
$b = array("posts" => 4, "reactions" => 4);
$c = array("posts" => 7, "reactions" => 0);
$d = array("posts" => 3, "reactions" => 1);
$array = array_merge_recursive($a,$b,$c,$d);
$sum = array_sum($array['posts']);
echo $sum;
?>
If you want to read more about array_merge_recursive
Follow this Link
Don't know how your variables are named, but some sort of:
$total = 0;
foreach ($arrayofstuff as $item) {
$total += $item['posts'];
}
$numposts = $arr1["posts"]+$arr2["posts"]+$arr3["posts"]+$arr4["posts"]
Do you have them in separate arrays or in one single array? If you have them separately then this would suffice:
$array1 = array("posts" => 2, "reactions" => 0);
$array2 = array("posts" => 4, "reactions" => 4);
$array3 = array("posts" => 7, "reactions" => 0);
$array4 = array("posts" => 3, "reactions" => 1);
$sum = $array1['posts'] + $array2['posts'] + $array3['posts'] + $array4['posts'];
echo $sum;
Outputs:
16
I have 2 arrays that i would like to loop through and combine into an associative array. I would like to use the 2 arrays as the keys for the new associative array. I am new to php so any and all help would be appreciated.
$id = array( 2, 4);
$qty = array( 5, 7);
array('id' => , 'qty' => );
Thanks in advance
I would like to output something like this
array(
'id' => 2,
'qty' => 5),
array(
'id'=> 4,
'qty' => 7
)
You can do:
$result = array();
for($i=0;$i<count($id);$i++) {
$result[] = array('id' => $id[$i], 'qty' => $qty[$i]);
}
Added by Mchl:
Alternative, IMHO a bit clearer, but it's matter of opinion mostly
$result = array();
foreach($id as $key => $value) {
$result[] = array('id' => $id[$key], 'qty' => $qty[$key]);
}
Also one-liner w/ lambda (PHP >= 5.3.0) and short array syntax [] (PHP >= 5.4)
$combined = array_map(function($id, $qty) {return ['id' => $id, 'qty' => $qty];}, $id, $qty);
or callback and old array() for earlier versions
function comb($id, $qty)
{
return array('id' => $id, 'qty' => $qty);
}
$combined = array_map('comb', $id, $qty);