I have really no idea what's happening. The story:
I use PDO for a SELECT statement on a database.
$sql = "SELECT a,b,c,performance,points,compare
FROM normvalues
WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
So that's fine and it is working. When I var_dump the $res variable I get something like:
array(6) {
["a"]=> string(2) "44"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "50.1"
["points"]=> string(1) "1"
["compare"]=> string(2) "-1"
}
[1]=>
array(6) {
["a"]=> string(2) "57"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "47.7"
["points"]=> string(1) "2"
["compare"]=> string(2) "-1"
}
[2]=>
array(6) {
["a"]=> string(2) "70"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "44.7"
["points"]=> string(1) "3"
["compare"]=> string(2) "-1"
}
...
That's also okay. But I have to sort the result another way. So I am doing:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
$this->normTable[$e['a']]['compare']=$e['compare'];
}
And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.
[1176]=>
array(4) {
[1]=> string(2) "50"
["compare"]=> string(2) "-1"
[2]=> string(2) "48"
[3]=> string(2) "45"
}
I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.
Update:
It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new:
I do an 'echo()' of my first variable in the normTable during the loop:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
$this->normTable[$e['a']]['compare']=$e['compare'];
}
and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?
UPDATE 2 and a big SORRY!
As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!
Thanks for your help.
Meh, I don't have 50 reputation; can't comment only answer.
If you replace a with be in your loop, you should get your expected result (given your data sample). I.e.:
foreach($res as $e){
$this->normTable[$e['b']][$e['points']]=$e['performance'];
$this->normTable[$e['b']]['compare']=$e['compare'];
}
But I'm not sure this really solves your problem. YMMV.
Related
I am trying to decode JSON
I am reading the JSON through STOMP. There are different JSON datasets so I need to work out which JSON dataset has come through. I do this by reading its title.
However there is one particular dataset I am having trouble reading
foreach (json_decode($msg->body,true) as $event) {
if(isset($event['schemaLocation'])) {
$schedule_id=($event['schedule']['schedule_start_date']);
$signalling_id=($event['schedule']['schedule_segment']['signalling_id']);
echo $schedule_id;
}
In the above example the isset function works fine and also $schedule_id obtains the right answer
However the $signalling_id gives an error of Undefined index:
Here is a dump of PART of the JSON (Its rather long............).The piece of JSON with the signalling_id is towards the end of the JSON. Any help to get the variable signalling_id much appreciated.
array(7) {
["schemaLocation"]=>
string(72) "http://xml.networkrail.co.uk/ns/2008/Train itm_vstp_cif_messaging_v1.xsd"
["classification"]=>
string(8) "industry"
["timestamp"]=>
string(13) "1410374918000"
["owner"]=>
string(12) "Network Rail"
["originMsgId"]=>
string(47) "2014-09-10T18:48:38-00:00vstp.networkrail.co.uk"
["Sender"]=>
array(3) {
["organisation"]=>
string(12) "Network Rail"
["application"]=>
string(4) "TOPS"
["component"]=>
string(4) "VSTP"
}
["schedule"]=>
array(11) {
["schedule_id"]=>
string(0) ""
["transaction_type"]=>
string(6) "Create"
["schedule_start_date"]=>
string(10) "2014-09-10"
["schedule_end_date"]=>
string(10) "2014-09-10"
["schedule_days_runs"]=>
string(7) "0010000"
["applicable_timetable"]=>
string(1) "N"
["CIF_bank_holiday_running"]=>
string(1) " "
["CIF_train_uid"]=>
string(6) "W64017"
["train_status"]=>
string(1) "1"
["CIF_stp_indicator"]=>
string(1) "O"
["schedule_segment"]=>
array(1) {
[0]=>
array(20) {
["signalling_id"]=>
string(4) "5Y75"
["uic_code"]=>
string(0) ""
["atoc_code"]=>
string(0) ""
["CIF_train_category"]=>
string(2) "EE"
["CIF_headcode"]=>
string(0) ""
["CIF_course_indicator"]=
............................................
schedule_segment is itself an array, so instead of
['schedule']['schedule_segment']['signalling_id']);
that should probably be
['schedule']['schedule_segment'][0]['signalling_id']);
As you can see in the var dump, signalling_id is inside another array. Use:
$signalling_id=($event ['schedule']['schedule_segment'][0]['signalling_id']);
If that one element array with key 0 is not constant throughout, you may need some logic to figure out what it is in each iteration.
Hello awesome programmers!
I am so sorry for the the novice question, however, I am having trouble finding a solution to my problem. I am attempting to run a for each loop through my array passed from my controller, however, the data being outputted is not the same as when I run a var_dump($array). I am thinking perhaps that I need to iterate through this object maybe? However, when I attempt to do so, I get a non-object error.
Controller:
$data['user_details'] = $this->ion_auth->user()->row();
View:
var_dump($user_details);
foreach($user_details as $item){
echo $item['email'];
}
The output of this is : "21nfn4111NR12" but should be roger#peterson.net!
I have also tried the object form:
var_dump($user_details);
foreach($user_details as $item){
echo $item->email;
}
However, it results in error trying to get property of non-object!
When I run the var dump I get the following:
object(stdClass)#21 (17) {
["id"]=>
string(1) "2"
["ip_address"]=>
string(14) "119.132.127.01"
["username"]=>
string(12) "roger petereson"
["password"]=>
string(40) "fdZxF/RQo4nZKmbA5XQlwefbc8f8e5c74899c3d0"
["salt"]=>
NULL
["email"]=>
string(19) "roger#peterson.net"
["activation_code"]=>
NULL
["forgotten_password_code"]=>
NULL
["forgotten_password_time"]=>
NULL
["remember_code"]=>
string(22) "44hjOlloLTIrkSrjSBVNie"
["created_on"]=>
string(10) "1404939094"
["last_login"]=>
string(10) "1405099607"
["active"]=>
string(1) "1"
["first_name"]=>
string(6) "Roger"
["last_name"]=>
string(5) "Peterson"
["is_owner"]=>
string(1) "1"
["user_id"]=>
string(1) "2"
}
The following line:
$this->ion_auth->user()->row();
returns an object not an array (check your var_dump output), so you just need to
echo $user_details->email;
Very sorry to be asking one of these questions, but I have been trying for the past hour to figure this one out and I am just plain stuck.
I am trying to update a lot of columns at the same time. I am using PDO.
Some PHP code:
$db=db_connect();
$wid=$_POST['wid'];
$time=time();
$username=$_SESSION['s_username'];
$arr=explode("|", $_POST['idata']);
array_push($arr, $time,$username,$wid );
$statement = $db->prepare("UPDATE widgets SET file_id=?,delay=?,title=?,instructions=?,width=?,height=?,bg_color=?,bg_image=?,border-radius=?,offer_table_border_width=?,offer_table_border_color=?,offer_table_link_color=?,offer_table_link_hover_color=?,offer_table_bg1=?,offer_table_bg2=?,head_font_size=?,head_font_color=?,instructions_font_size=?,instructions_font_color=?,time=? WHERE username=? AND wid=?");
$statement->execute($arr);
var_dump($arr);
The code comes from an AJAX request in the format "var1|var2|var3..." etc. This information is split by the "|" delimiter and then a few variables are added, then the table should be updated.
Here is the result of the var_dump (this is what $arr equates to):
array(22) { [0]=> string(2) "40" [1]=> string(1) "0" [2]=> string(9) "My Title!" [3]=> string(16) "My Instructions!" [4]=> string(3) "700" [5]=> string(3) "500" [6]=> string(4) "#FFF" [7]=> string(27) "http://background-image.png" [8]=> string(1) "0" [9]=> string(1) "1" [10]=> string(7) "#81ABD6" [11]=> string(7) "#0588C6" [12]=> string(7) "#81ABD6" [13]=> string(4) "#FFF" [14]=> string(7) "#DDEEFF" [15]=> string(2) "24" [16]=> string(7) "#2779AA" [17]=> string(2) "18" [18]=> string(7) "#2779AA" [19]=> int(1349486490) [20]=> string(15) "myuser" [21]=> string(6) "2LvS4c" }
And once again, the PDO statement:
$statement = $db->prepare("UPDATE widgets SET file_id=?,delay=?,title=?,instructions=?,width=?,height=?,bg_color=?,bg_image=?,border-radius=?,offer_table_border_width=?,offer_table_border_color=?,offer_table_link_color=?,offer_table_link_hover_color=?,offer_table_bg1=?,offer_table_bg2=?,head_font_size=?,head_font_color=?,instructions_font_size=?,instructions_font_color=?,time=? WHERE username=? AND wid=?");
The result from this: There are NO ERRORS returned from errorInfo(). There IS a row where username="myuser" and wid="2LvS4c", but it is simply not updated. There must be some sort of syntax error that I can't see, and is not being returned with errorInfo().
I tried with a simpler query:
$statement = $db->prepare("UPDATE widgets SET file_id=? WHERE username=? AND wid=?");
$statement->execute(array($arr[0], $username, $wid));
And it works like a charm! So what is wrong with the big query? Thanks!
This page lists the allowable characters in identifiers for MySQL, and - (minus) doesn't appear to be one of them.
First, are you certain that border-radius is the correct column name? It uses a minus sign whereas all the others use the underscore, _. At a bare minimum, you should maintain some consistency in your naming.
Other than that, start removing one column at a time from the update statement until it works. If it's a problem with a specific column (or possibly command length, however unlikely), this sort of "change one thing and retry" strategy can pinpoint the problem.
I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure out how to do it. I've tried $data->[question(13), option(0)] and $data->question(13). (I tried to look this up but I'm not sure what the notation means, so I'm not sure what I'm looking for)
object(stdClass)#133 (36) {
["id"]=>
string(1) "1"
["contact_id"]=>
string(0) ""
["status"]=>
string(8) "Complete"
["is_test_data"]=>
string(1) "0"
["datesubmitted"]=>
string(19) "2012-04-19 17:11:00"
["[question(5)]"]=>
string(11) "C. 40%, 40%"
["[question(9)]"]=>
string(47) "D. EBITDA and Free cash flow are the same thing"
["[question(10)]"]=>
string(48) "A. Accounts Payable as % of sales would increase"
["[question(11)]"]=>
string(20) "E. None of the above"
["[question(12)]"]=>
string(97) "A. A larger portion of initial investment is equity which can increase exit return potential."
["[question(13), option(0)]"]=>
string(2) "20"
["[url("embed")]"]=>
string(0) ""
["[variable("STANDARD_IP")]"]=>
string(13) "38.107.74.230"
["[variable("STANDARD_LONG")]"]=>
string(10) "-73.976303"
["[variable("STANDARD_LAT")]"]=>
string(9) "40.761902"
}
Either use extended object access notation:
$data->{'[question(13), option(0)]'}
Or just ask for normal array and use it as normal array.
json_decode($json, true);
try this
echo $data->{'[question(13), option(0)]'};
I cannot seem to figure out how to send to a group in my list using mailChimp API
My code looks like this:
$conditions = array('field'=>'interests-1', 'op'=>'all', 'value'=>'myGroup');
$opts = array('match'=>'any', 'conditions'=>$conditions);
$retval = $api->campaignSegmentTest($listId, $opts);
But this yields bool(false). When fetched by
$retval = $api->listInterestGroupings($listId);
my list looks like this:
array(1) {
[0]=>
array(5) {
["id"]=>
int(1)
["name"]=>
string(10) "myList"
["form_field"]=>
string(5) "radio"
["display_order"]=>
string(1) "0"
["groups"]=>
array(5) {
[0]=>
array(4) {
["bit"]=>
string(1) "1"
["name"]=>
string(9) "myGroup"
["display_order"]=>
string(1) "1"
["subscribers"]=>
int(1)
}
[1]=>
array(4) {
["bit"]=>
string(1) "2"
["name"]=>
string(9) "myGroup_2"
["display_order"]=>
string(1) "2"
["subscribers"]=>
int(1)
}
}
}
}
I have looked in the API documentation and searched for the answer, but cannot seem to figure it out. Grateful for help!
Looks like you are using the PHP wrapper - the first thing to do, like the examples included with it do, is to check for any errors by looking at $api->errorCode before messing with the $retval.
When you do that I'm certain you will see an error telling you that you haven't passed a proper "conditions" parameter since it is an array of arrays, not an array.