A dynamic web application that automatically loads and displays cat photos using PHP and AJAX
CatPics is a dynamic web application that creates an automated cat photo gallery using PHP and AJAX. The project demonstrates modern web development techniques by automatically scanning a directory of cat images and dynamically loading them into a responsive web interface without requiring manual HTML updates.
The application features a unique background image of cats, embedded YouTube video content, and a grid-based photo gallery that loads images dynamically through server-side PHP processing and client-side AJAX requests. This project showcases the integration of multiple web technologies to create an engaging, content-rich user experience.
glob()
function
The backend uses PHP's glob()
function to scan the media/images/
directory and return a JSON array of all image file paths. This approach eliminates the need for manual file listing and enables dynamic content generation.
$(window).load(function(){
$("#msg2").html(function(){
$.ajax({
type: "GET",
url: "./catpics.php",
dataType: "json",
success:function(result){
for (var i in result){
$("#msg2").append("
");
}
}
});
});
});
The frontend uses jQuery's AJAX functionality to fetch the image list from the PHP backend and dynamically append each image to the gallery. The $(window).load()
event ensures all page resources are loaded before executing the AJAX request.
img {
display: inline-block;
width: 250px;
height: 250px;
overflow: hidden;
}
body {
background-image: url("mycats.jpeg");
text-align: left;
font-size: 100%;
}
The CSS provides a consistent grid layout with fixed image dimensions (250x250px), custom background imagery, and responsive design elements. The cache-busting technique using PHP's date()
function ensures CSS updates are immediately visible.
catpics/
├── mycats.php # Main HTML interface
├── catpics.php # PHP backend for image listing
├── media/
│ ├── catstyle.css # Custom styling
│ ├── mycats.jpeg # Background image
│ └── images/ # Dynamic image directory
│ ├── 20180307_211324.jpg
│ ├── 20180307_212320_HDR.jpg
│ └── [77+ cat images...]
The CatPics application is designed for easy deployment and maintenance:
Simply place new cat images in the media/images/
directory, and they will automatically appear in the gallery on the next page load. No code changes or manual HTML updates are required.
The application can be easily customized by modifying the CSS file for different styling, changing the background image, or adjusting the image grid layout parameters.
CatPics demonstrates several important web development concepts:
This project serves as an excellent example of how simple web technologies can be combined to create engaging, dynamic user experiences. The modular architecture makes it easy to extend and modify, while the automated content loading reduces maintenance overhead.