Common PHP Tasks and Questions
Web Pages With Variable Content
How do I include variable content in a PHP page?
You may have seen on quite a few sites when they link to a file they just link to "www.example-domain.com/?page=pic.html&pic=jane-austin.jpg" or such. The idea is to link to a PHP page that includes the selected picture inside a web page without having to make a separate html file for every single picture, for example.
Unless you need to apply a universal template to the pages, you can simply make this a PHP page that accepts and displays variable content. PHP is a preprocessor, which means it automatically reads the page and executes code it finds. Any variables from a link are automatically brought into existence.
For this example, If you do not need templates to be varied by the image, just create a PHP page with whatever HTML you want on it, then include:
<img src=<?php print $pic; ?> alt="">
Fortunately, PHP does have an easy way of getting the height and width of an image so they can be added to the IMG tag. (However, an imaginative programmer might create snippets of JavaScript to peek at the height and width and document.write() them out to the IMG tag.)
<?php
$inf = getimagesize("pictures/".$pic);
print '<img src="pictures/'.$pic.'" '.$inf[3].' alt="">';
?>
If the page is index.php, you can call it with only the domain name
http://www.domain.com/?pic=pic-name.jpg
or by specifying a directory:
http://www.domain.com/pictures/?pic=pic-name.jpg
Pages: 1 2 3 4 | Next: Basics of Accessing the CGI Enviroment » |