using this:
$rdy = R::find( 'players','room=0');
$count = count($rdy);
if($count>1)
{
for($i=0;$i<$count;$i++)
{
$id = $rdy[$i]->id;
$user = R::load('players',$id);
if($user->id!=0)
{
//changing $user variables..
R::store($user);
}
}
}
but beans that R::find() function found give empty fields,
am i using it wrong?
Replace your first line
$rdy = R::find( 'players','room=0');
with
$rdy = R::find('players', " room = ?", [0]);
You can place variables instead of the hard coded 0.
You can also check on multiple properties and pass multiple variables in the array
Related
I call a function, and I want to get a response with an array and two elements per key.
include_once($_SERVER['DOCUMENT_ROOT'].'/matsuka/_giveRandomQuestSearchPok.php');
$pokQ = QuestPoks::giveRandomQuestPok();
The function itself looks like this
public static function giveRandomQuestPok() {
$sC = \Work::$sql->prepare('SELECT region FROM users WHERE id = ?');
$sC->bind_param("i", $_SESSION['id']);
$sC->execute();
$dA = $sC->get_result();
$region = $dA->fetch_assoc();
if($region['region'] == 1){
include_once($_SERVER['DOCUMENT_ROOT'].'/matsuka/randomPoks/Kanto.php');
}elseif($region['region'] == 3){
include_once($_SERVER['DOCUMENT_ROOT'].'/matsuka/randomPoks/Hoenn.php');
}
$rarity = mt_rand(1,100);
if($rarity == 1){
$rare = 1;
$object = \matsuka\Rare[mt_rand(0, count(\matsuka\Rare) - 1)];
}elseif($rarity > 1 && $rarity <= 33){
$rare = 2;
$object = \matsuka\Uncommon[mt_rand(0, count(\matsuka\Uncommon) - 1)];
}else{
$rare = 3;
$object = \matsuka\Common[mt_rand(0, count(\matsuka\Common) - 1)];
}
$array = array();
$array['pok'] = $object;
$array['rare'] = $rare;
return $array;
}
After executing the function I want to assign two variables to control the result
$pok = $pokQ["pok"];
$rare = $pokQ["rare"];
But the handler still reads them as an array.
And when I paste it into sql I get an Array, although I was hoping to get the array data by their corresponding key.
In var_dump I get quite a strange answer. I seem to see the required value of array called by the key, but on top of that I see another array, if I understand correctly.
int(401)
{
"name":"\u0413\u0443\u0441\u0442\u0430\u0432",
"actionQuest":"<img src=\"\/img\/quests\/7.webp\" class=\"quest\"> \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0432 \u0437\u0430\u0434\u0430\u043d\u0438\u0438 \u00ab\u041f\u043e\u043c\u043e\u0449\u044c \u0441\u0442\u0430\u0440\u043e\u043c\u0443 \u043f\u0440\u0438\u044f\u0442\u0435\u043b\u044e\u00bb. \u0417\u0430\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u0432 \u0410\u043a\u0432\u0430\u0431\u0443\u043a.",
"question":"\u0422\u044b \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u043c\u043e\u0438\u043c \u043f\u0440\u043e\u0448\u043b\u044b\u043c \u0437\u0430\u0434\u0430\u043d\u0438\u0435\u043c. \u042f \u0445\u043e\u0440\u043e\u0448\u0435\u043d\u044c\u043a\u043e \u0438\u0437\u0443\u0447\u0438\u043b \u0442\u043e\u0433\u043e \u043f\u043e\u043a\u0435\u043c\u043e\u043d\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043d\u0435 \u043d\u0443\u0436\u0435\u043d: <div class=\"questPokemon\"><img src=\"\/img\/pokemons\/mini\/normal\/Array.webp\"> <div>#Array <\/div><\/div>"
}
Why does this happen and what is my mistake?
In the array I should only get 3-digit numbers under each key
I dont know why after clicking the "Add Button", the items that needs to insert into the database is NULL.
so here's my controller
public function addItem(){
$data = array(
"item_ID"=>$_POST['item_ID'],
"itemName"=>$_POST['itemName'],
"itemDescription"=>$_POST['itemDescription'],
"itemLink"=>$_POST['itemLink']
);
$orgID = $this->model->get_org();
$this->model->insertItem($data,$orgID);
}
and here's my model
public function insertItem($data,$orgID){
$this->db->insert('Items',$data);
$getID=$this->db->insert_id();
foreach($orgID as $temp):
$Organization_ID = $_POST[$temp->OrganizationID];
endforeach;
$SkillReq=$this->input->post('0');
$Skill_ReqID=$this->input->post();
$insertdata = array();
for($i=0; $i=count($orgID); $i++){
$insertdata[]=array(
'Skill_ReqID'=>$Skill_ReqID[$i],
'OrganizationID'=>$Organization_ID[$i],
'item_ID'=>$getID[$i],
'SkillReq'=>$SkillReq[$i]
);
}
$this->db->insert_batch('skillreqdept',$insertdata);
}
it actually inserts the rows that had been counted in orgID but there is no data, only the Auto Incremented skill_reqID has in it.
I thing the issue is with Skill_ReqID'=>null[$i], I dont know what is null variable.
Please try the following:
public function insertItem($data,$orgID){
$this->db->insert('Items',$data);
$getID=$this->db->insert_id();
foreach($orgID as $temp):
$Organization_ID = $_POST[$temp->OrganizationID];
endforeach;
$SkillReq=$this->input->post('0');
$insertdata = array();
for($i=0; $i=count($orgID); $i++){
$items=array(
'OrganizationID'=>$Organization_ID[$i],
'itemID'=>$getID[$i],
'SkillReq'=>$SkillReq[$i]
);
array_push($insertdata, $items);
}
$this->db->insert_batch('skillreqdept',$insertdata);
}
Ya, it should be empty. Check your array "
public function addItem(){
$data = array(
'item_ID"=>$_POST['item_ID'], <== wrong wrapping
'itemName"=>$_POST['itemName'],
'itemDescription"=>$_POST['itemDescription'],
'itemLink"=>$_POST['itemLink']
);
$orgID = $this->model->get_org();
$this->model->insertItem($data,$orgID);
}
If you're using proper IDE, it will highlight such a minor issues like this
As well insertdata is any predefined PHP tag/function ??
As well what is this null[$i] assign. I never heard of it.
Before do insert use print_r to check data is in the correct format or not. print_r($insertdata); die;
I am taking data from many tables. I want to display many objects in different places. I got the data from data base, but I want to put the data into an array for useful purpose, but it's not working.
This my controller code:
public function compare_by_business_sectors() {
//print_r($this->input->post());exit;
if ($this->input->post())
{
$solution_array = array();
//print_r (json_encode($business_sectors)); exit;
$business_sectors=$this->home_model->compare_business_sectors_data($this->input->post());
$tab_child_id = "";
$id="";
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id = array();
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
//$solution_array[$id]= $id;
}
print_r($id);
//$this->load->view('compare_by_business_sectors.php');
}
This is my model code:
public function compare_business_sectors_data($sectorid) {
$query = $this->db->select('solutions.*,solution_tabs_child_fields.field_id,solution_tabs_child_fields.tab_child_id,solution_tabs_child_fields.title')
->from('solutions')
//->join('solutions', 'business_sector.sector_id = solutions.business_sector_id',"left")
->join('solution_features','solutions.entry_id = solution_features.entry_id',"left")
->join('solution_tabs_child_fields','solution_features.field_id = solution_tabs_child_fields.field_id')
->where('solutions.business_sector_id', $sectorid['id'])
->get();
return $query->result();
//print_r($query->result());exit;
}
change it as follow and try.
$id_string = "";
$id_array = array();
foreach ($business_sectors as $key=>$sectors) {
$solution_array[1]=$sectors->solution_name;
$solution_array[2]=$sectors->description;
$solution_array[3]=$sectors->vendor_name;
$solution_array[4]=$sectors->video_presentation;
$solution_array[5]=$sectors->start_free_trail;
$solution_array[6]=$sectors->hardware_package;
$solution_array[7]=$sectors->pos_market_rating;
//$solution_array[$sectors->field_id] = $sectors->value;
$id_string = "solution".$sectors->tab_child_id;
if ($tab_child_id != $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
else if ($tab_child_id == $sectors->tab_child_id) {
$id[$sectors->field_id] = $sectors->title;
}
}
print_r($id_string);
print_r($id_array);
First you are assigning string value to $id then, $id will convert to array only if first if() statement execute other wise it will not be a string. So to overcome from this keep $id_array before for loop and you can capture string in another variable.
With
$result = Braintree_Customer::find($id);
it's possible to fetch all data from a customer by providing an additional attribute. But if I want to fetch bulked data like all creditCards types it's not possible as I can't iterate through the creditCards attribute with a foreach or a for loop. Does this attribute contain a length or size value that I could use?
What here is the right approach?
I found a possible approach by simply checking for the last attribute of an object to be null.
public function getCustomerPaymentMethods($id)
{
try {
$i = 0;
$result = Braintree_Customer::find($id);
while($result->creditCards[$i]->token !== null) {
$card[$i] = new stdClass();
$card[$i]->token = $result->creditCards[$i]->token;
$card[$i]->type = $result->creditCards[$i]->cardType;
$card[$i]->default = $result->creditCards[$i]->default;
$card[$i]->expired = $result->creditCards[$i]->expired;
$card[$i]->last4 = $result->creditCards[$i]->last4;
$i++;
}
return $card;
} catch(Braintree_Exception_NotFound $e) {
return false;
}
}
I have five text fields that are not mandatory. Sometimes enter data in one field, sometimes 2 or 3 or 4 or 5. I have to count this data:
$sib1=$this->input->post(sib1);
$sib2=$this->input->post(sib2);
$sib3=$this->input->post(sib3);
Like this, I want to count this. Now 3
You can use a for loop with length = number of text fields. Put it to the database afterwards. sibcount is the number of filled entries.
$data = array();
$length = 5;
$sibcount = 0;
for ($i=0;$i<$length;$i++) {
$entry = $this->input->post("sib".$i);
if (!empty($entry)) {
$data["sib".$i] = $entry;
$sibcount++;
}
}
$data["sibcount"] = $sibcount;
// use in controller or model
if (!empty($data))
$this->db->insert('mytable', $data);
this is way to do it:
Take an array $arr=array();
$arr['sib1']=$this->input->post("sib1",true);
$arr['sib2']=$this->input->post("sib2",true);
$arr['sib3']=$this->input->post("sib3",true);
$arr['count']=count($this->input->post($arr)); //this will count your post
$result = $this->Model_name->model_function($arr);
In Model
function model_function($arr)
{
$this->db->insert('tbl',$arr);
if ($this->db->affected_rows() > 0) {
return true;
} else {
return false;
}