Using data from a web-service in an iOS Application - php

I have to list the table entries from mysql in my iPhone app when a button is pressed. I am able to do it properly but it is just not the way I want it. I am using a PHP script to send Xcode what has to be printed.
First, I tried doing it using HTML table, but I didn't like the way it was printed with cells. Next I tried printing plain text by giving spaces(between columns) and \n for every new row. I used NSURL and loaded the webView to the iPhone. Looks good on browser but the same is not preserved when the iPhone tries to open it.
Is there a good way to do this? So I can just list the table entries without having to go through the traditional HTML table or any other idea is welcome.
Also, please try to be easily understood, as I am new to Obj-C, and PHP as well.
Thanks!!
Any thoughts on how I can do this in a UITableView..?? Do I have to return a string with component separation characters and fill in the tableView?

Output the results encoded in JSON. Send an a(sync) request to the server using NSURLConnection or using a third-party library such as AFNetworking. Parse the JSON using NSJSONSerialization, turning the results into an array/dictionary depending on the contents. Then parse the results into the UITableViewCell. It may be easier to subclass the cell so that you include the data that you'd like to use.
To encode the results from the database into JSON, you can use the method json_encode().

Related

How to pass field values from database hosted on a server to android app using php?

I have a database hosted on server whose field values have to be passed to my app. I would like to do with PHP. But people suggest me to use JSON too. Is JSON required? Please guide me how to pass the field values to android app.
JSON means JavaScript Object Notation and it's just a way of formatting your output in a standard way.
So, if you'd like to pass data from a database to an application, you'd need to implement a small API. This can be done using PHP. At this point, you can access data from your database using a browser and parametrizing your queries using url parameters.
PHP can render the data in a simple HTML table for example, but this is just a way of presenting your data. You can also use JSON.
This means that if you need the badges a user has earned, you'll use something like this:
<link_to_your_api>/index.php?method=getBadges&user=<user>
This in turn, will make a request to the database
<?php
// 1. connect to database
// 2. query for the information
// 3. get the result as array
$result = $db->getData();
echo json_encode($result);
?>
This is just an example, hope it helps.
link to json documentation: http://www.json.org/
You pass the data to your app, when it makes a request to your PHP script. JSON is handy because you can package your data in a format, that is both well readable by humans and machines. You can use the gson library then, to process the JSON data in your app.
Create REST api using any server side script PHP,nodejs or any you like which returns JSON response
call the rest api using http request which returns JSOn text
decode the JSON string to JSON object and use with your android code

JSON to a HTML Table

Well i still haven't solved this. Been at it for a couple of days now. What i have done already is: I have written a php script which echos my JSON data structure. I am now trying to write a Html script which will obtain the results in JSON format but echo this out in a html table. I coded my html page, however for some reason the table isn't showing up.
Ok what my json_encode($res->fetchAll()) echo looks like is this;
[{"name":"Victoria
Use data tables . It is very powerful and easy to use java script api. It provides a lot of features as well like sorting searching etc.
You only have to change the format of JSON and rest all will be taken care by datatables.
An example
Your out is not valid json, the echo "{$res->numRows()}<br/>"; invalidates it and needs to be removed.

Which thing is better and efficient for data display using AJAX Full template or JSON data

I am new to jquery and JSON and i am bit confused as what is best and professional approach.
Suppose i have the long list of data like song name , artist , author etc.
Now i dynamically want to dislay records from database.
I have two options
Here i return the full html and update that with the target element
Second is to retrive the JSON data full of songs info and then build that html with javascript and populate it.
I want to know which approach is better and used by high traffic sites
Second is better because of data size being sent over the network. Reducing it will improve the performance.
Write a function that will use JSON data to generate HTML elements.
If you're running a high traffic site with lots of data then the second solution, using JSON does have the advantage of only giving you the raw data and relying on the browser to generate the HTML to display the data.
Personally I would need to hear some really convincing arguments for using the first option at all.

JQ-GRID implementation in php file

Hi friends i am working on JQ-GRID. I want to show Image in specific column, But i don't know how to attach image in JQ-GRID. Can anybody help me or please send me some links, thanks
jqgrid is a feature monster. I tell this everybody who asks about it.
When jqgrid loads, a function is called which actually gets the data you want to display.
This is normally an ajax call to your php. As a result set of this function, you can just use xml or json.
I prefer json, so I build my result array and do a echo json_encode($myarray)
jQuery("#your_grid_id").jqGrid({ url : '/ajax/getjqgriddata.php'})
Now displaying pictures, there are different ways you can do that. You can either generate a <img src="wherever/mypicture1.png"></img>-link and hand it over in your result, or encode your picture binary data with base64 and deliver it with your result.
A more addvanced way is to use an so called formatter and just returning a id for the image.
This depends on you, but I would suggest to get confident with jqgrid, experiment with returning -links to get a feeling how jqgrid works.
There is plenty of good documentation at:
http://trirand.com/blog/jqgrid/jqgrid.html
Just take a look at it.

Is it possible to use two json_encode() in one php file?

I'm trying to use json to send text and blob image information from MySQL to iPhone App.
However, someone told me that I should separate text and blob image info because blob is technically not binary - thus I have to use base64_encode(). Well.... I'm going to create two php files but want to check if it's possible to do it in one php file. :/ I tried but n o luck yet... Is it even possible?
You can invoke json_encode, the function, as often as you want.
But throwing the output together is not going to work.
If you intended to do
print json_encode($data_array);
print json_encode($image_blob);
Then this will lead to an invalid JSON response.
Like {"data":123} {"image":"PNG%...."}
You need to combine the data and the image blob somehow if you want this to work in one response:
print json_encode(array_merge($data_ar, array("image"=>$image_blob)));
Yes, you just need to have some kind of conditional to decide which JSON document you are going to return… but if you are going to serve the image up separately then you are almost certainly better off just serving up the image and not encoding it in a JSON document.
There is no such restriction. You can use json_encode as many times as you need.

Categories