0

I have two tables A & B

TableA // (People)
UserID    Name
10         Dan
20         Jane
30         Shelley

TableB // (Pics)
PicID    UserID
100      10
200      10
300      20

I want to take everything from Table A and load it into an array and then add arrays for the images within the array but am unsure how to do this:

Currently for Table A I think:

$query = "select * from TableA";

$result = $mysqli->query($query) or die(mysqli_error($mysqli));

while ($row = $result->fetch_array()){
    $array[] = $row;
    // do another select here to get pics and add data to array multiDimensionally
}

echo json_encode($array);

Then I'm reading it like:

$.ajax({
    url: apiURL,
    dataType: 'json',
    success: onLoadData
});

function onLoadData(data) {

// Create HTML for the images.
var html = '';

var i = 0, length = data.length, image;

for (; i < length; i++) {

    image = data[i];

    html += '<div class="holder">';
    html += '<div class="name">' + image.Name + '</div>;

    // do another loop here to show images for each person

    html += '</div>';

} 

Is this the best way to do it and if so, how would I add to the array and then read from it?

1 Answer 1

1

If I am not wrong then this can be done in another way-

Mysql :

$query = "select * from TableA join TableB on TableA.UserID=TableB.UserId order by TableA.UserID asc";

And the Jquery part:

 $.ajax({
    url: apiURL,
    dataType: 'json',
    success: function(data)
    {
       $.each( data, function( key, value ) {
           //alert( key + ": " + value ); //do what ever you want
      });
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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.