I have written following lines of code in php like below
$keyarguments = array($orlastname,$oradmissionno,$orcourse,$orgender,$ordob,$orrollno,$ormiddlename,$oremail,$orguardian,$orphone,$orfullname,$orfirstmiddle,$orfirstlast);
foreach ($keyarguments as $key) {
${$key} = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
}
I want that code should be executed like
orlastname = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
oradmissionno = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
...
...
...
and so on
Please help!!!
try this, use string as the $keyarguments's elemnt.
$keyarguments = array('orlastname', 'oradmissionno');
Related
This code snippet:
$inline_keyboard = new InlineKeyboard([
['text' => 'valueA', 'callback_data' => 'valueA'],
], [
['text' => 'valueB', 'callback_data' => 'valueB'],
]);
produces in my telegramm bot the following inline keyboard:
So far so good... But instead of hardcoding the values, I want to produce the same output with values from an array (database query).
I tried with something like this:
$dbValues = array("valueA", "valueB");
foreach ($dbValues as $value)
{
$inline_keyboard .= new InlineKeyboard([
['text' => "$value", 'callback_data' => "$value"],
]);
}
But this fails... I think because I don't have to run a "new" instance in each iteration?
Thanks for helping!
You can't concatenation object like string. you can go another way, build the array, and after send array to InlineKeyboard
$dbValues = array("valueA", "valueB");
foreach ($dbValues as $value)
{
$inline_keyboard[] = [['text' => "$value", 'callback_data' => "$value"]];
}
$inline_keyboard = new InlineKeyboard(...$inline_keyboard);
Further details see "New Keyboard structure and how to pass dynamic arguments" from the php-telegram-bot wiki.
To get a horizontal keyboard you can use this code snippet:
$dbValues = array("valueA", "valueB");
foreach ($dbValues as $value)
{
$inline_keyboard[] = ['text' => "$value", 'callback_data' => "$value"];
}
$inline_keyboard = new InlineKeyboard($inline_keyboard);
hi im using this php code
$data = new $models([
'number' => $row[0],
'name' => $row[1],
]);
like this its working fine but what i want is that i dont know the keys 'number' and 'name' what if i want them come from array like this ..
$array = ['name','number','anything'];
$data = new $models([
foreach($array as $key => $arr)
{
$arr => $row[$key];
}
]);
how can i do something like this ..
calling foreach inside creating new class ..
thanks ..
<?php
class Foo{}
$className='Foo';
$array = ['name','number','anything'];
$row = ['name_v', 'number_v', 'anything_v'];
$foo = new $className();
foreach(array_combine($array, $row) as $k => $v){
$foo->$k = $v;
}
print(json_encode($foo));
So long as they have the same number of elements in the order that you want, just combine them:
$array = ['name', 'number', 'anything'];
$data = new $models(array_combine($array, $row));
You can do it in one line:
$data = new $models(array_combine(['name', 'number', 'anything'], $row));
I have an issue where query fails to work when i use values from mongoDB but works when i try static/hard-coded values.
Can you kindly point out what i am not doing correctly ?
When i hard-code the values like below, the code works fine.
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' => array('$in' => array('591d380c227951b706e18a56','591da979227951a10f004ad7','591d42292279517209004ad7'))));
but when the values come from mongoDB this query fails to work.
$cattype = array();
foreach ($catlist as $key => $value){ $cattype[] = "'".$value['cid']."'";}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' => array('$in' => array($catlist))));
$catlist = comes from mongoDB
Thanks in advance
There are two errors:
The first one is in your for each. Instead of adding the id's, you add '...'.
foreach ($catlist as $key => $value){
$cattype[] = $value['cid']
}
The second on is in your statement. You want to pass the $cattype array, but $cattype is already an array, so you wrap it again inside an array.
$cursor = $customer->find([
'provider_id' => "0",
'cat_id' => ['$in' => $catlist]
]);
Please use first need to be convert in mongo id,
It may help you.
$cattype = array();
foreach ($catlist as $key => $value) {
$cattype[] = new \MongoId($value['cid']);
}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' =>
array('$in' => array($catlist))));
$cattype = array();
foreach ($catlist as $key => $value) {
$cattype[] = $value['cid'];
}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' =>
array('$in' => $catlist))
);
I had found answer. no need to implode $cattype. just give $cattype in direct inside of $in like array('$in' => array($cattype))
I'm pulling through data from my database and wish to add an object to the end of each item. The following code works, but I'm assuming there's a better way than repeating all the info and adding to a new object.
$cs = $client->contact()->get();
foreach ($cs as $c) {
$contact = (object)[
'id' => $c->id,
'name' => $c->name,
'role' => $c->role,
'phone' => $c->phone,
'address' => $c->address,
'postcode' => $c->postcode,
'otherClients' => Contact::find($c->id)->clients()->get(), //this is the additional info
];
$contacts[]=$contact;
You could simply mutate the original objects if you don't need to leave $cs intact.
foreach ($cs as $c) {
$c->otherClients = Contact::find($c->id)->clients()->get();
}
you can use
As suggested by #MrCode
$cs = $client->contact()->get();
PHP 5.4+
foreach ($cs as $c) {
$c->otherClients = Contact::find($c->id)->clients()->get(), //this is the additional info
}
PHP 4 Or below
foreach ($cs as &$c) {
$c->otherClients = Contact::find($c->id)->clients()->get(), //this is the additional info
}
This shows me everything (including 55d9d86746ba9a3a7f642b83).
I don't want it to show me the data in the array $veri.
$veri=Array
(
[0] => 55d9d86746ba9a3a7f642b83
)
$urun = $c->find(array('_id' => array('$nin' => $veri)));
Try the below code:
<?php
$mongo = new Mongo();
$db = $mongo->selectDB("foo");
$cur = $db->bar;
$veri = array(
new MongoId('55d9d86746ba9a3a7f642b83')
);
$urun = $cur->find(array('_id' => array('$nin' => $veri)));
foreach($urun as $doc) {
var_dump($doc);
}
?>
Notice how I use MongoId, instead of just copy pasting the id as is. Also notice that the array doesn't need an index [0]