Issue in the code while writting variables dynamically - php

I have written below lines of code
$advsr_firstname="David"
$advsr_middlename ="";
// note that these two are passed through function parametre
$first_name=array('$or' => array(array("student.first_name" => new MongoRegex("/$advsr_firstname/i"))));
$last_name= array('$or' => array(array("student.last_name" => new MongoRegex("/$advsr_middlename/i"))));
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
$empty = "X9w+";
foreach ($keyarguments as $key => $value)
{
if(${$value} =='' || ${$value} =='0' || ${$value} =="Select")
{
${$key} = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
}
}
$orrollno= array('$or' => array(array("student.roll_no" => new MongoRegex("/$arg/i"))));
$query = array( '$or' => array($first_name,$last_name,$orrollno));
I am trying to write query dynamically so that $query will work properly.
I want that if "$advsr_middlename" is empty then the code should dynamically modify the expression $last_name in the loop like
$last_name = array('$or' => array(array("dummy_feild" => new MongoRegex("/$empty/i"))));
It is throwing error messages undefined variable first_name=>advsr_firstname
....
Please help me as the code is not working!!!

I think this could be the solution: fix the syntax of $keyarguments array from
$keyarguments = array(
'first_name=>advsr_firstname',
'last_name=>advsr_middlename'
);
to
$keyarguments = [
'first_name' => 'advsr_firstname',
'last_name' => 'advsr_middlename',
];

Related

Compile Error Can't use method return value in write context in code igniter

i got this model which inserts data on the database but the problem is, other fields are not required so i did this,
public function insert(){
if($this->input->post('MNAME') = NULL){
$mname = "n/a";
}else{
$mname = $this->input->post('MNAME');
}
if($this->input->post('EXTENSION') = NULL){
$ext = "n/a";
}else{
$ext = $this->input->post('EXTENSION');
}
if($this->input->post('EMAIL') = NULL){
$email = "n/a";
}else{
$email = $this->input->post('EMAIL');
}
if($this->input->post('CELLNO') = NULL){
$cell = "n/a";
}else{
$cell = $this->input->post('CELLNO');
}
if($this->input->post('AGENCY_NO') = NULL){
$agency = "n/a";
}else{
$agency = $this->input->post('AGENCY_NO');
}
$input = array(
'ID_NUM' => $this->uri->segment(3),
'FNAME' => $this->input->post('FNAME' ),
'SURNAME' => $this->input->post('SURNAME' ),
'DO_BIRTH' => $this->input->post('DO_BIRTH' ),
'POBIRTH' => $this->input->post('POBIRTH' ),
'SEX' => $this->input->post('SEX' ),
'CIVILSTAT' => $this->input->post('CIVILSTAT' ),
'ID_NAT' => $this->input->post('ID_NAT' ),
'HEIGHT' => $this->input->post('HEIGHT' ),
'WEIGHT' => $this->input->post('WEIGHT' ),
'BLOOD_TYPE' => $this->input->post('BLOOD_TYPE' ),
'RES_ADD' => $this->input->post('RES_ADD' ),
'RES_ZIP' => $this->input->post('RES_ZIP' ),
'PERM_ADD' => $this->input->post('PERM_ADD' ),
'PERM_ZIP' => $this->input->post('PERM_ZIP' ),
'MNAME' => $mname,
'EXTENSION' => $ext,
'EMAIL' => $email,
'CELLNO' => $cell,
'AGENCY_NO' => $agency,
'DATE_CREATED' => date("Y-m-d")
);
$insert = $this->db->insert($this->table,$input);
return $insert;
}
but the problem is that i get this error Can't use method return value in write context says that its on line 108. which, the line 108 is the first if of my model. what is my error? and is there any codes which will be shorter?
You're only stating 1 = in your IF statements, for future note: It should be 2 ='s in a match.
I restructured this for you so your code is more readable and short.
// Checking if they're null and appending n/a if they're
$example = array('MNAME','EXTENSION','EMAIL', 'CELLNO', 'AGENCY_NO');
$new = array();
foreach ($example as $_ex)
{
$check = $this->input->post($_ex);
if ($check == null)? array_push($new, 'n/a') : array_push($new, $check);
}
Then replace with this (keeping in mind index's start at 0 in an array):
// Inside your DB insert query
'MNAME' => $new[0],
'EXTENSION' => $new[1],
'EMAIL' => $new[2],
'CELLNO' => $new[3],
'AGENCY_NO' => $new[4],
I hope this helped.

Codeigniter combining information

first of, just wanted to let you know that I am a newbie at CI. but I am having trouble with this piece of code where is breaking and I can't seem to be able to find the answer anywhere.
for some reason the code is breaking at the first if statement.. if possible could you help me out understand what is really happening there?
Thank you all for your help!
function main
{
$this->load->model(getData) psudo code for this area...
}
---model---
function getData....
{
Sql = this->db->query(sql code that returns all the information required.)
$result = $sql->result_array();
$types = array ( 'EVENT|TIME' => array( 'id' => 1, 'name' => 'Regular' ),
'PROPOSITION|REGULAR' => array( 'id' => 2, 'name' =>'Propositions'),
'EVENT|TIME' => array( 'id' => 3, 'name' => 'Now' ),
'PROPOSITION|FUTURES' => array( 'id' => 4, 'name' => 'Future' ));
$var = array();
foreach ($result as $event) {
$cat = $event['type'] . '|' . $event['sub_type'];
$typeId = $types[$cat]['id'];
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))
{
if(!is_array($var[$event['event_id']]))
{
$var[$event['event_id']] = array( 'event_name' =>
$event['event_name'],'event_abbreviation' =>
$event['event_abbreviation']);
}
$var[$event['event_id']]['var'][$event['type_id']] = array(
'type_name' => $event['abbreviation'],'type_abbreviation' => $event['name']
);
}
$event[$event['event_id']]['var'][$event['type_id']]['types'][$typeId] =
$types[$cat]['name'];
}
return $myResults;
}
In this line
if(!is_array($var[$event['event_id']]['var']$event['type_id']]))
You are missing a [ somewhere. I'm guessing before $event['type_id'].
So replace with:
if(!is_array($var[$event['event_id']]['var'][$event['type_id']]))

Multidimensional array in php to save mysqli output

Here is a piece of my code:
if($all_pages)
foreach ($all_pages as $page)
{
$all_hokms = $mysqli->query("SELECT * FROM qm_hokm WHERE page_id = ".$page['page_id']."");
if($all_hokms)
foreach ($all_hokms as $hokm)
{
$one_page_ahkams[] = array(
'hokm_id_inPage' => $hokm['hokm_id_inPage'],
'type' => $page['type'],
);
}
else
$one_page_ahkams[] = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams), //Here Is the PROBLEM : (((
);
}
else
$all_pages_data[] = array();
echo json_encode($all_pages_data);
As you see, I want to send multidimensial array() in a json format (from server to client), but how can correct the insertion of an array in an other, I mean that I need to add $one_page_ahkams array in $all_pages_data one, any ideas
$one_page_ahkams = array();
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array($one_page_ahkams);//this comma (,) causes the problem i guess**
);
An other way to do it :
$all_pages_data[] = array(
'page_id' => $page['page_id'],
'done' => $page['done'],
'checked' => $page['checked'],
'ahkams' => array(['one_page_ahkams']=>$one_page_ahkams);
);
Just put that: 'ahkams' => array($one_page_ahkams), but check the content of $one_page_ahkams

Looping through results of a sql query

I have a query that returns multiple rows. I can't seem to find a way to store the rows in the $params array. Is there a way to loop throw and store each row in the $params variable
$aResult = $db->exec_sql($sql);
$params = array(
// where $aResult[o]'' would be row 1 [1] row 2 etc. //
'store_id' => $aResult[]['iStoreID'],
'user_id' => $aResult[]['iUserID'],
'store_name' => $aResult[]['cStoreName'],
'store_url' => $aResult[]['cStoreURL'],
'rid' => $aResult[]['cRID'],
'affiliate_id' => $aResult[]['iAffiliateID'],
'team_id' => $aResult[]['iTeamID'],
'bizOP' => $aResult[]['cDefaultBizOpp'],
'showBizOPp' => $aResult[]['iShowBizOppDropdown'],
'boPosting' => $aResult[]['iEnableBOPosting'],
'brandinglevel' => $aResult[]['iBrandingLevel']
);
thank you for your help
As simple as that:
$params = array();
foreach($aResult as $row) {
$params[] = array(
'store_id' => $row['iStoreID'],
'user_id' => $row['iUserID'],
'store_name' => $row['cStoreName'],
'store_url' => $row['cStoreURL'],
'rid' => $row['cRID'],
'affiliate_id' => $row['iAffiliateID'],
'team_id' => $row['iTeamID'],
'bizOP' => $row['cDefaultBizOpp'],
'showBizOPp' => $row['iShowBizOppDropdown'],
'boPosting' => $row['iEnableBOPosting'],
'brandinglevel' => $row['iBrandingLevel']
);
}
Without knowing the exact structure of the result array i guess you need something like this:
<?php
$params = array();
$mapping = array(
'store_id' => 'iStoredID',
'user_id' => 'iUserID',
// and so on...
);
foreach ($aResult as $row) {
$tempRow = array();
foreach ($row as $key => $value) {
$paramKey = isset($mapping[$key]) ? $mapping[$key] : $key;
$tempRow[$paramKey] = $value;
}
$params[] = $tempRow;
}
I use it like this
$aResult = mysql_query($sql);
while($data = mysql_fetch_array($result)) {
'store_id' = $aResult['iStoreID'];
}
At least that is the idea

Check if an array of an array contains a certain string

I'm checking to make sure an array of arrays does not contain certain strings before adding any new child arrays to the parent array
I want to make sure that if an array with the same website and condition exists a new child array will not be added to the parent array.
e.g. in this example the $newArr must not be inserted in to the array $arr because their already exists an array with the same website and condition.
$arr = array(
array(
'website' => 'amazon',
'price' => 20,
'location' => 'uk',
'link' => '...',
'condition' => 'new'
),
array(
'website' => 'abe',
'price' => 20,
'location' => 'uk',
'link' => '...',
'condition' => 'new'
)
);
$newArr = array(
'website' => 'amazon',
'price' => 60,
'location' => 'uk',
'link' => '...',
'condition' => 'new'
)
I'm looking for an easy solution as using the function in_array on the parent array is not enough.
code so far
$arr = array();
foreach($table->find('tr.result') as $row){
if(($website = $row->find('a img',0))
&& ($price = $row->find('span.results-price a',0))
&& ($location = $row->find('.results-explanatory-text-Logo'))
&& ($link = $row->find('a',0))){
$website = str_replace( array('.gif','.jpg','.png'), '', basename($website->src));
$price = floatval(trim(str_replace(',', '', $price->innertext), "£"));
$location = "uk";
$link = $link->href;
$arr[] = array(
'website' => $website,
'price' => $price,
'location' => $location,
'link' => $link,
'condition' => 'new'
);
}
}
You loop over $arr each time to look for $website and $condition (always 'new'?) or you can keep a secondary array of the found keys. If you're starting with an empty $arr each time, the second approach will work and be faster.
$arr = array();
$keys = array();
foreach($table->find('tr.result') as $row){
if(...){
...
$condition = 'new'; // set as needed
// track seen keys
$key = $website . '|' . $condition; // assumes neither field contains '|'
if (!isset($keys[$key])) {
$keys[$key] = true;
$arr[] = array(...);
}
}
}
I hope the comments in the below code speak for themselves... I'm not a PHP pro, and this is probably not the most elegant way, but I believe the logic makes sense. Obviously the $new_array object has some variables that aren't declared but it's for example only.
I hope that helps and that no one down votes me :)
<?php
// Original array
$arr = array();
foreach($result as $row) {
// Get the new array as an object first so we can check whether to add to the loop
$new_array = array(
'website' => $website,
'price' => $price,
'location' => $location,
'link' => $link,
'condition' => 'new'
);
// If the original array is empty there's no point in looping through it
if(!empty($arr)) {
foreach($arr as $child) {
// Check through each item of the original array
foreach($new_array as $compare) {
// Compare each item in the new array against the original array
if(in_array($compare, $child)) {
// if there's a match, the new array will not get added
continue;
}
}
}
}
// If there's no match, the new array gets added
$arr[] = $new_array;
}
?>

Categories