Need Help Setting Up Dynamic URL's

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
nbasso713
New php-forum User
New php-forum User
Posts: 18
Joined: Sat Dec 10, 2011 4:00 pm

Sat Dec 10, 2011 4:02 pm

I've been working on this for the last few hours and haven't had much luck. Basically, every time you visit the index of my site a random image is shown. The images are stored in a directory, and i use a table to hold the id, name, and url of each image. When the index page is loaded a random int is chosen, and the id equal to the int prints the corresponding url for the image.

Now i'm trying to add the id into the url, "domain.com?img=1". I also need this url to always refer to the image in row 1.

My table is called uploaded_images and the columns are id(primary key), name, url.

I'm pretty lost, any help is greatly appreciated.
User avatar
gesf
Moderator
Moderator
Posts: 1716
Joined: Sun Dec 29, 2002 5:03 am
Location: Portugal / Sweden
Contact:

Sat Dec 10, 2011 9:56 pm

Hey nbasso713,

Not sure about what you're trying to accomplish, but if you have a table (MySQL ?) to hold this URL/ID relation, it should be quite simple:

Code: Select all

<?php

if(isset($_GET['img']) && !empty($_GET['img'])){
     $id = (int) $_GET['img'];
     $row = mysql_fetch_array(mysql_query("SELECT url FROM uploaded_images WHERE id = " . $id));
     print $row['url']; // the corresponding url for the given id.
}

?>
You can also do something like, e.g.:

Code: Select all

<?php

// this will automatically return a random image url from db

$row = mysql_fetch_array(mysql_query("SELECT url FROM uploaded_images ORDER BY RAND() LIMIT 1"));
print '<img src="' . $row['url'] . '" alt="" />'; // and show the image

?>
Hope it helps ;)
nbasso713
New php-forum User
New php-forum User
Posts: 18
Joined: Sat Dec 10, 2011 4:00 pm

Sat Dec 10, 2011 10:45 pm

Thanks for getting back to me. I'm able to display images from the database without a problem, but i'm trying to get the id in the url to correspond to the image being displayed. So if i went to "domain.com?img=1" the image with id 1 in the uploaded_image table would be displayed.

I hope this is a bit more clear, here's a sample copy of what i'm working with. I'm new to php so my coding may look a little weird.

Code: Select all

<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="uploaded_images"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$query = mysql_query(" SELECT max(id) AS id FROM uploaded_images ");
while ($row = mysql_fetch_assoc($query)) {
    $id = $row['id'];
}


$imgid = rand(18,$id);
$next = rand(18,$id);
	while($next == $imgid){
			$next = rand(18,$id);
	}



$ImageQuery = "SELECT name, url FROM uploaded_images WHERE id = '".$imgid."';";


$ImageResult = mysql_query($ImageQuery);

$ImageArray = mysql_fetch_array($ImageResult);
htmlspecialchars($_GET["name"]);
$ImageName = $ImageArray['name'];
      $url = $ImageArray['url'];
	  
<html>
<head></head>
<body>
<h5>Click or Browse For Another Random Image</h5>
<p id="img"><a id="img" href="http://domain.com/test.php?img=<?php echo $next;?>">
<img src="<?php echo $url;?>"/>
</a></p>
</body>
</html>

?>
Post Reply