0

i have a task which is very complicated for me and also explain to you.. I will give my summary on what i m supposed to do.. there are 3 files which are the 1.html file which perform ajax post to the passwrapper.php 2. passwrapper.php will receive the ajax post request and include another file which is 3.student.php which contain codes on how to perform connection to the database and convert all data to json and then show all the data.. I was asked to perform ajax post multiple items.. In other words, i was asked to receive name and religion from the last row of database.. here is my code below...

html file

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        dataType: "json",
        data: {
            lastName: true,
            lastReligion: true,
        },      
        success: function(data){
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
        },

    });
};
</script>
</body>
</html>

passwrapper.php

<?php
include 'student.php';
if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"]))){
    executePass();
} 
else 
{
    //how to get name and religion from the last row and then perform executepass() to show all data and also data from the last row
}
?>

student.php

<?php
function executePass()
{

    $conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
    $db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

    $result = mysqli_query($conn,"select * from student");
    $json_array = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $json_array[] = $row;
    }

    echo json_encode($json_array);
}

my question is how to get last data from the last row which are student_name and student_religion in the passwrapper.php and also perform executepass() to show all data... please do not modify the sql code. i also do not want get data from the success. for example: data[count(data)-1]['student_name']... my question is how to get the last values which are student_name and student_religion in the passwrapper.php... and also perform the executepass() function to show all data.. please help me... you can write the last data to the files or show in the html console..

Andy
  • 49,085
  • 60
  • 166
  • 233
  • You don't have `ORDER BY` in your SQL, so the order of the rows is unpredictable. What do you mean by the last row? – Barmar Aug 24 '17 at 01:30
  • What is `abdpass.php`? Should that be `student.php`? – Barmar Aug 24 '17 at 01:32
  • I know there is no order by but i want to show all data by default from 1 to the end. and also i perform ajax post request to get data from the last row of the database which are student_name and student_religion. for example, there are 10 rows of data. i want to get the values from the last row.. –  Aug 24 '17 at 01:33
  • @Barmar i have corrected my mistake.. –  Aug 24 '17 at 01:34
  • Why don't you get the last row in the Javascript? `data[data.length-1].student_name` – Barmar Aug 24 '17 at 01:35
  • @Barmar i know that we can perform that code to get the last values.. but i want to use passwrapper.php to get the last values and perform logging to a file... this is for my work.. my boss want passwrapper.php method.. is it possible –  Aug 24 '17 at 01:37
  • Can you change `executePass()` so it returns the array instead of echoing it? It's best to separate input/output from data processing, so you don't have problems like this. – Barmar Aug 24 '17 at 01:38
  • if i change the executepass() will it be possible to show 'all data' and 'last values' separately?? –  Aug 24 '17 at 01:39

1 Answers1

0

Best would be to change executePass() so it ends with return $json_array; instead of echoing it. Then passwrapper.php could do:

$students = executePass();
if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"]))){
    echo json_encode($students);
} 
else {
    file_put_contents("all_students.json", json_encode($students);
    $last_student = end($students);
    echo json_encode(array($last_student));
}

And other scripts that want to return all the students can do:

$students = executePass();
echo json_encode($students);

If you can't do that, use the output buffering functions to capture the output from executePass() instead of sending it to the client.

else {
    ob_start();
    executePass();
    $json = ob_end_clean();
    file_put_contents("all_students.json", $json);
    $students = json_decode($json, true);
    $last_student = end($users);
    echo json_encode(array($last_student));
} 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • you are right that ajax cant echo 2 json.. I have another question which is about getting the ajax post data and showing the data in the console. However that posted data cannot be seen.... the link is down below... https://stackoverflow.com/questions/45853676/ajax-post-values-is-empty can you please help me in that question?? thank you –  Aug 24 '17 at 08:58