0

Hi guys :) I'm quite new in the programming world, so I really need your help. I tryed to get data from some database tables and unfortunately something goes wrong in my $.getJson() function . If i run the php file it works , and so that with the functions from script.js . My html is also ok so i suppose it is the $get.JSON fault. I don't know so many javascript , so i put all my hopes into you :*

html file:

 <!doctype html>
 <html>
 <head>
 <meta charset="utf-8" />
 <title>jQuery Ajax - PHP</title>
 <script type="text/javascript" src="jquery.js"></script>
 </head>
 <body>
 <script src="script.js"> 
 </script>
 </body>
 </html>

script.js file :

$('document').ready( function() {
                                done();
                                }
                    );
function done() {
    setTimeout(function() { 
                         updates();
                         done();
                           }
              , 200 );
                 }
function updates(){ 
   $.getJSON( "read.php", function (data){

      $.each(data.array, function () {
          $("body").append("<li>Titlu: "+this['title']+"</li>
                            <li>Descriere: "+this['describtion']+"</li>");
                                     }
                           );

      $.each(data.array1, function () {
          $("body").append("<li>Id: "+this['id']+"</li>
                          <li>Category_Id: "+this['category_id']+"</li>
                          <li>Titlu: "+this['title']+"</li>
                          <li>Descriere: "+this['describtion']+"</li>");
                                        }
             );

    $.each(data.array2, function () {
        $("body").append("<li>Id: "+this['id']+"</li>
                          <li>Titlu: "+this['title']+"</li>
                          <li>Latitudine: "+this['location_latitude']+"</li>
                         <li>Longitudine:"+this['location_longitude']+"</li>
                         <li>Numar de telefon: "+this['phone_number']+"</li>
                          <li>Descriere: "+this['describtion']+"</li>");
                                     }
          );

    $.each(data.array3, function () {
        $("body").append("<li>Id: "+this['id']+"</li>
                    <li>Interest_point_id:"+this['interest_point_id']+"</li>
                    <li>Pret: "+this['price']+"</li>
                    <li>Data: "+this['event1_time']+"</li>");
                                      }
          );
                                          }
                );

              }

And finally the read.php file (Here it shows me what i expect , so i think everything is all right) :

<?php
include_once ('db.php');
$query= "SELECT * FROM category";
$query1= "SELECT * FROM sub_category";
$query2= "SELECT * FROM interest_point";
$query3= "SELECT * FROM event1";
global $connect;
$result =  mysqli_query($connect,$query);
$result1 = mysqli_query($connect,$query1);
$result2 = mysqli_query($connect,$query2);
$result3 = mysqli_query($connect,$query3);
$array = array();
$array1 = array();
$array2 = array();
$array3 = array();

while($row=mysqli_fetch_array($result))
    array_push($array , array( 'id'          => $row[0],
                               'title'       => $row[1],
                               'describtion' => $row[2]
));

while($row1=mysqli_fetch_array($result1))
    array_push($array1 , array( 'id'         => $row1[0],
                               'category_id' => $row1[1],
                               'title'       => $row1[2],
                               'describtion' => $row1[3]

));
while($row2=mysqli_fetch_array($result2))
    array_push($array2 , array('id'                => $row2[0],
                               'title'             => $row2[1],
                               'location_latitude' => $row2[2],
                               'location_longitude'=> $row2[3],
                               'phone_number'      => $row2[4],
                               'describtion'       => $row2[5]

));
while($row3=mysqli_fetch_array($result3))
    array_push($array3 , array( 
                               'id'               => $row3[0],
                               'interest_point_id'=> $row3[1],
                               'price'            => $row3[2],
                               'event1_time'      => $row3[3]
));

    echo json_encode(array("array"=>$array)).'<br>'.'<br>';
    echo json_encode(array("array1"=>$array1)).'<br>'.'<br>';
    echo json_encode(array("array2"=>$array2)).'<br>'.'<br>';
    echo json_encode(array("array3"=>$array3)).'<br>'.'<br>';


?>
2
  • "Something goes wrong" is not a good explanation. What goes wrong? Commented Jul 2, 2016 at 21:48
  • Please include error messages or any information from your logs about the $.getJSON call that's failing. Commented Jul 2, 2016 at 22:28

1 Answer 1

1

You can only echo once when sending json and there can only be one php array containing all the data. You can't print anything outside of this such as the <br> tags

try changing

echo json_encode(array("array"=>$array)).'<br>'.'<br>';
echo json_encode(array("array1"=>$array1)).'<br>'.'<br>';
echo json_encode(array("array2"=>$array2)).'<br>'.'<br>';
echo json_encode(array("array3"=>$array3)).'<br>'.'<br>';

To

$output = array(
   "array1"=>$array1,
   "array2"=>$array2,
   "array3"=>$array3
);

echo json_encode($output);

Also note that <li> is an invalid child of <body>. Use <div> or insert into a <ul>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much man :) It works now :D :D :D God bless you ! :*

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.