1

First of all, I have to admit that I am pretty bad at handling JSON. I need to ask for some help on a problem that is bothering me for around two week. So, I am trying to create a basic Web Service for an iOS/Android application. I am following the instructions given by the App developer to correctly create the API. Everything seemed pretty easy until I reached the point I had to create the getContents.php. This is my PHP code for fetching and echoing the data:

<?php
header('content-type:application/json;');
$article_id = $_GET["ArticleID"];


$link = mysql_connect('localhost','bridgeapp_user','') or die('Cannot connect to the DB');
mysql_select_db('bridgeapp',$link) or die('Cannot select the DB');  


mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);


if (isset($article_id)) {
    $query = "SELECT description AS `content`, 'http://offercat.com/media/com_jbusinessdirectory/pictures'+logoLocation AS `images` FROM vrfmp_jbusinessdirectory_companies WHERE id = '".$article_id. "'";
    $result = mysql_query($query,$link) or die('Errant query:  '.$query);

}

$rows = array();
while($r = mysql_fetch_assoc($result)) {
    $rows[] = $r;
//    $rows = str_replace("name", "title", "$rows");
}

echo json_encode($rows);

@mysql_close($link);

What I get from that code is this:

[{"content":"<p>asdasdasd<\/p>","images":"0"}]

And what I would like to get looks something like this:

{
"content": "<p><img src=\"http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_screenshots.png\" alt=\"DigiDiet Screenshots\" width=\"100%\"/></p><p>Το <a href=\"http://www.digidiet.gr\" target=\"_blank\">DigiDiet</a> ήρθε για να σας βοηθήσει στην προσπάθειά σας για μια ισορροπημένη διατροφή που θα οδηγήσει στην απώλεια βάρους, με την καθοδήγηση εξειδικευμένων επιστημονικών συνεργατών.</p><p>Πρόκειται για μια πλατφόρμα που αναπτύχθηκε και εξελίσσεται συνεχώς από τις <a href=\"http://www.acsoft.gr\">ACSoft</a>, <a href=\"http://www.cwi.gr\" target=\"_blank\">CWI</a> και <a href=\"http://www.digiapps.gr\" target=\"_blank\">DigiApps</a>, εταιρείες που δραστηριοποιούνται στο χώρο της τεχνολογίας και της ανάπτυξης λογισμικού.</p><p>Απευθύνεται σε όσους:</p><p><ul><li>Βρίσκονται σε διαδικασία απώλειας βάρους</li><li>Δυσκολεύονται να τηρήσουν ένα σωστό διαιτολόγιο</li><li>Έχουν μεταβλητά ωράρια εργασίας και απαιτητικές επαγγελματικές υποχρεώσεις που επηρεάζουν τη σωστή διατροφή</li><li>Ξεχνούν να καταναλώσουν το σωστό γεύμα την κατάλληλη ώρα</li><li>Αθλούνται συστηματικά και χρειάζονται συνεχή παρακολούθηση και καθοδήγηση στα πλαίσια της καθημερινής διατροφής τους</li><li>Αναρρώνουν από κάποιο πρόβλημα υγείας και πρέπει να τηρήσουν συγκεκριμένο διαιτολόγιο, με την καθοδήγηση κάποιου εξειδικευμένου ιατρού</li></ul></p><p>Η εφαρμογή δημιουργήθηκε από την <a href=\"http://www.acsoft.gr\">ACSoft</a> και μπορείτε να την κατεβάσετε <strong>ΔΩΡΕΑΝ</strong> στο κινητό σας από το Apple AppStore.</p><p>Σύντομα θα είναι διαθέσιμη και η αντίστοιχη έκδοση για το Android στο Google PlayStore.</p><p><a href=\"https://itunes.apple.com/gr/app/digidiet/id761559243?mt=8&uo=4\" style=\"margin-right:10px;\" target=\"_blank\"><img alt=\"\" src=\"http://toolbox.acsoft.gr/acblog/images/AppStore.png\" /></a></p>",
"social_link": "http://www.acsoft.gr/index.php/blog/apps/13-digidiet-app",
"images": [
    "http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_photo1.png",
    "http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_photo2.png",
    "http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_photo3.png",
    "http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_photo4.png",
    "http://toolbox.acsoft.gr/acblog/images/digidiet/digidiet_photo5.png"
]}

Any ideas on how I could achieve that ? Thank you in advance!

5
  • You cannot select social_link from db Commented Jul 20, 2015 at 7:21
  • can you show schema of vrfmp_jbusinessdirectory_companies Commented Jul 20, 2015 at 7:22
  • And this is wrong, what's that url? "SELECT description AS content, 'http://offercat.com/media/com_jbusinessdirectory/pictures'+logoLocation AS images Commented Jul 20, 2015 at 7:22
  • 1
    Your queries are open for sql injections, I would recommend to not use the deprecated mysql_* API but rather mysqli or PDO and to use prepared statements. Commented Jul 20, 2015 at 7:22
  • It is my understanding that is has a ton of issues I should fix. Thanks for your pieces of advice. It would be great if I had some advice on the format. Commented Jul 20, 2015 at 7:28

1 Answer 1

3
$rows = array();
while($r = mysql_fetch_assoc($result)) {

$images[]=$r['images'];


}
$social="some content here";

$content="some content here ";

$rows=array("content"=>$content,"social_link"=>$social,"images"=>$images);

echo json_encode($rows);
Sign up to request clarification or add additional context in comments.

1 Comment

Saved my day. Thank you very much !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.