I am trying to wrap a variable in single quotes in PHP. This is my code:
$my_post = array(
'post_title' => $orderRequestCode.' - ' . $customerName,
'post_content' => $orderContent,
'post_status' => 'draft',
'post_author' => "'".$user_ID."'",
'post_type' => 'orders'
);
Currently var_dump($my_post) outputs:
array (size=5)
'post_title' => string '2014-06-13-15-13-52 - xxxxxxx' (length=35)
'post_content' => string 'Order Code: 2014-06-13-15-13-52
Customer Name: xxxxxxxxxxxx
Customer Email: xxx#xxxx.xx
Order Items:
Stock Code: Q20-50-6101 Quantity: 12
Comments:
' (length=162)
'post_status' => string 'draft' (length=5)
'post_author' => string ''1'' (length=3) <--------------- should be '1'
'post_type' => string 'orders' (length=6)
This line:
'post_author' => string ''1'' (length=3)
Needs to be:
'post_author' => string '1' (length=3)
The outside quotes in your var_dump aren't really part of the string, hence the length being 3, not 5. If you echo your string it will be '1'
No, you're telling PHP to create a 3-character string:
$x = 1;
$y = "'" . $x . "'";
$y = ' 1 '
1 2 3
Since it's a string, it will get wrapped by OTHER ' as well when you do your dump. If your ID is an integer, and you want it to be treated as a string, then you could something as simple as:
'post_author' => (string)$user_ID // cast to string
or
'post_author' => '' . $user_ID; // concatentate with empty string
Related
I am trying to understand why a variable that contains an integer cannot but used to replace the integer in GFAPI::get_entries field_filters value.
This works:
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'any',
array(
'key' => 'gpnf_entry_parent',
'value' => '72'
)
)
);
This does not work:
$child_entry_ID = "{entry_id}";
where $child_entry_ID is 72. Of course
echo $child_entry_ID . "<br />";
prints "72"
$search_criteria = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'any',
array(
'key' => 'gpnf_entry_parent',
'value' => $child_entry_ID
)
)
);
I use it like this
$entry2 = GFAPI::get_entries(33, $search_criteria);
print_r($entry2);
In the case that works, my arrays print correctly, in the case that doesn't work, I get "Array().
So it seems that when I put $child_entry_ID which is:
$child_entry_ID = "{entry_id}";
It echoes out to an integer (72 in this case) but is not interpreted as it's result (72) but as a string ({entry_id}).
I used the function rgar to change $child_entry_ID :
$child_entry_ID = rgar( $entry, 'id' );
And this works where the value is really interpreted as the result (72) and my search criteria interprets correctly.
I know this is old, but shouldn't
$child_entry_ID = "{entry_id}";
Actually be:
$child_entry_ID = "{$entry_id}";
For a brief, I have a USER which has multiple INI Files uploaded to his account.
The Path and Filename are stored in the database.
So The INI file has SECTIONS then the parameter and the value.
This is my INI File sample:
[INIDetails]
SipUserName=
Password=
Domain=182.010.50.59
Proxy=5080
Port=
SipAuthName=
DisplayName=
ServerMode=Automatic
UCServer=192.168.50.5250
UCUserName=
UCPassword=
[DialPlan]
DP_Exception=
DP_Rule1=
DP_Rule2=
[Advanced]
OperationMode=1
MutePkey=16
Codec=
PTime=20
AudioMode=3
SoftwareAEC=
EchoTailLength=5
PlaybackBuffer=10
CaptureBuffer=10
JBPrefetchDelay=60
JBMaxDelay=160
SipToS=
RTPToS=00
LogLevel=
So now for values like Domain and Proxy, I store them in the database, and I let the user edit those values.
Now the problem:
If there is an update in database value, I loop through and I fetch all the files that user has and I want to update them. So I do this:
function update_ini($site_session)
{
$base_configs= $this->upload_ini_m->get_files();
//Now Here We Have Files That Has to be updated as per DATABASE PARAMS
$count = count($base_configs);
//WE KNOW THE NUMBER OF FILES TO UPDATED
for ($i=0; $i < $count; $i++)
{
$filename = $base_configs[$i]->base_ini_filename;
$path = $base_configs[$i]->file_path;
//Here We Get Final File To Process//
$file_to_update =$path.$filename;
// $file_contents = file_get_contents( $file_to_update);
//Parse the INI
$file_contents = $this->data['parameters'] = parse_ini_file($file_to_update,true);
//This is Our Array which has the values so replace the values here.
foreach ($file_contents as $key => $sections)
{
foreach ($sections as $parameter => $value)
{
//Update files here
}
}
}
}
Now I want to put the values that user inserted into database written against the parameter it has, If I update the Domain name then I just want to write the value I get from POSTand write it against the parameter.
How Can I Achieve This?
I Guess I can use STR_REPLACE but how I am not sure. I want to replace the values I get from post from the values in the File.
When I var_dump() $file_contents I Get this:
array (size=3)
'INIDetails' =>
array (size=11)
'SipUserName' => string '' (length=0)
'Password' => string '' (length=0)
'Domain' => string '182.010.50.59' (length=13)
'Proxy' => string '5080' (length=4)
'Port' => string '' (length=0)
'SipAuthName' => string '' (length=0)
'DisplayName' => string '' (length=0)
'ServerMode' => string 'Automatic' (length=9)
'UCServer' => string '192.168.50.5250' (length=15)
'UCUserName' => string '' (length=0)
'UCPassword' => string '' (length=0)
'DialPlan' =>
array (size=3)
'DP_Exception' => string '' (length=0)
'DP_Rule1' => string '' (length=0)
'DP_Rule2' => string '' (length=0)
'Advanced' =>
array (size=14)
'OperationMode' => string '1' (length=1)
'MutePkey' => string '16' (length=2)
'Codec' => string '' (length=0)
'PTime' => string '20' (length=2)
'AudioMode' => string '3' (length=1)
'SoftwareAEC' => string '' (length=0)
'EchoTailLength' => string '5' (length=1)
'PlaybackBuffer' => string '10' (length=2)
'CaptureBuffer' => string '10' (length=2)
'JBPrefetchDelay' => string '60' (length=2)
'JBMaxDelay' => string '160' (length=3)
'SipToS' => string '' (length=0)
'RTPToS' => string '00' (length=2)
'LogLevel' => string '' (length=0
Guys I have a simple but boring question...
I have the following scenario:
Solicitation -> HasMany -> Destiny
Destiny -> BelongsTo -> City
My problem is on the view, I want to retrieve the City name instead of the city_id and I can't figure out.
Here is my Result:
array (size=12)
'Solicitation' =>
array (size=32)
'id' => string '25' (length=2)
'Destiny' =>
array (size=2)
0 =>
array (size=3)
'id' => string '3' (length=1)
'solicitation_id' => string '25' (length=2)
'city_id' => string '4382' (length=4)
1 =>
array (size=3)
'id' => string '4' (length=1)
'solicitation_id' => string '25' (length=2)
'city_id' => string '4350' (length=4)
How can I retrieve the city name on the view?
Here's my controller:
public function mySolicitations()
{
$this->Solicitation->recursive = 1;
$person_id = $this->Auth->user('id');
$conditions = array(
'Solicitation.created_by' => $person_id
);
$this->set(array(
'data' => $this->paginate('Solicitation', $conditions, array('Solicitation.id' => 'DESC')),
'title_for_layout' => 'My Solicitations'
));
}
Is there a problem with my relation?
Thanks in advance.
There are two ways:
Change your model recursive value to 2.
$this->Solicitation->recursive = 2;
The drawback of using this is when you have quite a few associations involved, it'll retrieve a lot of unwanted unnecessary data.
You can use "Containable" Behavior here.
$this->Solicitation->Behaviors->load("Containable");
And specify the model names that you want to retrieve:
$this->Solicitation->contain(array(
"Destiny", "Destiny.City"
)
);
Then add your code:
$conditions = array(
'Solicitation.created_by' => $person_id
);
$this->set(array(
'data' => $this->paginate('Solicitation', $conditions, array('Solicitation.id' => 'DESC')),
'title_for_layout' => 'My Solicitations'
));
If you debug, you'll certainly find the "City" index within each Destiny sub arrays.
Hope this helps.
Peace! xD
i'm working on a small text based web game for a few friends to play (for nostalgia) but i'm having trouble with the very core of the game, the attacking/defending.
Each player has x amount of each troop type (more can be purchased and will be lost on atk/def)
so far I have a table with troop types and stats and a table with troop qty linked with user id and troop id.
I'm using the user id to get the troop id and qty using;
// Get attacker troop quantities.
$i = 0;
$attacker_deets = $conn->prepare("SELECT * FROM troop_qty WHERE user_id= :attacker_id");
$attacker_deets->bindParam(':attacker_id', $attacker_id);
$attacker_deets->execute();
$attacker_deets_results = $attacker_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($attacker_deets_results as $atk_key=>$atk_result) {
echo "<pre>"; var_dump($atk_result); echo "</pre>";
}
which outputs;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
I'm then using the troop id to get the troop details;
// get attacker troop details.
$attacker_troop_deets = $conn->prepare("SELECT * FROM troops WHERE troop_id= :atk_troop_id");
$attacker_troop_deets->bindParam(':atk_troop_id', $attacker_deets_results[$i]['troop_id']);
$attacker_troop_deets->execute();
$returned_results = $attacker_troop_deets->fetchAll(PDO::FETCH_ASSOC);
foreach($returned_results as $key=>$result) {
echo "<pre>"; var_dump($result); echo "</pre>";
}
++$i;
which in total gives me;
array (size=4)
'troop_qty_id' => string '1' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '1' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '1' (length=1)
'troop_name' => string 'Fist Puncher' (length=12)
'troop_atk' => string '1' (length=1)
'troop_def' => string '1' (length=1)
array (size=4)
'troop_qty_id' => string '2' (length=1)
'user_id' => string '2' (length=1)
'troop_id' => string '2' (length=1)
'qty' => string '100' (length=3)
array (size=4)
'troop_id' => string '2' (length=1)
'troop_name' => string 'Stick Waver' (length=11)
'troop_atk' => string '2' (length=1)
'troop_def' => string '1' (length=1)
Now where i'm stuck is I need to be able to compare the troop_atk of First Puncher with the defence of Stick Waver which is gotten from a different set of duplicate query's getting the defenders details, i also need to be able to multiply and divide the atk and def variables.
so how would i go about achieving this? i would assume i would need to give each field in the array their own variable, but how? i have tried using ${"troop_name" . $i} = $attacker_deets_results[$i]['troop_name']; but it only ever outputs the last entered name.
any help would be amazing. thanks.
Edit: To clear things up a little, my goal is to get the troop qty and multipy it by both atk and def then use these numbers to do some other math with the same fields from the defending player which I'll use to decrease the qty field.
Maybe you could modify your code a little and do the following.
Fetch the data about the user's troops in one query:
$results = $conn->query("SELECT troop_qty.user_id as uid, troop.troop_id, troop.troop_name, troop_qty.qty, troop.troop_atk, troop.troop_def FROM troop_qty join troop on troop.troop_id=troop_qty.troop_qty_id WHERE troop_qty.user_id=:attacker_id
Create a new array which contains information about all user's troops:
$userData = array(
'id' => $attacker_id,
'troops' => array());
while (($res = $results->fetch_assoc()) != null) {
$troopData = array(
'name' => $res['troop_name'],
'qty' => $res['qty'],
'atk' => $res['troop_atk'],
'def' => $res['troop_def']);
$userData['troops'][$res['troop_id']] = $troopData;
}
As result you would have something like this (vardump($userData)):
array (size=2)
'id' => int 2
'troops' =>
array (size=2)
1 =>
array (size=4)
'name' => string 'Fist Puncher' (length=12)
'qty' => string '100' (length=3)
'atk' => string '1' (length=1)
'def' => string '1' (length=1)
2 =>
array (size=4)
'name' => string 'Stick Waver' (length=11)
'qty' => string '100' (length=3)
'atk' => string '2' (length=1)
'def' => string '1' (length=1)
I don't know how exactly you want to manipulate the data later on in the game but I think this would ease the work.
Btw. I've thrown away fetchAll() function because I personaly think it is not a good practice to fetch all data into memory and then modify it or do whatever with it. It's a waste of the memory and when you would deal with a big amount of data, at some point that could cause you some troubles.
I have a table in mongo with the following structure:
array(
'Subscriber' => array(
'0' => 'Name 1',
'1' => 'Name 1',
'2' => 'Name 2',
)
)
I am using the following code to remove an item from subscriber:
$setData["subscriber.1"]=1;
$result = $this->mongo->pages->update($condition1, array('$unset' => $setData), array('safe' => true));
$result = $this->mongo->pages->update($condition1, array('$pull' => array('subscriber' => null)), array('safe' => true));
It works fine but gives Mongo cursor Error:Cannot apply $pull/$pullAll modifier to non-array
Anyone know why?
I am guessing that it is because of:
$result = $this->mongo->pages->update($condition1, array('$pull' => array('Pinned' => null)), array('safe' => true));
You have no Pinned in your document and it most definitely is not an array.
That is essentially what this error says: Pinned is not an array.
You did used single quotes (''). PHP will ignore the variables which are within single quotes. You have to remove the single quotes or replace them by double quotes.
This should work:
$setData["subscriber.1"]=1;
$result = $this->mongo->pages->update($condition1, array($unset => $setData), array('safe' => true));
$result = $this->mongo->pages->update($condition1, array($pull => array('Pinned' => null)), array('safe' => true));