Intro
Alright, we are sending data to the database but now we are going to have to take that data and do something with that. Oh my gosh did that take forever to learn how to do it. The big thing that I have to do is get the data to change it into JSON and then send it to the javascript code. This is how I did it.
PHP code first
First I have to connect to the database again create a connection and kill the connection if I can’t get in. then from there I write my sql code to grab results. This is were I started to beat my head against the wall. This could have been prevented if I understood PHP and MySQLi better. Here is my solution.
PHP Code
$results = $conn->query($sql);
$counter = 0;
if ($results->num_rows > 0) {
// output data of each row
echo '{';
while($row = $results->fetch_assoc()) {
if($counter == $results->num_rows-1){
echo ''' .$counter .'':{'id':''. $row['id'] . '','Exercise':'' . $row['Exercise'] .'','Rep':'' . $row['Rep'] .'','Weight':'' . $row['Weight'] .'','Date':'' . $row['W_date'] .''}';
}else{
echo ''' .$counter .'':{'id':''. $row['id'] . '','Exercise':'' . $row['Exercise'] .'','Rep':'' . $row['Rep'] .'','Weight':'' . $row['Weight'] .'','Date':'' . $row['W_date'] .''},';
$counter++;
}
}
echo '}';
} else {
echo '0 results';
}
$conn->close();
?>
Now I query the system and save all the data to results. Here is the killer the data comes in the form of an object. You can’t echo an object from what I can tell I can only echo a string but don’t quote me because I have only been doing this for 33 days. Now there I looked at like ten tutorials. I just looked at all these ->s and had no idea what the heck there was. These are just the periods in javascript. All the methods and properties of the object get called this way. That helps a bunch to know this. Here was the next problem I had I wanted to send the string to the javascript file and convert it over to a JSON file. There is a PHP function called json_encode. Could not figure out how to do that whatsoever. That’s where all this crazy code comes from because JSON is so particular about how it wants to be formatted. I need to have curly brackets in the front and in the back that is why I need to echo them at the end. Then I need to make sure that the echo line has a prosperity name that’s where the counter comes in. the counter will be the property name of the top level of the objects. Also, I have to use the number of rows minus one so the last row of the code doesn’t have a comma in it at the end. Finally, after all of that, I can send it the javascript file and then run JSON.parse on it and it will become a JSON object. With this, I know how I’m going to send the data to the page so I can take a snippet and work on my page with it.