0

I am trying to get list of image path from my db and with help of Jquery and Json triying to add to my site. But I dont know why after encoding my string usingjson_encode` in php it changes it path and shows me like

[{"0":"user\/photogallery\/images\/members\/2\/2_1.jpg","src":"user\/photogallery\/images\/members\/2\/2_1.jpg"},{"0":"user\/photogallery\/images\/members\/2\/2_2.jpg","src":"user\/photogallery\/images\/members\/2\/2_2.jpg"}]

I need only user/photogallery/images/members/2/2_2.jpg part to create new <img src ="user/photogallery/images/members/2/2_2.jpg " /> component.

Here my php code and script

$member_id  = $GET['member_id'];
$files      = find_all_photos($member_id);

$encoded    = json_encode($files);
echo $encoded;
unset($encoded);

 function find_all_photos($id)
    {
      db_connect();

    $query = sprintf("SELECT src FROM photo_album_list WHERE user_id = '%s'", 
                           mysql_real_escape_string($id)); 

        $result = mysql_query($query);

        $result = db_result_to_array($result);

        return $result;
    }


    function db_result_to_array($result)
    {
        $res_array = array();

    for ($count=0;  $row = mysql_fetch_array($result); $count++)
    {
      $res_array[$count] = $row;
    }

        return $res_array;

    }

And script

$.get('photostack.php', {member_id:2} , function(data) {
                        console.log(data);
                        var items_count = data.length;
                        for(var i = 0; i < items_count; ++i){
                            var item_source = data[i];
                            var cnt         = 0;
                            $('<img />').load(function(){
                                var $image = $(this);
                                ++cnt;
                                resizeCenterImage($image);
                                $ps_container.append($image);
                                var r       = Math.floor(Math.random()*41)-20;
                                if(cnt < items_count){
                                    $image.css({
                                        '-moz-transform'    :'rotate('+r+'deg)',
                                        '-webkit-transform' :'rotate('+r+'deg)',
                                        'transform'         :'rotate('+r+'deg)'
                                    });
                                }
                                if(cnt == items_count){
                                    $loading.remove();
                                    $ps_container.show();
                                    $ps_close.show();
                                    $ps_overlay.show();
                                }
                            }).attr('src',item_source);
                        }
                    },'json');
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Ercan
  • 2,699
  • 10
  • 53
  • 60
  • Much more a PHP question than a jQuery question. I've added the PHP tag, and you might consider removing the jQuery one. – T.J. Crowder Feb 27 '11 at 10:28
  • well are you getting it form the server side? check out the response on your console – S L Feb 27 '11 at 10:28
  • possible duplicate of [JSON: why are forward slashes escaped?](http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped) – T.J. Crowder Feb 27 '11 at 10:31
  • yes it is duplicate question but I had also one question too.In my path it also creates something like "0":"user\/photogallery\/images\/members\/2\/2_1.jpg","src":"user\/photogallery\/images\/members\/2\/2_1.jpg" why not only src ? what is 0 ? it also duplicates. – Ercan Feb 27 '11 at 10:41
  • The duplicates are likely because the result of `mysql_fetch_array()` is defined as MYSQL_BOTH (default). From http://php.net/manual/en/function.mysql-fetch-array.php - "By using MYSQL_BOTH (default), you'll get an array with both associative and number indices." – ggutenberg Feb 27 '11 at 13:10

2 Answers2

0

It sounds like you're bothered that backslashes are being added prior to the slashes in the path. Completely agree that that's very odd (there's no need whatsoever to "escape" a slash in JSON strings), but in most notations it's harmless to escape a character that doesn't require escaping if that character doesn't have a special escaped meaning. (That is, it's harmless to escape / even though it doesn't need it, but obviously not harmless to escape n even though it doesn't mean it, since \n means something).

To my eyes, the JSON page is silent on what to do with invalid escapes, but Crockford's own parser allows them (disregards the unnecessary backslash), so... (Crockford being the inventor of JSON.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

It's possible that the escaped backslashes are stored in the DB that way, so your JSON string is just pulling them verbatim. If you want to remove them you'll need to process the string returned from the DB before you return or json_encode() them.

ggutenberg
  • 6,880
  • 8
  • 39
  • 48