I'm executing this query on PHP:
$query = 'SELECT email__c, firstname__c FROM user__c';
$result = $mySforceConnection->query($query);
for($i = 0; $i < count($result->records); $i++) {
print_r($result->records[$i])."<br/>\n";
echo("<span>done</span><br/>\n");
}
I just have 2 elements in my database and the output is:
done
done
How I can print the values?
Salesforce returns an array of objects, try something like this instead:
$i=0;
foreach ($response->records as $record) {
$i++;
echo "\n".$i.": \n";
print_r($record);
echo "THE ID IS: ".$record->Id."\n";
}
Another point to remember with salesforce query results is if the field is empty they won't even return it in the object, so you may need to do something like this:
if (isset($record->Name)) { echo "The name is $record->Name\n"; } else { echo "name is blank\n"; }
Related
I'm wondering if there is a quick way to do the following:
I have a PHP object with multiple attributes. For example
$person;
$person->height = 165;
$person->name = 'john';
I would like to dump this into an SQLite MySQL DB while creating each column automatically from the attribute names of the PHP object. So for example, if I were to dump the above object into mySQL, it would look something like this:
Table : people
Name(Column, VARCHAR) - "John"
Height(Column, INT) - 165
The reason I am asking this is because the number of attributes is growing constantly, and having to create and manage the table columns manually is a lot of work. I am wondering if there is an automated way of doing this.
First you need to convert object into array and then you can iterate through it and can create table and insert values in it.
Something like below:
Step 1: Convert object to array
Step 2: Get keys(fields) and values out of array
Step 3: Generate sql queries
<?php
//Step 1: convert object to array
//$persion = (array) $yourObject;
//Step 2: get keys(fields) and values out of array
$person = array(
"height" => "165",
"name" => "john",
"age" => "23"
);
function data_type($val) {
if(is_numeric($val)) {
return "int";
}
else {
return "varchar(15)";
}
}
//Step 3: sql query, only including sql query
function create_table($person) {
$create = "CREATE TABLE IF NOT EXISTS people";
$ctr = 0;
foreach($person as $key => $value) {
if($ctr == 0) {
$field_query = $key." ".data_type($value);
} else {
$field_query .= ", ".$key." ".data_type($value);
}
$ctr++;
}
echo $create .= " (".$field_query.")";
echo "<br/>";
}
create_table($person);
function insert_table($person) {
$ctr = 0;
foreach($person as $key => $value) {
if($ctr == 0) {
$field_query = $key;
$value_query = $value;
} else {
$field_query .= ", ".$key;
$value_query .= ", ".$value;
}
$ctr++;
}
echo $insert = "INSERT INTO people"." (".$field_query.") VALUES (".$value_query.")";
}
insert_table($person);
?>
Hope this will help you in some way(y).
I am trying to do a select statement from my MySQL DB in PHP using PDO. I successfully create an array with the query results. But then, I want to filter out duplicate entries with the same date, thus creating an array of dates.
function grabUsefulDates() {
$sql = "SELECT DATETIME, pointcount FROM `fjbarrett`";
$sql_array = dbSelect($sql);
$datesarray = array();
$i = 0;
while($i < count($sql_array)) {
if (count($datesarray) == 0) {
$datesarray[] = datetimeConvertToDate($sql_array[$i]['DATETIME']);
echo "rollone";
} else {
foreach ($datesarray as $f => $date) {
if ($date !== datetimeConvertToDate($sql_array[$i]['DATETIME'])) {
echo $date;
echo " " . datetimeConvertToDate($sql_array[$i]['DATETIME']);
echo "<br>";
$datesarray[] = datetimeConvertToDate($sql_array[$i]['DATETIME']);
}
}
}
$i++;
}
var_dump($datesarray);
}
This code will not run, but when I remove the "add to array" line within the foreach loop, it will run. Any suggestions?
Simply use this query :
SELECT DISTINCT DATETIME, pointcount FROM `fjbarrett`;
Below is array stored in session. It works finely.
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon);
?>
And I call them out like this,
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
echo $id['addon'];
}
Now I want to add another array inside the above array to include date_from and date_to for
addon items ($id['addon']).
How can I do that and then call them out according to addon_id?
This is the part that stores addon items id in the array for now:
'addon'=>$addon
EDITED
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,array('d_from'=>$date_from,'d_to'=>$date_to))
This will give you the wanted output
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$id[(whatever your want here)] = $array;
}
}
And just a suggestion to give more sense to your code.
It will gives you an array like
array ('var'=>value, ..., array ('addon'=>addon_id, 'date_from'=>date_from, 'date_to'=>date_to))
foreach($_SESSION['cart'] as $total=>$id)
{
if ($id['addon'] == $wanted_addon_to_add_dates) {
$array['date_from'] = $date_from;
$array['date_to'] = $date_to;
$array['addon'] = $id['addon'];
$id['addon'] = $array;
}
}
foreach($_SESSION['cart'] as $total=>$id)
{
echo $total;
foreach( $id['addon'] as $key=>$value)
{
echo "$key - $value";
}
}
I assumed the dates were in $addon already. If not you can just add a variable to the array you already have, like
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> array('d_from'=>$date_from,'d_to'=>$date_to))
or
$dates = array('d_from'=>$date_from,'d_to'=>$date_to);
$_SESSION['cart'][$sum_1]=array('car_id'=>$car_id,'location'=>$location,'dropoff'=>$d_location,'date_value'=>$date_value,'date_value_2'=>$date_value_2,'total_days'=>$total_days,'addon'=>$addon,'dates'=> $dates)
In that case you have to replace 'addon' with 'dates' in the first code example.
I currently have 2 arrays where i would like to compare dates in. here are how my arrays are structured:
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');
The aim is to detect whether or not a value from $userdaysoff exists in the $bholidays array.
The above works great and detects that 26-05-2014 exists in both arrays, but if the $userdaysoff array looks like this:
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');
Then the duplicate date 26-05-2014 is not detected.
Is there any reason why this would be occuring?
here is how i run my code:
$results = array_intersect($bholidays, $userdaysoff);
if($results){
foreach($results as $result){
echo 'yes';
}
} else {
echo 'no';
}
Could you not quite simply use in_array?
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('23-05-2014','24-05-2014','25-05-2014', '26-05-2014');
$count = count($userdaysoff);
for($i = 0; $i == $count; $i++) {
if(in_array($userdaysoff[$i], $bholidays)) {
echo $userdaysoff[$i] . " is in array.";
}
}
$bholidays = array('05-05-2014','26-05-2014');
$userdaysoff = array('26-05-2014','27-05-2014','28-05-2014', '29-05-2014');
$results = array_intersect($bholidays, $userdaysoff);
if($results)
{
foreach($results as $result)
{
echo 'yes';
}
}
else
{
echo 'no';
}
Run this code and check it works fine..
The output is yes.
I have a result from my db query that's look like this when dump and the query is doing what is expected, but I am have a little problem getting the array values. I am using PHP PDO to get the result.
$result = $_stmt->fetchAll();
$row = count($result);
print_r($result);
Array ( [0] => Array ( [SUM(od_price * od_qty)] => 69.85 [0] => 69.85 )
[1] => Array ( [SUM(od_price * od_qty)] => 13.97 [0] => 13.97 )
) 69.8513.97
You can see that the result contains both an array and a string values. I have an option to get either the array or the string value. But I would rather to get the array values since the the string values are all togather. Can some one please explain what I am doing that's wrong in the foreach loop?
if($row == 2)
{
foreach ($result as $k)
{
echo $price_1 = $k[0][0]; // expected 69.85
echo $price_2 = $k[0][1]; // expected 13.97
}
unset($k);
}
I need to get the expected values, but instead I am getting the string values that are all togather.
After reviewing the solutions below, here is what I came up with that works well for what I wanted.
$result = $_stmt->fetchAll();
$row = count($result);
$price = "";
if($row == 2)
{
foreach ($result as $k)
{
$price .= $k[0].',';
}
}
// remove the last comma
$price = substr($price, 0, -1);
list($totalPurchase, $shippingCost) = explode(",",$price);
$orderAmount = $totalPurchase + $shippingCost;
echo 'The amount is: '.$orderAmount;
Try storing the values in an array instead of echoing them..
$price_1 = array(); $price_2 = array();
if($row == 2)
{
foreach ($result as $k)
{
array_push($price_1, $k[0][0]); // expected 69.85
array_push($price_2, $k[0][1]); // expected 13.97
}
// print_r($price_1); //should store all the price1s
// print_r($price_2); // should store all the price2s.
// uncomment to these lines to view array contents
}
Although you need to learn multidimensional arrays and foreach in general, to solve this particular task you need to use fetchAll with little addendum:
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
foreach ($result as $k)
{
echo $k;
}
replace your loop with this:
foreach ($result as $k) // iterate through all datasets
{
echo $k[0]; // echo price of dataset
}
It's already been answered, but i've seen in a comment that you want them seperate. So just concat a "<br>" or a new line space after your echo:
foreach ($result as $k)
{
echo $k[0]."<br>";
}