I'm trying search process input with php and send to json
the json output like this:
{
"err": 0,
"msg": "",
"data": {
"f": 0,
"hotel": [
{
"att": 25147,
"name": "Crowne Plaza Changi Airport",
"city": "Singapore",
"country": "Singapore"
}
],
"city": [
{
"att": "-2679652",
"name": "Singapore",
"region": "",
"country": "Singapore",
"nr_hotels": ""
}
]
}
}
i was trying with test array but not work
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){
$search_array = array(
"err"=>"","msg"=>"",
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']),
"city"=> array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country'])
);
array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>
i change with this , but show only 1 record not all similar from key search
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($hasil=mysql_fetch_array($result)){
$search_array = array(
"err"=>intval("0"),"msg"=>"","data"=>array("f"=>intval("114"),
"hotel"=>array(
$hotel_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
)),
"city"=>array(
$city_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"city" => $hasil['city'],
))
));
}
echo json_encode($search_array);
?>
please help how the syntax for array thank
it's similar process with this site http://www.myhotelfinder.com/id/home/dohttp/predict?q=singapore
thank you this json output what i mean
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);
array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>
kiss ^^
I think you are asking how you need to format your array within the while block in order to achieve the JSON output.
I think you could try the following array declaration:
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){
$search_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']
),
"city" => array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country']
)
)
);
array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>
UPDATE:
Ok, I am starting to see a but more what you might be doing. Try the following:
<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);
// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);
array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>
Related
I need to remove Jackie Jackson from this array, I tried unset(I do not want to use key), array_diff, array_search. Nothing is working for me.
$employeeList= array(
array(
"ID" => "ID",
"Name" => "Name",
"Surname" => "Surname",
),
array(
"ID" => 1,
"Name" => "John",
"Surname" => "Smith",
),
array(
"ID" => 2,
"Name" => "Jackie",
"Surname" => "Jackson",
),
array(
"ID" => 3,
"Name" => "Chris",
"Surname" => "Jones",
),
array(
"ID" =>4,
"Name" => "Amanda",
"Surname" => "Cullen",
),
array(
"ID" =>5,
"Name" => "Jeremy",
"Surname" => "Goodwin",
),
);
if you want to unset you can use the id of Jackie Jackson to match his key.
$id = 2;
unset($employeeList[array_search($id,array_column($employeeList, "ID"))]);
Have fun :)
Array filter to check if the name and surname match. The string must contain name and surname separated by a space in this case so we can "explode" and pick up each individually. If name or surname do not exist in the "name_to_exclude" the function will simply return the original array.
$name_to_exclude = "Jackie Jackson";
$exclude = explode(' ', $name_to_exclude);
$employeeList = array_filter($employeeList, function($val) use ($exclude) {
if(array_key_exists(0, $exclude) && array_key_exists(1, $exclude)) {
return $val['Name'] != $exclude[0] && $val['Surname'] != $exclude[1];
}
return true;
});
This question already has answers here:
PHP Append an array to a sub array
(2 answers)
Closed 2 years ago.
I want to achieve below array format:
{
"success": true,
"results": [
{
"name" : "Choice 1",
"value" : "value1",
"text" : "Choice 1"
},
{
"name" : "Choice 2",
"value" : "value2",
"text" : "Choice 2"
}
]
}
However, I am using PHP and a foreach loop, to return some values from my database:
//Search for clients in our database.
$stmt = $dbh->prepare("SELECT * FROM customers");
$stmt->execute();
$showAll = $stmt->fetchAll();
I then have my first part of the array, and my foreach loop:
$data = array(
"success" => false,
"results" => array()
);
foreach ($showAll as $client) {
$data_array[] =
array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
Above only outputs:
[
{
"name":"Choice 1",
"value":"value 1",
"text":"Choice 1"
},
{
"name":"Choice 2",
"value":"value2",
"text":"Choice 2"
}
]
So it is missing the top part of my original array - but I want to loop through each database results in "results": [ ... ]
Try this
$data = array(
"success" => false,
"results" => array()
);
foreach ($showAll as $client) {
$data['results'][] = array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
$data['success'] = true; // if you want to update `status` as well
echo json_encode($data);
After creating $data_array array just add few lines which i have in my post.
Try this code snippet here(with sample input)
ini_set('display_errors', 1);
foreach ($showAll as $client)
{
$data_array[] = array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
// add these lines to your code.
$result=array();
$result["success"]=true;
$result["results"]=$data_array;
echo json_encode($result);
try this as you have an array inside the $data_array on Key "Results" so you should use "results" as a key also and then try to push data in that array
foreach ($showAll as $client) {
$data_array["results"][] =
array(
'name' => $client['name'],
'value' => $client['name'],
'text' => $client['name']
);
}
You can simply use json_encode and push it to your result array
$data = array(
"success" => false,
"results" => array()
);
$result = [
[
"name" => "Choice 1",
"value" => "value 1",
"text" => "Choice 1"
],
[
"name" => "Choice 2",
"value" => "value2",
"text" => "Choice 2"
]
];
$data['results'] = json_encode($result);
I am creating bar chart , i am unable to get output and generate chart.
here is my code
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row) {
array_push($arrData["data"], array(
"label" => $row["name"],
"value" => $row["value"]
)
var_dump($row["value"]);
);
}
}
I am not getting any output for ( var_dump($row["value"]);)
am i going in a right way?
Can any one help me in this.
Got my answer
if ($sql) {
$arrData = array(
"chart" => array(
"caption" => "Status stastics",
"showValues" => "0",
"theme" => "zune"
)
);
$arrData["data"] = array();
foreach($sql as $row)
{
$object_array =(array)$row;
array_push($arrData["data"], array(
"label" => $object_array["name"],
"value" => $object_array["value"]
)
);
}
}
?>
I am new in MongoDB, and I'm trying to create a text index.
After trying several hours, I have not accomplished anything.
I have an array like this:
//The keys and values are reversed to avoid duplicates in the array keys.
$arr = array(
'Personal' => array(
'PEPE' => "_id",
'd' => array(
"full_name" => "Pedro",
"last_name" => "Picapiedras",
"address"=> "La matanza 123",
"department"=> "Soporte"
), //d end
'PABLO' => "_id",
'd' => array(
"full_name"=> "Pablo",
"last_name"=> "Marmolejo",
"address"=> "Pica 123",
"department"=> "Soporte"
), //d end
)//personal end
);//arr end
I want to create an Index of the _id field so that to edit or view documents, access them through "Pablo" or "Pepe".
Is this possible? Could you give me a hand on how I can do this?
Edit
I have tried with
db.reviews.createIndex( { comments: "text" } )
and
$user = Array(
'_id' => "$id",
'name' => $name,
);
$query = Array( '_id' => "$id" );
$query = Array( '_id' => new MongoId("$id") );
Try this:
$arr = array(
'PEPE' => array(
"_id" => 'PEPE',
'd' => array(
"full_name" => "Pedro",
"last_name" => "Picapiedras",
"address" => "La matanza 123",
"department" => "Soporte"
) //d end
), //PEPE END
'PABLO' => array(
"_id" => 'PABLO',
'd' => array(
"full_name" => "Pablo",
"last_name" => "Marmolejo",
"address" => "Pica 123",
"department" => "Soporte"
), //d end
) // Pablo END
); //$arr end
function insertMongo($array) {
try {
$mongo = new MongoClient();
$db = $mongo -> dbName;
$collection = $db -> collectionName;
$collection -> insert($array);
} catch (MongoCursorException $e) {
die("Query failed ".$e - > getMessage());
}
} //insertMongo
//To execute:
foreach($arr as $a) {
insertMongo($a);
};
I have this piece of code:
<?php
require_once ('mercadopago.php');
$monto = $_POST['amount'];
$mp = new MP('XXXX', 'XXXXXXXXXXX');
$preference_data = array(
"items" => array(
array(
"title" => "item",
"quantity" => 1,
"currency_id" => "usd",
"unit_price" => HERE
)
)
);
$preference = $mp->create_preference ($preference_data);
?>
and I need to make the variable $monto that i define from a post send before to give its value to "Unit_price" where it says "HERE". I tried just writting $monto, but it didnt work.
there is some how i can do this? thanks u and sorry for my english. its not pretty good.
you can just use the variable, like:
//check if your POST data is not empty and assign some default value in case its empty
$monto = (!empty($_POST['amount'])) ? $_POST['amount'] : 0; //0 is default value
$preference_data = array(
"items" => array(
array(
"title" => "item",
"quantity" => 1,
"currency_id" => "usd",
"unit_price" => $monto
)
)
);
<?php
$preference_data = array(
"items" => array(
array(
"title" => "RosquinhaPvP - NickDoPlayer - VIP",
"quantity" => 1,
"currency_id" => "BRL",
"unit_price" => doubleval($monto)
)
)
);