I have a php file that returns a single number (i.e. 360). How can I get that number to appear in my android textview. I am able to do this with arrays that look like this:
{"success":1,"message":"Post Available!","posts":[{"name":"John Smith","id":"1"}, ...]}
but how can I do it with just a single number.
Here is the php:
<?php
include('connect.php');
$gettotal=mysql_query("SELECT * FROM records");
totalrecords=mysql_num_rows($gettotal);
echo json_encode($totalrecords);
?>
Thanks.
A number is STILL going to be just a number after your json_encode() it.
$num = 42;
$json = json_encode($num);
echo "$num / $json"
will just output 42 / 42 - there will be no practical difference between the two.
Related
I am 2 days into learning PHP and frustratingly I am struggling to GET and use a parameter passed into my API.
I have read the PHP $_GET documentation, it didn't take long, and also a number of SO pages about $_GET.
My use case is simple. I want to retrieve a list of records from MySQL db if they have a modified date greater than the passed in date. The function works if I hard code '2019-03-18 00:00:01' for example.
Using echo I can see I am getting the parameter.
In fact, copying the output from echo and using to update the function which I use in the argument returns the expected result.
I'm guessing this has been asked before but I can't find it. All the examples I read seem more challenging i.e. multiple variables or challenges with the function etc.
I must have made a newb mistake somewhere.
<?php
//Returns Lab Results Modified after the passed in date
include_once 'db_functions.php';
$db = new DB_Functions();
//Get JSON posted by Android Application
if (isset($_GET['date'])) {
$json = $_GET['date'];
echo $json;
// commenting out the next two line results in
// '2019-03-19 00:00:01' returning and not the json array.
$json = '2019-03-18 00:00:01';
echo $json;
$mod = $db->getLabResultsModifiedAfter($json);
$a = array();
$b = array();
if ($mod != false){
while ($row = mysqli_fetch_array($mod)) {
$b["ID"] = $row["ID"];
$b["last_modified"] = $row["last_modified"];
$b["LabRef"] = $row["LabRef"];
array_push($a,$b);
}
echo json_encode($a);
}
} else {
// Fallback behaviour goes here
}
?>
I haven't developed the app side function yet. I am using postman to pass in date of '2019-03-18 00:00:01' i.e. this for local.
Answer as provided by #aisby. I was using quotes in the passed in parameter.
The date you are passing in your URL does not following the format
'2019-03-18 00:00:01'. Try navigating to this URL ...
localhost:8080/its/… ... I have URL encoded the date 2019-03-18
00:00:01 in the querystring. I can see that you comment shows a date
query string starting with %27 which is the URL encoded single quote.
You should only send the value in query string variables. Not the
single or double quote string delimiters. – asiby 18 hours ago
I am trying to get a number value that is pulled from a mysql database to be formatted differently in a php document.
$oIteminfo = mysql_query("
SELECT Items.PartID, Items.VendorCost, Items.ProductName, Sum(Sales.AmountSold) AS SumOfAmountSold
FROM Items
LEFT JOIN Sales
ON Items.PartID = Sales.PartID
GROUP BY Items.PartID, Items.VendorCost, Items.ProductName
HAVING Items.PartID=$iPartID;
") or die(mysql_error());
while($row = mysql_fetch_array($oIteminfo))
{
ECHO "<h1>WOWAuctionSales</h1>";
ECHO "<img src=\"http://www.wowauctionsales.com/images/".$iPartID.".jpg\"height='35' width='35'>";
ECHO "<h1>".$row['ProductName']."</h1><br>";
ECHO "<B>Total Sold:</B>".$row['SumOfAmountSold']."<br>";
ECHO "<B>Vendor Price:</B>".$row['VendorCost']."<br>";
For example in one instance the result will pull the following number 162356 I want that number to be formatted to say 16g 23s 56c or 00g 00s 00c I have looked a fprintf, sprintf and printf, but they do not seem to do the trick. I have seen this formatting done before in php but I cannot seem to figure it out how to do it myself.
i can think of a dozen options here is one
//$number ="162356";
$number =$row['VendorCost'];
echo substr($number,0,2).'g '.substr($number,2,2).'s '.substr($number,4,2).'c';
I have real data type values in my database. But when i try print (php) i can see very long numbers:
512.2 501.12 506.66 in db
$variable = $row['column_name'];
then i receive values like:
512.20001220703 and
501.11999511719
how to parse in to normal format?
you can try this
$variable = round($row['column_name'],2); //display 2 decimal
i'm using facebook fql to get some data , these data are in array
when i use json to decode the result it gives me users id in that style
$result = json_decode($result, true);
the result from array is 1.0000148607246E+14
instead of 10000148607246
i know its the same number but when i use that result to get new data from facebook "request back" it gives me error with id
the question now is how to convert 1.0000148607246E+14 to 10000148607246 in php
Add the precision setting on to the top of the PHP tag.
<?php
echo $var=1.0000148607246E+14; //"prints" 1.0000148607246E+14
ini_set('precision', 18); // <------------------ Add this on to the top of your code
echo $var=1.0000148607246E+14; //"prints" 1.0000148607246
number_format(1.0000148607246E+14,0,'','');
You can read more here
Output
100001486072460
Hi to all!
I have a query that gets the name and an id.
The results is like this :
54 - Rian Ree Barrientos
I wanted to get the number 54.
I used echo (int)$_GET['number'];
But the result is "0". How can I get the number?
You can't get it that way, you need to make two query string vars for it eg:
<a href="somepage.php?number=54&str=some_string">
Make sure that if you do so, you use the urlencode function.
Now you can use:
echo (int) $_GET['number'];
And:
echo $_GET['str'];
Otherise you can use the explode function to get the two values by specifying the - delimiter.
I think you want id from the value you get from the query
I think you want something like following
$strPrice1 = mysql_query("SELECT id, name FROM table_name where id=".$_GET['id'] );
$strPrice = mysql_fetch_array($strPrice1);
echo (int)$_strPrice['id'];
But the result is "0". How can I get the number?
Really?
When I run:
print (int)('54 - Rian Ree Barrientos') . "\n";
I get 54
(php v 5.1.6)
Maybe $_GET['number'] doesn't contain what you think.
C.