I am currently working on this website, and i need some help regarding php and ajax.
I apologise if this is a simple fix, but i’ve looked everywhere and i can’t find the solution.
Anyway, the links on the left load content on the right with ajax. That’s the easy part.
The sublinks under ‘gallery’ load a php file like this:
<?php
include "connect_to_mysql.php";
include_once("classes/category.class.php");
include_once("classes/album.class.php");
include_once("classes/images.class.php");
$category = new category();
$categories = $category->loadAbstract();
?>
<div id="image_gallery">
<?php
foreach ($categories as $category) {
if ($category->hasAlbums()) {
foreach ($category->albums as $album) {
echo '<div class="album_container">';
echo '<div class="sublinks" onClick="loadAlbum(\''.$album->getId().'\')">
<h4>'.$album->getName().'</h4>
</div>';
}
}
}
?>
</div>
<script>
function loadAlbum(Id) {
$.ajax({
type: 'POST',
url: 'loadalbum.php',
data: "selectedAlbum="+Id,
success: function(result) {
$("#"+Id).html(result);
}
});
</script>
Obviously, this calls loadalbum.php, which i have to add the same includes:
<?php
include "connect_to_mysql.php";
include_once("classes/category.class.php");
include_once("classes/album.class.php");
include_once("classes/images.class.php");
$selectedAlbum=$_POST['selectedAlbum'];
?>
The problem is that every time i select a different category on the left, it has to call these classes, and then when an album is selected from that category, the classes are called again. This is slowing down the load time considerably as it has to keep retrieving data from the database.
I use a bit of javascript to stop it from loading an album if it has already been loaded, but as soon as a different link is selected on the left, it goes back to square one.
Is there a way to load all of my classes when the web page opens and then reference them when my php files are loaded through ajax?
Thanks in advance!
I think you need to redefine your entity relationships. Based on your code: category, album, and image all have equal standing. I don’t think that is true, but it’s not my design. At any rate, include your models into your controller class
So now within your controller, you have access to all your models. Put your data manipulation functions inside your model classes and call them from the controller. The included page will be preloaded with the $categories data. You can then loop through it in the view.
Two important notes: 1) Require files that MUST exist at runtime. You’ll get a fatal error if they don’t. An include will only give a warning, which may be suppressed, and 2) requires and includes are language constructs, not methods. Parentheses are unnecessary.
I’ve provided a basic outline for your controller class. My way is just one of hundreds of other MVC implementations. Many will argue the finer points of what I wrote. Don’t worry about it. Just try to understand the basics of what I provided. If it helps, great. Be sure to accept the answer. If not, then ignore it.
Good luck.