I have an issue on how to create individual php pages automatically.
I have already created a page called catalog.php.
In catalog.php, mySQL query would take place, querying:
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT id, title, caption, comments, imagelink, year FROM photo";
Then this query would loop and display the contents on catalog.php side by side:
<ul class="grid">
<?php while ($row = mysql_fetch_assoc($result))
{ ?>
<li>
<p class="image"><a href="?"><img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" /></a></p>
<p class="name"><?php echo $row['title']; ?></p>
<p class="year"><?php echo $row['year']; ?></p>
</li>
<?php } // while
?>
</ul>
where class=”grid” would arrange all the queried data side by side, with image, title and year being displayed.
However, one requirement that i need is that whenever i click on any of these images, it should link to its own php page(individual.php) to show a detailed image, title, caption and author’s comments. An elaboration is shown below:
Title
IMAGE Caption
Author's comments
An example:
Picture of bridge
IMAGE This image was taken in paris
Low Shuttle Speed
In the above, IMAGE, caption, title, author’s comments are found in the same database “photodb” in the same table “photo”
My issue here is as follows:
-
Is there a way to create such pages automatically? The reason being is, if I create them manually I would have a hard time because my database has more that 100 entries.
-
In the
<a href="">tag as seen in catalog.php, what should the values be? -
I already have a template for such a “individual.php” page. An example of he structure is follows:
<body><p class="title">London Bridge</p><p class="caption">Image taken in summer 2009<br /><br /></p><p class="image"><img border="0" class="floatleft" src="imagelink" width="250" height="400" />Low Shuttle Speed</p></body>
How can i change this structure to suit my requirements?
Four. Can such “individual.php” url be renamed to something else with a unique id? Each image found in the database has their own unique ID.
Five. I have already queried the database in catalog.php. Can i somehow reuse this query individual.php?
In case you are wondering, getimage.php is as follows:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT imagelink FROM photo WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['imagelink']);
?>
Many thanks for reading my questions.
Your last code snippet essentially answered your question – you’ll create another page and pass a variable to it through the URL (
$_GET). So your URL may look like “individual.php?id=25” which would pull the image with the ID of ’25’.$_GET['id']would be ’25’ in this example.I also noticed you are using the “root” user for the MySQL connection. I would strongly advise against that.
http://www.1stwebdesigner.com/tutorials/getting-started-php-dynamic-content/
http://www.greensql.com/articles/mysql-security-best-practices