0

Here is my JavaScript Code:

                $.ajax({
                    url: './checkcolors.php',
                    type: 'post',
                    data: {
                        url: '<?php echo $LINK;?>',
                        SizeId: SelectedSizeID
                    },
                    dataType: 'json',
                    success: function (data) {
                        $('.ColorImagesNOColor').fadeIn();
                        $('#LoadingImage').hide();
                    $.each(data, function(index, item) {
                            $('#' + data.colorids).hide();
                            });
                    }
                });

Here is my checkcolors.php:

<?PHP


$url = 'http://www.sportsdirect.com/dunlop-mens-canvas-low-top-trainers-246046?colcode=24604622';
libxml_use_internal_errors(true); 
$doc = new DOMDocument();
$doc->loadHTMLFile($url);

$xpath = new DOMXpath($doc);

    $DataVariants = $xpath->query('//span[@class="ImgButWrap"]/@data-variants')->item(0)->nodeValue;

    $jsonStart = strpos($DataVariants, '[');
    $jsonEnd = strrpos($DataVariants, ']');

    $collections = json_decode(substr($DataVariants, $jsonStart, $jsonEnd - $jsonStart + 1));   



    foreach ($collections as $item) {
        $ColVarId = $item->ColVarId;
        $result = [];
        $SizeNames = [];
        foreach ($item->SizeVariants as $size) {
            $SizeNames[] = $size->SizeName;
        }

        if (in_array("7", $SizeNames)) {
            $result = array('colorids' => $ColVarId);


        }
    echo json_encode($result);  
    }



?>

The echo result from checkcolors.php is looking like this:

{"colorids":"24604603"}{"colorids":"24604684"}{"colorids":"24604640"}{"colorids":"24604609"}{"colorids":"24604682"}{"colorids":"24604686"}{"colorids":"24604681"}{"colorids":"24604689"}{"colorids":"24604602"}{"colorids":"24604679"}{"colorids":"24604680"}{"colorids":"24604622"}{"colorids":"24604685"}{"colorids":"24604683"}{"colorids":"24604621"}{"colorids":"24604677"}{"colorids":"24604688"}

I think i have problem with the json_encode function. I think the echo result is not correct. Can you help me out fix it ?

Thanks in advance!

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • You need to collect the `$result` rows into an indexed list/array of its own. Then only output the cumulative structure via json_encode. (Not each row by itself.) – mario Aug 23 '15 at 18:22
  • How i can achieve that ? Can you please give a working example from the code shown in my question ? Thanks in advance! – Venelin Aug 23 '15 at 18:23
  • Not a code fixing servant. Please look at the link, it's really similar enough. – mario Aug 23 '15 at 18:24
  • Where is that link ? – Venelin Aug 23 '15 at 18:24

0 Answers0