So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
I currently handle MongoDB with PHP.
I am trying to process data on Android using that value.
PHP:
public function Find($collection, $query = null, $fields = null) {
/* init */
$this->mClient = new MongoClient(...);
$this->mDBName = $dbName;
$this->mDB = $this->mClient->{$this->mDBName};
if(!isset($this->mDB)) {
// TODO
return;
}
/* find query */
$coll = $this->mDB->{$collection};
if(isset($query)) {
if(isset($fields)) $cursor = $coll->find($query, $fields);
else $cursor = $coll->find($query);
} else {
$cursor = $coll->find();
}
return json_encode(iterator_to_array($cursor, false));
}
ANDROID:
// Get String From PHP
// ex) [{"_id":{"$id":"59ad4d2425b572b7124be684"},"name":"\uacf5\ud3ec"},{"_id":{"$id":"59ad4d3625b572b7124be69a"},"name":"SF"}]
String result = getHttpData(getInstance().mStrUrl, data);
// In this part, data processing is done
List<DBObject> arr = new ArrayList<DBObject>();
//JSONObject json = new JSONObject(result);
JSONArray jsonArray = new JSONArray(result);
int len = jsonArray.length();
for (int i=0;i<len;i++){
String json = jsonArray.get(i).toString();
//Object o = com.mongodb.util.JSON.parse(result);
Object o = com.mongodb.util.JSON.parse(json);
DBObject dbObj = (DBObject) o;
arr.add(dbObj);
}
In the above case, referring to "_ id" will return BasicDBObject. It is necessary to receive an ObjectID.
Likewise for child document "_id" should be ObjectID.
HOW?
<?php
$con=mysqli_connect("localhost","root","");
mysqli_select_db($con,"qstatslite")or die("error");
$q = mysqli_query($con,"SELECT * FROM queue_stats ORDER BY queue_stats_id DESC LIMIT 20");
$return_arr = array();
while ($row = mysqli_fetch_array($q)) {
$row_array['queue_stats_id'] = $row['queue_stats_id'];
$row_array['datetime'] = $row['datetime'];
$row_array['qname'] = $row['qname'];
$row_array['qagent'] = $row['qagent'];
$row_array['qevent'] = $row['qevent'];
$row_array['info1'] = $row['info1'];
$row_array['info2'] = $row['info2'];
$row_array['info3'] = $row['info3'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
?>
Output:
[{"queue_stats_id":"198191","datetime":"2016-06-16 17:32:32","qname":"5","qagent":"50","qevent":"7","info1":"2","info2":"91","info3":"1"},{"queue_stats_id":"198190","datetime":"2016-06-16 17:31:01","qname":"5","qagent":"50","qevent":"10","info1":"2","info2":"1466069459.6496","info3":"1"},{"queue_stats_id":"198188","datetime":"2016-06-16 16:28:41","qname":"5","qagent":"53","qevent":"8","info1":"2","info2":"113","info3":"1"},{"queue_stats_id":"198187","datetime":"2016-06-16 16:26:48","qname":"5","qagent":"53","qevent":"10","info1":"2","info2":"1466065606.6445","info3":"1"},{"queue_stats_id":"198185","datetime":"2016-06-16 15:47:25","qname":"5","qagent":"50","qevent":"7","info1":"4","info2":"454","info3":"1"},{"queue_stats_id":"198183","datetime":"2016-06-16 15:39:51","qname":"5","qagent":"50","qevent":"10","info1":"4","info2":"1466062787.6382","info3":"3"},{"queue_stats_id":"198179","datetime":"2016-06-16 15:27:45","qname":"5","qagent":"50","qevent":"8","info1":"5","info2":"339","info3":"1"},{"queue_stats_id":"198178","datetime":"2016-06-16 15:22:06","qname":"5","qagent":"50","qevent":"10","info1":"5","info2":"1466061721.6337","info3":"4"},{"queue_stats_id":"198176","datetime":"2016-06-16 15:18:16","qname":"5","qagent":"53","qevent":"7","info1":"2","info2":"50","info3":"1"},{"queue_stats_id":"198175","datetime":"2016-06-16 15:17:26","qname":"5","qagent":"53","qevent":"10","info1":"2","info2":"1466061444.6325","info3":"1"},{"queue_stats_id":"198173","datetime":"2016-06-16 15:14:06","qname":"5","qagent":"50","qevent":"7","info1":"5","info2":"60","info3":"1"},{"queue_stats_id":"198172","datetime":"2016-06-16 15:13:06","qname":"5","qagent":"50","qevent":"10","info1":"5","info2":"1466061181.6318","info3":"4"},{"queue_stats_id":"198170","datetime":"2016-06-16 14:52:52","qname":"5","qagent":"53","qevent":"7","info1":"3","info2":"50","info3":"1"},{"queue_stats_id":"198169","datetime":"2016-06-16 14:52:02","qname":"5","qagent":"53","qevent":"10","info1":"3","info2":"1466059919.6275","info3":"3"},{"queue_stats_id":"198167","datetime":"2016-06-16 14:49:50","qname":"5","qagent":"28","qevent":"1","info1":"1","info2":"1","info3":"2"},{"queue_stats_id":"198165","datetime":"2016-06-16 14:25:44","qname":"5","qagent":"53","qevent":"7","info1":"47","info2":"162","info3":"1"},{"queue_stats_id":"198164","datetime":"2016-06-16 14:23:02","qname":"5","qagent":"53","qevent":"10","info1":"47","info2":"1466058176.6227","info3":"5"},{"queue_stats_id":"198163","datetime":"2016-06-16 14:22:51","qname":"5","qagent":"53","qevent":"0","info1":"15000","info2":"","info3":""},{"queue_stats_id":"198162","datetime":"2016-06-16 14:22:35","qname":"5","qagent":"50","qevent":"0","info1":"0","info2":"","info3":""},{"queue_stats_id":"198161","datetime":"2016-06-16 14:22:30","qname":"5","qagent":"53","qevent":"0","info1":"15000","info2":"","info3":""}]
Hi, I have this php/json codes..
can someone teach me how to display this JSON to android using TableLayout or in Listview?
use the following snippet to parse the JsonArray.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String queue_stats_i = jsonobject.getString("queue_stats_i");
String datetime = jsonobject.getString("datetime");
String qname = jsonobject.getString("qname");
String qagent = jsonobject.getString("qagent");
String qevent = jsonobject.getString("qevent");
String info1 = jsonobject.getString("info1");
String info2 = jsonobject.getString("info2");
String info3 = jsonobject.getString("info3");
}
Hope it help
First: parse the json.
Second: create item layout and adapter for listView.
You can see here: listView
I am sending the following json to the server
{
"username":"abc#abc.com" ,
"password":"abc#123","access_key": "api_key",
"brands": [
{ "brandname": "Lee","xcoord": "1345",
"ycoord": "2345","color": {"colorId":8, "rvalue": "234",
"gvalue": "213","bvalue": "233" }
},
{ "brandname": "Pepe","xcoord": "432",
"ycoord": "4210","color": {"colorId":5, "rvalue": "234",
"gvalue": "213","bvalue": "233"}
}
],
"description": "free text",
"ocassion": 1, // an ocassion id goes here.
"other_tags": ["other1","other2"],
"upload_platform":"android|iOS|web"
}
When i try to read a specific object color, which resides brands array object as below I am unable to do so and the echo fails, printing nothing. I have never written php, its so easy in java to just use gson and define models that would fill every model up.
$userData = urldecode ( $_POST['form'] );
$json = json_decode ( $userData );
$brandTagsArr = $json->brands;
foreach ($brandTagsArr as $brandTag){
$brandName = $brandTag->brandName; // need to fetch the name and associate brand tag id
$xCoord = $brandTag->xcoord; //
$yCoord = $brandTag->ycoord;
$this->rest_image_upload_model->insertBrandTags($imageId, $brandName, $xCoord, $yCoord);
// insert colors
echo "insert brand tags <br>";
$color = $brandTag['color']; // returns nothing FAILS
$color = $brandTag->color; // returns nothing FAILS
echo "color id" . $color['colorId'];
$this->rest_image_upload_model->insertColorTag($imageId, $color['colorId'],$color['rValue'], $color['gValue'], $color['bValue']);
echo "insert color tags<br>";
// end inserting colors
}
the tag name for colors is rvalue,gvalue and bvalue, but you are using as rValue,gValue and bValue. I think thats the issue in your code.
$imageId = 1;
$a["username"] = "abc#abc.com";
$a["password"] = "abc#123";
$a["access_key"] = "api_key";
$a["description"] = "free text";
$a["ocassion"] = "1";
$a["brands"][0]["brandName"] = "Lee";
$a["brands"][0]["xcoord"] = "1345";
$a["brands"][0]["ycoord"] = "2345";
$a["brands"][0]["color"]["colorId"] = "8";
$a["brands"][0]["color"]["rvalue"] = "234";
$a["brands"][0]["color"]["gvalue"] = "213";
$a["brands"][0]["color"]["bvalue"] = "432";
$a["brands"][1]["brandName"] = "Lee";
$a["brands"][1]["xcoord"] = "1345";
$a["brands"][1]["ycoord"] = "2345";
$a["brands"][1]["color"]["colorId"] = "8";
$a["brands"][1]["color"]["rvalue"] = "234";
$a["brands"][1]["color"]["gvalue"] = "213";
$a["brands"][1]["color"]["bvalue"] = "432";
$json = json_decode(json_encode($a));
$brandTagsArr = $json->brands;
foreach ($brandTagsArr as $brandTag) {
// print_r($brandTag->color);exit;
$brandName = $brandTag->brandName; // need to fetch the name and associate brand tag id
$xCoord = $brandTag->xcoord; //
$yCoord = $brandTag->ycoord;
// $this->rest_image_upload_model->insertBrandTags($imageId, $brandName, $xCoord, $yCoord);
echo $imageId."===>".$brandName."===>".$xCoord."===>".$yCoord."<br>";
// insert colors
echo "insert brand tags <br>";
// $color = $brandTag['color']; // returns nothing FAILS
$color = $brandTag->color; // returns nothing FAILS
echo "color id =>" . $color->colorId;
echo $imageId."===>".$color->colorId."===>".$color->rvalue."===>".$color->gvalue."===>".$color->bvalue."<br>";
echo "insert color tags<br>";
// end inserting colors
}
For your convenience i ve created an array encoded and decoded there only.Hope it helps.
In my android app user can upload up to 5 images for an item they have in their collection.
This is what my android app java code is sending over to the PHP server via POST:
[user_id=83,
item_id=24,
item_number_of_photos=1,
item_image_filename_0=http://www.asdqwe.net/item_images/83_image_2014-02-07-16-44-12.jpg,
item_image_description_0=mouse]
This is the PHP Code that handles it:
if (!empty($_POST)) {
$user_id = $_POST["user_id"];
$item_id = $_POST["item_id"];
$item_number_of_photos = $_POST['item_number_of_photos'];
for ($x=0; $x<$item_number_of_photos; $x++) {
if(isset($_POST['item_image_filename_'.$x]) && !empty($_POST['item_image_filename_'.$x])) {
$item_image_filenames[$x] = $_POST['item_image_filename_'.$x];
}
if(isset($_POST['item_image_description_'.$x]) && !empty($_POST['item_image_description_'.$x])) {
$item_image_descriptions[$x] = $_POST['item_image_description_'.$x];
}
}
$query_add_item_photos = "INSERT INTO
product_photos (
product_id,
product_photo,
product_photo_added_by_user,
product_photo_description
)";
////////////////////////////////////////////////////////////
////ADD THE PHOTOS TO THE PHOTOS TABLE//////////////////////
////////////////////////////////////////////////////////////
try {
for($x = 0; $x<$item_number_of_photos; $x++) {
$query_add_item_photos+= " VALUES
(
:product_id,
:product_photo_filename_" . $x .",
:product_photo_added_by_user,
:product_photo_description_" . $x . "
)";
}
$input_parameters = array(
':product_id' => $item_id,
':product_photo_added_by_user' => $user_id,
);
for($x = 0; $x<$item_number_of_photos; $x++) {
$input_parameters[':product_photo_filename_' . $x] = $item_image_filenames[$x];
$input_parameters[':product_photo_description_' . $x] = $item_image_descriptions[$x];
}
$sth = $connection->prepare($query_add_item_photos);
$sth->execute($input_parameters);
} catch(PDOException $pe) {
$response["success"] = $http_response_server_error;
$response["message"] = $http_response_server_error . $pe . $query_add_item_photos;
die(json_encode($response));
}
$response["success"] = $http_response_success;
$response["message"] = "WE MADE IT";
die(json_encode($response));
$connection = null;
} else {
$response["success"] = $http_response_bad_request;
$response["message"] = $http_message_bad_request;
die(json_encode($response));
$connection = null;
}
At the end when I run this, I get a PDO error saying that the query = "0".
I have only basic understanding of PHP so this is huge pain for me
Local Variables Issue
In your for loop, you are assigning values to $item_image_filenames[$x] array element and also to $item_image_descriptions[$x] array element.
for ($x=0; $x<$item_number_of_photos; $x++) {
...
$item_image_filenames[$x] = $_POST['item_image_filename_'.$x];
...
$item_image_descriptions[$x] = $_POST['item_image_description_'.$x];
}
But (assuming you have posted all of your code), these 2 arrays fall out of scope and disappear as soon as you exit your for loop. They weren't defined before your for loop, which means that they are being defined local to your for loop.
The result of this is that, later, when you attempt to reference these arrays, they don't exist, and they have no values. So, later on in your code...
for($x = 0; $x<$item_number_of_photos; $x++) {
$input_parameters[':product_photo_filename_' . $x] = $item_image_filenames[$x];
$input_parameters[':product_photo_description_' . $x] = $item_image_descriptions[$x];
}
... is going to result in assigning to $input_parameters values that don't exist.
To solve this problem, define $item_image_filenames and $item_image_descriptions before you enter your for loop. This way, they will continue to exist inside and after your for loop. You can do this:
$user_id = $_POST["user_id"];
$item_id = $_POST["item_id"];
$item_number_of_photos = $_POST['item_number_of_photos'];
// Define empty arrays so that I can use them throughout my code.
$item_image_filenames = array();
$item_image_descriptions = array();
for ($x=0; $x<$item_number_of_photos; $x++) {
...
String Concatenation Syntax
I also noticed that your line of code:
$query_add_item_photos+= " VALUES
...
... is not the correct way to do string concatenation in PHP. You want to use .= instead of +=. In PHP, strings are concatenated with ., as in the example: $string1 = $string2 . $string3;
Your code should instead be:
$query_add_item_photos .= " VALUES
...