I'm using the php transmission rpc class (https://github.com/brycied00d/PHP-Transmission-Class) to retrieve torrent info. But I don't know how to retrieve data from an array object (level 5).
I retrieve data like
$torrent_name = $result['arguments']['torrents'][0]['name'];
The Info I want is like :
$torrent_client_name = $result['arguments']['torrents'][0]['peers']['clientName'];
and here is the function,
public function get ( $ids = array(), $fields = array() )
{
if ( !is_array( $ids ) ) $ids = array( $ids );
if ( count( $fields ) == 0 ) $fields = array( "id", "name"); //this array
$request = array(
"fields" => $fields,
"ids" => $ids
);
return $this->request( "torrent-get", $request );
}
I tried to make something like this but no result:
$fields = array( "id", "name", array("peers"=>"clientName"));
or
$fields = array( "id", "name", array("clientName"));
or
$fields = array( "id", "name", 'peers' => array('clientName'));
The error I get :
Notice: Undefined index: peers
and here the var_dump($result);
Related
I am learning Codeigniter and hitting some turbulence. One of my input fields collects an array of values (from check-boxes), while the rest are single values. I would like to insert the data into MySQLi in such a way that the number of inserted rows is equal to the number of values in the array. It looks like:
My Controller:
function request_rooms() {
if ($_POST) {
//room is an array or one or more values
$this->form_validation->set_rules('room[]', 'Select Box', 'trim|required|min_length[1]|max_length[25]');
$this->form_validation->set_rules('guest_id', 'Guest ID', 'trim|required|xss_clean|min_length[4]|max_length[20]');
$this->form_validation->set_rules('date_in', 'Check-in Date', 'trim|required|exact_length[10]');
$this->form_validation->set_rules('date_out', 'Check-out Date', 'trim|required|exact_length[10]');
if ($this->form_validation->run($this) == FALSE) {
$somedata = array('errors' => validation_errors());
$this->session->set_flashdata($somedata);
require("includes/load_view_reservation.php");
} else {
//Prepare data to send to Reservation model
$data = array(
'room_no' = ?
'guest_id' = $this->input->post('guest_id'),
'check_in' = $this->input->post('check_in'),
'check_in' = $this->input->post('check_in')
$this->reservation_model->insert_reservation($data);
}
}
}
The Problem:
My problem is how to handle the room array. I want to create a $data array for the data to be inserted into the Database. So suppose the room array input ($this->input->post('room[]')) is as follows:
$room = array(42, 123, 16);
I would like the $data array to be like:
$data = array
(
array(
"room_no" => "42",
"guest_id" => "2",
"date_in" => "2018-05-20",
"date_out" => "2018-05-27"
),
array(
"room_no" => "123",
"guest_id" => "2",
"date_in" => "2018-05-20",
"date_out" => "2018-05-27" ),
array(
"room_no" => "16",
"guest_id" => "2",
"date_in" => "2018-05-20",
"date_out" => "2018-05-27" )
);
I have a faint idea that a loop could accomplish something like this so that the room_no part of the array has each of the values from the array while the other values remain the same, but I can't see how to proceed.
Question:
How do I create a Multidimensional array like above in Codeigniter? Secondly, This far, I have only inserted (into MySQL) a simple one-dimensional array using a Codeigniter Model. How do I insert a Multidimensional array like the one above? Please help.
Hope this will help you :
First method insert individual record;
//$rooms = $this->input->post('room');
$rooms = array(42, 123, 16);
if ( ! empty($rooms))
{
foreach($rooms AS $room)
{
$data['room_no'] = $room;
$data['guest_id'] = $this->input->post('guest_id');
$data['check_in'] = $this->input->post('check_in');
$this->reservation_model->insert_reservation($data);
}
}
Your model method insert_reservation should be like this :
public function insert_reservation($data)
{
return $this->db->insert('table_name',$data);
}
Second method insert multiple (batch) record;
//$rooms = $this->input->post('room');
$rooms = array(42, 123, 16);
if( ! empty($rooms))
{
foreach($rooms AS $room)
{
$data[] = [
'room_no' => $room,
'guest_id' => $this->input->post('guest_id'),
'check_in' => $this->input->post('check_in'),
];
}
}
//print_r($data);
$this->reservation_model->insert_reservation($data);
In this case your model method insert_reservation should be like this :
public function insert_reservation($data)
{
return $this->db->insert_batch('table_name',$data);
}
For more : https://www.codeigniter.com/user_guide/database/query_builder.html#inserting-data
doesn't look that tough just iterate over your array
something like the following should do the job.
$room = array(42, 123, 16);
$arrData = [];
foreach($room AS $roomNo)
{
$arrData[] = [
'room_no' => $roomNo,
'guest_id' => $this->input->post('guest_id'),
'check_in' = $this->input->post('check_in'),
'check_in' = $this->input->post('check_in')
];
}
print_r($arrData);
I'm running into some issues getting the correct JSON output from my SQL query. Essentially what I'm struggling with is getting an array of options objects as opposed to singular option objects.
$query = 'SELECT matchup.matchupID, matchup_option.player_name, matchup_option.player_id FROM matchup
INNER JOIN matchup_option
ON matchup_option.matchupID= matchup.matchupID;';
$attachments = $db->query($query);
$data = array();
while ($attachment = $db->fetch_array($attachments)){
$data[] = array (
'id' => $attachment['matchupID'],
'options' => array(
array (
"name" => $attachment['player_name'],
"playerid" => $attachment['player_id']
)
)
);
//VAR_DUMP($attachment);
}
$data = array("matchup"=>$data);
print json_encode($data);
Gives me this output:
{
"matchup":[
{
"id":"111222",
"options":[
{
"name":"111",
"playerid":"111"
}
]
},
{
"id":"111222",
"options":[
{
"name":"222",
"playerid":"222"
}
]
}
]
}
And here's what I'm trying to get to:
{
"matchup":[
{
"id":"111222",
"options":[
{
"name":"111",
"playerid":"111"
},
{
"name":"222",
"playerid":"222"
}
]
}
]
}
I'd like to follow best practices as well as structure this appropriately, if there's a better way to go about this, please let me know!
You need to store $attachment['matchupID'] as an array key of $data:
$data = array();
while ($attachment = $db->fetch_array($attachments)){
if (!isset($data[$attachment['matchupID']])) {
$data[$attachment['matchupID']] = array (
'id' => $attachment['matchupID'],
'options' => array()
);
}
$data[$attachment['matchupID']]['options'][] = array (
"name" => $attachment['player_name'],
"playerid" => $attachment['player_id']
);
}
// use `array_values` to reindex `$data`
$data = array("matchup" => array_values($data));
print json_encode($data);
How to get the street value using 'php'?
{
_id : "001",
name : "fakename",
address : {
street : "12 Street",
city : "Cosmos",
}
}
Query :
$collection = database_name->collection_name;
$cursor = $collection->find ( array('name' => 'fakename' ), array( 'address.street' ) );
foreach ( $cursor as $doc ) {
echo $doc[ 'address.street' ];
}
Result :
$doc[ 'address.street' ]
value does not get printed.
$db = (new \MongoDB\Client("mongodb://localhost:27017"))->database_name;
$collection = $db->collection_name;
$cursor = $collection->find ( array('name' => 'fakename' ), array( 'address.street' ) );
foreach ($cursor as $doc) {
echo $doc['address']['street'];
}
PHP uses multidimensional arrays:
$doc['address']['street']
Hi Im working on parameter binding query Phalcon. Following is my code
$conditions = "client = :client: AND inv_date = :inv_date: AND date_sent = :date_sent: AND date_received = :date_received:";
$parameters = array(
"client" => $search_client,
"inv_date" => $invoice_date,
"date_sent" => $date_sent,
"date_received" => $date_received
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
Do I have use the AND correctly??
FYI its not working, if I try the following, it works:
$conditions = "client = :client: AND inv_date = :inv_date:";
$parameters = array(
"client" => $search_client,
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
All the fields exist in table, I think there is other way to use multiple AND
$conditions = "client = :client: AND inv_date = :inv_date:";
$parameters = array(
"client" => $search_client,
);
$invoices = Invoices::find(
array(
$conditions,
"bind" => $parameters
)
);
This code doesn't work because you pass just one parameter client, but have to pass 2 parameters: client and inv_date.
$parameters = array(
"client" => $search_client,
"inv_date" => $invoice_date,
);
Hence - you can use this option (because it really works) or you can use andWhere like #Niki Mihaylov advised.
I try to extract all filtered fields from Zend_Filter_Input in order to add extra fields and pass it to the model to insert or update, however I getting lost with extract piece:
$filters = Array( '*' => Array( 'StripTags', 'StringTrim' ) );
$data = new Zend_Filter_Input( $filters, Array( ), $this->_request->getParams( ) );
// line below does't work what I should use instead ?
$data = $data->toArray( );
foreach ( $extra_fields as $key => $value ) {
$data [ $key ] = $value;
} // FOREACH
I just looking to get all filtered fields as array of value pairs.
$data = $data->getEscaped();
Go and have a look here : http://zendframework.com/manual/en/zend.filter.input.html for more informations