-1

My problem is that this is only returning one image and there are 10.

This is my jquery code:

function mainImages() {
    $.ajax({
        url: '../includes/action.php',
        method: 'POST',
        data: {mainImages:1},
        success: function(data) {
            $('#main').html(data.mainImagesdiv);
        }
    });
}
mainImages();

and following is my php code, which is returning json data.

if(isset($_POST['mainImages'])) {
    $query = "SELECT * FROM images ORDER BY date_time DESC";
    $query_run = mysqli_query($dbc, $query);
    if(mysqli_num_rows($query_run) > 0) {
        while($rows = mysqli_fetch_array($query_run)) {
            $img_id = $rows['img_id'];
            $img_category = $rows['img_category'];
            $img_title = $rows['img_title'];
            $title_color = $rows['title_color'];
            $image = $rows['image'];
            $img_content = $rows['img_content'];
            $date_time = $rows['date_time'];
            $data = [ "mainImagesdiv" => "
            <div class='col s12 m6 l6'>
                <a href='#!'><div class='card grey'>
                    <div class='card-image'>
                        <img src='../images/".$image."' alt='Blog Picture'>
                        <span class='card-title'>
                            <h5 style='color: ".$title_color.";'>".truncate($img_title, 60)."</h5>
                            <span class='new badge' data-badge-caption='".strtoupper($img_category)."'></span>
                        </span>
                    </div>
                </div></a>
            </div>
            "];
        }
    }
}
header('Content-Type: application/json');
echo json_encode($data);
closeconnection();
2
  • On every iteration you overwrite the $data variable you return from PHP. You need to add data to it instead of overwriting it Commented Feb 25, 2018 at 20:40
  • How do I add instead of overwriting? Commented Feb 25, 2018 at 20:59

1 Answer 1

1

You are overriding your variable $data on each loop, so you get only the last one.

Change this line :

$data = [ "mainImagesdiv" => "<div class='col s12 m6 l6'>...</div>"] ;

By this line (append in "mainImagesdiv" string) :

$data["mainImagesdiv"] .= "<div class='col s12 m6 l6'>...</div>" ;

And before while($rows, add (to create the new string) :

$data["mainImagesDiv"] = "" ;

Finally, at the beginning add (to avoid "undefined variable" notice) :

$data=[];

Example :

$data=[] ; // data for JSON
if(isset($_POST['mainImages'])) {
    $query = "SELECT * FROM images ORDER BY date_time DESC";
    $query_run = mysqli_query($dbc, $query);
    if(mysqli_num_rows($query_run) > 0) {
        $data["mainImagesdiv"]=""; // new String in 
        while($rows = mysqli_fetch_array($query_run)) {
            $img_id = $rows['img_id'];
            $img_category = $rows['img_category'];
            $img_title = $rows['img_title'];
            $title_color = $rows['title_color'];
            $image = $rows['image'];
            $img_content = $rows['img_content'];
            $date_time = $rows['date_time'];
            // concatenate the string
            $data["mainImagesdiv"] .= "
            <div class='col s12 m6 l6'>
                <a href='#!'><div class='card grey'>
                    <div class='card-image'>
                        <img src='../images/".$image."' alt='Blog Picture'>
                        <span class='card-title'>
                            <h5 style='color: ".$title_color.";'>".truncate($img_title, 60)."</h5>
                            <span class='new badge' data-badge-caption='".strtoupper($img_category)."'></span>
                        </span>
                    </div>
                </div></a>
            </div>
            ";
        }
    }
}
header('Content-Type: application/json');
echo json_encode($data);
closeconnection();
Sign up to request clarification or add additional context in comments.

4 Comments

This did not output anything. but when i checked the console it made the request and the return was [] [].
@Fortie I've updated the answer several time. Please check the last one.
so hey. what if i want multiple divs.Like there we have moreImagesdiv.and you want to add another type of data. Say moreimg.
@Fortie You have to create more key in array like this :$data["moreImagesdiv"].="<div></div>"; (don't forget to create it before the while).

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.