Alright, here's what I want my website (http://gift-for-men.com/) to do. When someone visits the index page the index.php file is runned. What I want to index file to do is grab a random row from the database then redirect people to random page based on the id of the row grabbed.For example, I visit gift-for-men.com/ then I'm redirected to gift-for-men.com/page?=1 ( 1 is the id ) it could be the id 2, 3, 440, whatever row from the database was grabbed. I added the page?= to make it prettier if you can do this. Also in when the row is grabbed I have other columns that I want to echo throughout the entire document. So if it can be stored in a array I would appreciate it. Anyone who helps thanks. If you write the entire program we can discuss payment.
Place the following code right at top of (http://gift-for-men.com/) index.php: <?php //retrieve random id from table... - replace table_name with the name of your table... $result = mysql_query("SELECT id FROM table_name ORDER by rand() LIMIT 1"); $id = mysql_result($result, 0, 0); //perform redirect... header("Location: http://gift-for-men.com/page?={$id}"); ?> PHP:
I get the following error: Warning: Cannot modify header information - headers already sent by (output started at /home/content/00/6453700/html/index.php:4) in /home/content/00/6453700/html/index.php on line 10
Hmm thats a typical problem, usually occured due to output being sent before the header() redirect...theirfore this should resolve it (quick fix): Add to the very top of index.php, straight after the php tag (<?php): ob_start(); PHP: And at the end just before the closing php tag (?>): ob_end_flush(); PHP:
Sorry I should of done that in the first place... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php include 'include/simple.php'; $query = "SELECT * FROM suggestions ORDER BY Rand() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($result); ?> <head> <title>Gift For Men</title> <meta name="keywords" content="Gift For Men, Gift" /> <meta name="description" content="Don't know what to get a man? With Gifts-For-Men.Com we'll randomly make suggestions to you!" /> <link rel="stylesheet" type="text/css" href="assets/Stylesheet.css" /> </head> <!--HEADER--> <body> <div id="fb-root"></div> <div id="contain" class="header"> <div id="contain" class="title"> <h1><a href="http://gift-for-men.com/" title="Gift For Men">Gift For Men</a></h1> <?php echo "Get your man " , $row['link']; ?> </div> <div id="contain" class="logo"> <center><img src="http://i983.photobucket.com/albums/ae320/Hamiltoncameron33/logo.png" width="115" height="160" alt="Gift For Men Logo" /></center> </div> </div> <!--BODY --> <div id="funny"> <?php echo $row['funny']; ?> </div> <div id="body" class="contain"> <div id="body" class="amazon"> <?php echo $row['product']; ?> </div> </div> <!--FOOTER --> <div id="suggest"> <a href="http://gift-for-men.com/" title="Need another suggestion?">Need another suggestion?</a> </div> <div id="footer" class="contain"> <div id="footer" class="copyright"> <p><a href="http://gift-for-men.com/" title="Gift For Men">Gift For Men</a> - Copyright 2010 ©</p> </div> <div id="footer" class="facebook"> <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgift-for-men.com%2F&layout=standard&show_faces=false&width=320&action=like&colorscheme=dark&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:320px; height:35px;" allowTransparency="true"></iframe> </div> </div> </body> </html> PHP:
<?php include 'include/simple.php'; $query = "SELECT * FROM suggestions ORDER BY Rand() LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_array($result); //perform redirect... header("Location: http://gift-for-men.com/page?={$row['id']}"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Gift For Men</title> <meta name="keywords" content="Gift For Men, Gift" /> <meta name="description" content="Don't know what to get a man? With Gifts-For-Men.Com we'll randomly make suggestions to you!" /> <link rel="stylesheet" type="text/css" href="assets/Stylesheet.css" /> </head> <!--HEADER--> <body> <div id="fb-root"></div> <div id="contain" class="header"> <div id="contain" class="title"> <h1><a href="http://gift-for-men.com/" title="Gift For Men">Gift For Men</a></h1> <?php echo "Get your man " , $row['link']; ?> </div> <div id="contain" class="logo"> <center><img src="http://i983.photobucket.com/albums/ae320/Hamiltoncameron33/logo.png" width="115" height="160" alt="Gift For Men Logo" /></center> </div> </div> <!--BODY --> <div id="funny"> <?php echo $row['funny']; ?> </div> <div id="body" class="contain"> <div id="body" class="amazon"> <?php echo $row['product']; ?> </div> </div> <!--FOOTER --> <div id="suggest"> <a href="http://gift-for-men.com/" title="Need another suggestion?">Need another suggestion?</a> </div> <div id="footer" class="contain"> <div id="footer" class="copyright"> <p><a href="http://gift-for-men.com/" title="Gift For Men">Gift For Men</a> - Copyright 2010 ©</p> </div> <div id="footer" class="facebook"> <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fgift-for-men.com%2F&layout=standard&show_faces=false&width=320&action=like&colorscheme=dark&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:320px; height:35px;" allowTransparency="true"></iframe> </div> </div> </body> </html> PHP:
No error this time but it doesn't display the URL like we want... Sorry, if you're getting irritated man.