9

Database :

`--> product table
id  name      cid     uploadKey
1   Cemera     1       7365
2   Notebook   2       7222`

`--> category table
id    catename
1      canon
2      toshiba`


`--> attactments table
id      uploadKey       filepath
1       7365            /img/jdf.png
2       7365            /img/sdsd.jpg`

This code to create json file:

$_GET['id']="1";
 $json_response = array();
    if(isset($_GET['id']))
    {
        $id=$_GET['id'];        
 $select = mysql_query("SELECT product.name,category.catename,attactments.filepath FROM product INNER JOIN category ON category.id = product.cid INNER JOIN attactments ON attactments.uploadKey = product.uploadKey where product.id='".$id."'  ");
    while ($row = mysql_fetch_array($select , MYSQL_ASSOC)) {       
        $json_response[] = $row;
         } 
    }
 echo $val= str_replace('\\/', '/', json_encode($json_response));

The result repeat information, how to remove repeat i want to show as below :

[{"name":"Cemera","catename":"canon","filepath":"/img/jdf.png"},{"name":"Cemera","catename":"canon","filepath":"/img/sdsd.jpg"}]

I want to show like this, how we edit it:

[{"name":"Cemera","catename":"canon","filepath":"/img/jdf.png","filepath":"/img/sdsd.jpg"}]
Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32
DNu
  • 123
  • 10
  • this will help you http://stackoverflow.com/questions/23507853/remove-duplicate-objects-from-json-array http://stackoverflow.com/questions/18034696/rebuild-a-json-data-remove-duplicate-value-in-one-child-node – Nishit Maheta Jul 14 '15 at 04:13

3 Answers3

2

You can GROUP_CONCAT() filepath, try bellow code

$select = mysql_query("SELECT product.name,category.catename,GROUP_CONCAT(attactments.filepath SEPARATOR ',') AS filepath FROM product INNER JOIN category ON category.id = product.cid INNER JOIN attactments ON attactments.uploadKey = product.uploadKey where product.id='".$id."'  ");

while ($row = mysql_fetch_array($select , MYSQL_ASSOC)) {   
   $row['filepath'] = explode(',',$row['filepath']);
        $json_response[] = $row;
         } 

then you will get following result

{"name":"Cemera","catename":"canon","filepath":["\/img\/jdf.png","\/img\/sdsd.jpg"]}
KTAnj
  • 1,346
  • 14
  • 36
  • Thank you for your answer, can you give me like this [{"name":"Cemera","catename":"canon","filepath":"/img/jdf.png","filepath":"/img/sdsd.jpg"}] – DNu Jul 14 '15 at 04:58
  • How do you hope to give different values in same key ? does it possible ? – KTAnj Jul 14 '15 at 05:01
1

All Filepath should be comma separate using GROUP_CONCAT. Like this

$select = mysql_query("SELECT product.name,category.catename,attactments.filepath 
FROM product 
INNER JOIN category ON category.id = product.cid 
INNER JOIN(SELECT uploadKey, GROUP_CONCAT(filepath SEPARATOR ',') FROM attactments GROUP BY uploadKey) a ON a.uploadKey = product.uploadKey where product.id='".$id."'  ");
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
0
Put limit 0,1 at the end of query.
$select = mysql_query("SELECTproduct.name,category.catename,attactments.filepath FROM product INNER JOIN category ON category.id = product.cid INNER JOIN attactments ON attactments.uploadKey = product.uploadKey where product.id='".$id."' lomit 0,1 ");

I think it Helps..

Prabhagaran
  • 3,620
  • 1
  • 19
  • 19