I want to create json which contains all orders from datebase. I tried to write this code but it returns only one order.
$query = mysql_query("SELECT * FROM orders WHERE id_user = '".$userdata['user_login']."' ORDER BY `nom` ASC ");
if ($query)
{
$i = 0;
while ($row = mysql_fetch_assoc($query))
{
$where=$row["where"];
$time_min=$row["time_min"];
$time_max=$row["time_max"];
$date1=$row["date1"];
$date2=$row["date2"];
$from=$row["from"];
$id=$row["id"];
$orders =[
'from' => $from,
'where' => $where,
'time_min' => $time_min,
'time_max' => $time_max,
'date1' => $date1,
'date2' => $date2,
'id' => $id];
$i++;
}
}
$data = [
'count' =>$i,
'orders' => $orders
];
header('Content-type: application/json');
echo json_encode( $data );
exit;
Now the response looks like this:
But I want like this:
Append to the $orders array:
$query = mysql_query("SELECT * FROM orders WHERE id_user = '".$userdata['user_login']."' ORDER BY `nom` ASC ");
if ($query)
{
$i = 0;
while ($row = mysql_fetch_assoc($query))
{
$where=$row["where"];
$time_min=$row["time_min"];
$time_max=$row["time_max"];
$date1=$row["date1"];
$date2=$row["date2"];
$from=$row["from"];
$id=$row["id"];
$orders[] =[ // <--- The difference
'from' => $from,
'where' => $where,
'time_min' => $time_min,
'time_max' => $time_max,
'date1' => $date1,
'date2' => $date2,
'id' => $id];
$i++;
}
}
$data = [
'count' =>$i,
'orders' => $orders
];
header('Content-type: application/json');
echo json_encode( $data );
exit;
Related
I'm using Elasticsearch with PHP and after the required data mapping. I inserted the data via insert function as seen below:
public function insertNode ($event_id) {
global $conn1;
$client = $this->elasticclient;
$params = null;
$stmt = "SELECT
events.event_id,
events.event_title,
events.event_details,
DATE_FORMAT(events.added_date,'%d-%m-%Y') AS added_date
FROM events
WHERE events.event_id = $event_id";
$query = $conn1->prepare($stmt);
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$params = [
'index' => 'eventree',
'type' => 'events',
'id' => $row['event_id'],
'body' => [
'EVENT_TITLE' => $row['event_title'],
'EVENT_DETAILS' => $row['event_details'],
'START_TIME' => $row['start_time'],
'ADDED_DATE' => $row['added_date'],
'STATUS' => $row['status']
]
];
}
$responses = $client->index($params);
return true;
}
below you can see how I update data:
public function updateNode($event_id) {
global $conn1;
$client = $this->elasticclient;
$params = null;
$stmt = "SELECT
events.event_id,
events.event_title,
events.event_details,
events.status,
DATE_FORMAT(events.added_date,'%d-%m-%Y') AS added_date
FROM events
WHERE events.event_id = $event_id";
$query = $conn1->prepare($stmt);
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$params = [
'index' => 'eventree',
'type' => 'events',
'id' => $row['event_id'],
'body' => [
'doc' => [
'EVENT_TITLE' => $row['event_title'],
'EVENT_DETAILS' => $row['event_details'],
'START_TIME' => $row['start_time'],
'ADDED_DATE' => $row['added_date'],
'STATUS' => $row['status']
]]];
}
$responses = $client->update($params);
return true;
}
The problem is that the update process won't accept ids larger than 9, although there are records with ids greter than number 9. This is the error I get after running the update proccess:
{
"error": "{\"error\":{\"root_cause\":[{\"type\":\"document_missing_exception\",\"reason\":\"[events][10]: document missing\",\"index_uuid\":\"6l5vWeLLSb6CvcCsqTws9g\",\"shard\":\"1\",\"index\":\"eventree\"}],\"type\":\"document_missing_exception\",\"reason\":\"[events][10]: document missing\",\"index_uuid\":\"6l5vWeLLSb6CvcCsqTws9g\",\"shard\":\"1\",\"index\":\"eventree\"},\"status\":404}"
}
Your php code is wrong, you loop $rows and override $params variable all the time!
the correct code
public function insertNode($event_id) {
global $conn1;
$client = $this->elasticclient;
$params = null;
$stmt = "SELECT
events.event_id,
events.event_title,
events.event_details,
DATE_FORMAT(events.added_date,'%d-%m-%Y') AS added_date
FROM events
WHERE events.event_id = $event_id";
$query = $conn1->prepare($stmt);
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$params = [
'index' => 'eventree',
'type' => 'events',
'id' => $row['event_id'],
'body' => [
'EVENT_TITLE' => $row['event_title'],
'EVENT_DETAILS' => $row['event_details'],
'START_TIME' => $row['start_time'],
'ADDED_DATE' => $row['added_date'],
'STATUS' => $row['status']
]];
$responses = $client->index($params);
}
return true;
}
public function updateNode($event_id) {
global $conn1;
$client = $this->elasticclient;
$params = null;
$stmt = "SELECT
events.event_id,
events.event_title,
events.event_details,
events.status,
DATE_FORMAT(events.added_date,'%d-%m-%Y') AS added_date
FROM events
WHERE events.event_id = $event_id";
$query = $conn1->prepare($stmt);
$query->execute();
$rows = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$params = [
'index' => 'eventree',
'type' => 'events',
'id' => $row['event_id'],
'body' => [
'doc' => [
'EVENT_TITLE' => $row['event_title'],
'EVENT_DETAILS' => $row['event_details'],
'START_TIME' => $row['start_time'],
'ADDED_DATE' => $row['added_date'],
'STATUS' => $row['status']
]]];
$responses = $client->update($params);
}
return true;
}
My PHP Code is the following, I need some assistance please:
//DB connection
$result = mysqli_query($con,"SELECT * FROM `clients`");
$info = array();
$count = 0;
$meta = array('page' => 1, 'pages' => 1, 'perpage' => -1, 'total' => 14, 'sort' => "asc", 'field' => "ID");
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$info[$row['clientid']]['id'] = $row['id'];
$info[$row['clientid']]['name'] = $row['name'];
$info[$row['clientid']]['email'] = $row['email'];
$info[$row['clientid']]['cell'] = $row['cell'];
$count++;
}
$data = json_encode(array_values($info));
echo $data;
My Result;
[{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
The JSON should add the meta array with the following result:
{"meta":
{"page": 1,"pages": 1,"perpage": -1,"total": 3,"sort": "asc","field": ID"},
"data": [{"ID":1,"name":"A","email":"a#a.com","cell":"082"},
{"ID":2,"name":"B","email":"b#b.com","cell":"083"},
{"ID":3,"name":"C","email":"c#c.com","cell":"079"}]
},
Create array of required structure and json_encode it:
$data = json_encode(array(
'meta' => $meta,
'data' => array_values($info),
));
I have a working query but its only returning 1 row - where or what can I do to return all existing rows?
Model:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return array(
'info_id' => $info_query->row['info_id'],
'name' => $info_query->row['name'],
'amount' => $info_query->row['amount'],
'date_start' => $info_query->row['date_start'],
'date_end' => $info_query->row['date_end'],
'status' => $info_query->row['status'],
'date_added' => $info_query->row['date_added']
);
}
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
if ($info_options){
$json['info_options'] = array();
$json['info_options'][] = array(
'info_id' => $info_options['info_id'],
'name' => $info_options['name'],
'amount' => $info_options['amount'],
'date_start' => $info_options['date_start'],
'date_end' => $info_options['date_end'],
'status' => $info_options['status'],
'date_added' => $info_options['date_added']
);
}
When I try foreach() in the controller, I still only get one row.:
foreach ($info_options as $info) {
var_dump($info_options['name']);exit;
//var_dump($info_options['name']); results: Warning: Illegal string offset 'name'
}
In the model, when I dump:
$info_query I get 9 -- which is all of the rows I'm expecting.
Not particularly certain what I'm missing or doing wrong.
You're not looping over the result. Not in model nor in the controller. Though I don't know why you'd do this twice. Maybe I'm just not understanding your code.
Model:
$data = [];
if ($info_query->num_rows){
foreach ($info_query->result() as $row) {
$data[] = array(
'info_id' => $row['info_id'],
'name' => $row['name'],
'amount' => $row['amount'],
'date_start' => $row['date_start'],
'date_end' => $row['date_end'],
'status' => $row['status'],
'date_added' => $row['date_added']
);
}
return $data;
}
Controller:
$info_options = $this->model_extension_total_info->getInfo();
$json['info_options'] = array();
if ($info_options){
foreach ($info_options as $info) {
$json['info_options'][] = array(
'info_id' => $info['info_id'],
'name' => $info['name'],
'amount' => $info['amount'],
'date_start' => $info['date_start'],
'date_end' => $info['date_end'],
'status' => $info['status'],
'date_added' => $info['date_added']
);
}
}
Technically, you don't have to loop at all. You can just:
public function getInfo() {
$info_query = $this->db->query("SELECT * FROM `km_info` WHERE ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) AND status = '1'");
if ($info_query->num_rows){
return $info_query->result();
}
else
{
return false;
}
}
and
$info_options = $this->model_extension_total_info->getInfo();
// and that's it, just do whatever you need with $info_options
I try to make my custom code to update data to my custom database table
after I send the data I get:
Warning: mysqli_real_escape_string() expects parameter 2 to be string,
array given in .../wp-includes/wp-db.php on line 1156
My table structure is:
table name: wp_wlm_user_options
column names: ID, user_id, option_name, option_value
My code is:
if (isset($_POST['submit'])) {
$user_id = $_POST['user_id'];
$table = 'wp_wlm_user_options';
$meta = array();
foreach ($wlm_user_info as $key => $value) {
$get_meta = $value['option_name'];
array_push($meta, $get_meta);
}
//var_dump($meta);
$value = array(
'custom_firstname' => $_POST['first_name'],
'custom_lastname' => $_POST['last_name'],
'custom_text_dateofbirth' => $_POST['text_dateofbirth'],
'custom_radio_gender' => $_POST['radio_gender'],
'custom_landlinephone' => $_POST['landlinephone'],
'custom_GoogleHangoutsId' => $_POST['GoogleHangoutsId']
);
$format = array(
'%s',
'%s'
);
//print_r($meta,$value);
$data = array(
'user_id' => $user_id,
'option_name' => $meta,
'option_value' => maybe_serialize($value)
);
$format = array('%d','%s','%s');
$where = array(
'user_id' => $user_id,
'option_name' => $meta
);
$x = $wpdb->update($table, $data, $where, $format);
//$x = $wpdb->update($table, $data, $where);
if($x){
echo '<h1>data has been save</h1>' ;
}
Can anyone tell me what is wrong with this?
Due to your poor information we can only guess.
You might want something like this:
if (isset($_POST['submit']))
{
$user_id = $_POST['user_id'];
$table = 'wp_wlm_user_options';
$values = array(
'custom_firstname' => $_POST['first_name' ],
'custom_lastname' => $_POST['last_name' ],
'custom_text_dateofbirth' => $_POST['text_dateofbirth'],
'custom_radio_gender' => $_POST['radio_gender' ],
'custom_landlinephone' => $_POST['landlinephone' ],
'custom_GoogleHangoutsId' => $_POST['GoogleHangoutsId']
);
$format = array('%s');
$whereformat = array('%d','%s');
foreach($values as $key => $val)
{
$data = array(
'option_value' => maybe_serialize($val)
);
$where = array(
'user_id' => $user_id,
'option_name' => $key
);
$num_rows = $wpdb->update($table, $data, $where, $format, $whereformat);
if($num_rows !== false){
echo "<h1>$key has been saved</h1>" ;
}
}
};
The code above does multiple updates mapping the data to option_name and option_value for each update() invocation. You don't need to update fields of the WHERE clause with unchanged values.
You can find the official documentation on wordpress.org
now i am having a hard time in placing all my values in an associative array.
Because I can only get the last value from query. Not all the values. I can't spot the error.
Here's my code:
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array
That;s my problem guys i hope you can help me. Thanks.
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE cscart_order_data.type = 'I'
AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
echo "<pre>";
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = array(
'id' => $row1['order_id'],
'total' => $row1['total'],
'status' => $row1['status'],
'data' => unserialize($row1['data_serialize']),
'date' => $row1['date']
);
}
$range = array();
$test = array();
foreach($var_data as $data){
$id = $data['id'];
$total = $data['total'];
$cost = $data['data']['cost'];
$var_date = $data['date'];
$status => $data['status'];
$date_var = date('Y-m-d H:i:s',$var_date);
$grand_total = $total + $cost;
$cost_ratio = ($cost/$grand_total) * 100;
$paid_ratio = ($total/$grand_total) * 100;
$range[] = $cost_ratio;
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
}
echo "</table>";
print_r($test); //this will return the last index from my array
This piece of code is loading a scalar value
$test = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);
And should be
$test[] = array(
'id' => $data['id'],
'ratio' => $cost_ratio,
'status' => $status
);