nbasso713 Posted December 10, 2011 Share Posted December 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/ Share on other sites More sharing options...
xyph Posted December 11, 2011 Share Posted December 11, 2011 If using somesite.com/?img=1 $_GET['img'] will equal 1 Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1296666 Share on other sites More sharing options...
jj20051 Posted December 11, 2011 Share Posted December 11, 2011 Here is some example code: <?php $img = $_GET['img']; if($img != NULL){ // Mysql query } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1296667 Share on other sites More sharing options...
nbasso713 Posted December 11, 2011 Author Share Posted December 11, 2011 Thanks for getting back to me, I can't tell if i'm over tired or just retarded but i'm still not having any luck. Can someone please just hold my hand through this it's driving me crazy and it seems like it should be simple. here's an example of what i'm working with. <?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> ?> Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1296686 Share on other sites More sharing options...
jj20051 Posted December 11, 2011 Share Posted December 11, 2011 Could you comment your code any better? I had a hard time figuring out what your trying to get it to do... It looks like your trying to get it to pull a random image out of the database and display it along with a next button? If thats the case let me know. Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1296956 Share on other sites More sharing options...
dachshund Posted December 11, 2011 Share Posted December 11, 2011 <?php $image = $_GET['img']; $query = mysql_query("SELECT * FROM uploaded_images WHERE id LIKE $image"); while ($rows = mysql_fetch_array($query)) { ?> <img src="<?php echo $rows['url']; ?>" /> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1296959 Share on other sites More sharing options...
nbasso713 Posted December 12, 2011 Author Share Posted December 12, 2011 Thank you all so much, I really appreciate all the help! That was exactly what i was looking for! Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1297001 Share on other sites More sharing options...
nbasso713 Posted December 12, 2011 Author Share Posted December 12, 2011 <?php $image = $_GET['img']; $query = mysql_query("SELECT * FROM uploaded_images WHERE id LIKE $image"); while ($rows = mysql_fetch_array($query)) { ?> <img src="<?php echo $rows['url']; ?>" /> <?php } ?> Is there anyway to have the page load to a ?img=# by default? I'm receiving an error if it's just domain.com/test.php But if it's domain.com/test.php?img=1 for example, it works perfectly. I eventually plan on moving all this to my index.php, so if someone just enters the domain i need it to redirect to a ?img=# by default, or the page will not load properly. Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1297019 Share on other sites More sharing options...
Drummin Posted December 12, 2011 Share Posted December 12, 2011 You would just add a simple IF statement to set what you want. if (isset($_GET['img'])){ $image = $_GET['img']; } else{ $image =1; //Your default image id } Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1297024 Share on other sites More sharing options...
nbasso713 Posted December 12, 2011 Author Share Posted December 12, 2011 Awesome thank you so much. I placed a header to redirect w/ a random ?img=# in the else statement and it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/252915-need-help-setting-up-dynamic-urls/#findComment-1297029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.