dmkc Posted April 29, 2010 Share Posted April 29, 2010 Morning guys, I'm hoping someone can help me. I'm pretty much self taught using php and mysql, and I've managed to put together a backend where records are entered onto a database, and I've managed to get the records displaying (paginated) on the front end. What I want to do now is make it so if there is a link entered in a field in the database called "url" that the record becomes a clickable link, leading through to a page called show_hotel.php. I'd also like to make it so that if the field in the db is empty, there is no link. I know that the link should be something like show_hotel.php?id=*id field here* but I really just don't know where to start. Could someone help me out by explaining how I'd do this? I've posted my code below, and there's a live example at: http://www.hotelsukonline.com/test/liverpool-hotels/hotels.php Thanks in advance for your help guys <?php $conn = mysql_connect(HOST,USER,PASS); $db = mysql_select_db("********") or die( "Unable to select database"); $sql = "SELECT COUNT(*) FROM "*******" where area='Liverpool'"; $result = mysql_query($sql, $conn) or die ("unable to select db"); $r = mysql_fetch_row($result); $numrows = $r[0]; $rowsperpage = 15; $totalpages = ceil($numrows / $rowsperpage); if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; } else { $currentpage = 1; } if ($currentpage > $totalpages) { $currentpage = $totalpages; } if ($currentpage < 1) { $currentpage = 1; } $offset = ($currentpage - 1) * $rowsperpage; $sql = "select * from ".IWARE_DOCS2." WHERE area='Liverpool' order by link_text LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or die ("unable to select db2"); $rest_name=mysql_result($result,$i,"link_text"); $address=mysql_result($result,$i,"address"); $telephone=mysql_result($result,$i,"telephone"); while ($list = mysql_fetch_assoc($result)) { echo "<strong>" . $list['link_text'] . "</strong>" . " <br />" . $list['address'] . " | " . $list['telephone'] ."<br /><br />"; } $range = 5; if ($currentpage > 1) { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; $prevpage = $currentpage - 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { if (($x > 0) && ($x <= $totalpages)) { if ($x == $currentpage) { echo " [<b>$x</b>] "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } } } if ($currentpage != $totalpages) { $nextpage = $currentpage + 1; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } ?> Quote Link to comment Share on other sites More sharing options...
callesson Posted April 29, 2010 Share Posted April 29, 2010 Wouldn't this work ? echo "<a href='show_hotel.php?id=$list[link_text] '><strong>" . $list['link_text'] . "</strong></a>" . " <br />" . $list['address'] . " | " . $list['telephone'] ."<br /><br />"; } Quote Link to comment Share on other sites More sharing options...
dmkc Posted April 29, 2010 Author Share Posted April 29, 2010 I was just about to try something similar, but I was sure it couldn't be that easy haha!! Thank you very much for your help mate, it's much appreciated, hopefully once I get a bit more up to speed I can answer a few myself... (yes, this does mean I have more questions coming up hehe) Quote Link to comment Share on other sites More sharing options...
callesson Posted April 29, 2010 Share Posted April 29, 2010 Np Haha, we will be waiting!xD Quote Link to comment Share on other sites More sharing options...
dmkc Posted April 29, 2010 Author Share Posted April 29, 2010 Hehe, ok.... I tweaked it slightly to show by id, as below: $location=mysql_result (£result,$i,"id"); while ($list = mysql_fetch_assoc($result)) {''; echo "<a href='show_hotel.php?id=$list[id] '><strong>" . $list['link_text'] . "</strong></a>" . " <br />" . $list['address'] . " " . $list['telephone'] ."<br /><br />"; } which works great. It goes through to the show_hotel.php page as I wanted, and is picking up the right record by id. What I'm wondering now is how to get it to display the information from that specific record on the show_hotel.php page. I'm trying to do this myself, using the following code: <?php $conn = mysql_connect(HOST,USER,PASS); $db = mysql_select_db("*******") or die( "Unable to select database"); $id = $_GET['id']; $query = "SELECT * FROM table WHERE id = '$id'"; { echo "<strong>" . $list['link_text'] . "</strong>" . " <br />" . $list['address'] . " " . $list['telephone'] ."<br /><br />"; ?> As always, I'd appreciate the pointers. I'm basically only trying to display the name (link_text), address (address) and telephone number (telephone). I'd also like to pull the hotels own website from our records and display it in an iframe, but I figure that I can call the variable and then use the echo command with something like this? $url=mysql_result("url"); echo "<iframe src="$url" style="background: #fff;" frameborder="0" height="450" scrolling="auto" width="100%"></iframe>"; Thanks again guys... Quote Link to comment Share on other sites More sharing options...
dmkc Posted April 29, 2010 Author Share Posted April 29, 2010 anyone? Quote Link to comment Share on other sites More sharing options...
callesson Posted April 30, 2010 Share Posted April 30, 2010 Well... If you want to display the info so. <?php $conn = mysql_connect(HOST,USER,PASS); $db = mysql_select_db("*******") or die( "Unable to select database"); if(isset($_GET["id"])) { $gethotel= mysql_query ("SELECT * FROM hoteltable WHERE id='$_GET[id]'") or die(mysql_error()); $count = mysql_num_rows($gethotel); if($count == 1) { $row = mysql_fetch_assoc($gethotel); { $hotelname= $row['link_text']; $address = $row['address ']; $telephone= $row['telephone']; echo "<strong> $hotelname </strong><br /> $address : $telephone <br /><br />"; } } elseif($count==0) { echo "There is no hotel with that name found!"; } } else { echo "There is no hotel found!"; } ?> Quote Link to comment Share on other sites More sharing options...
dmkc Posted May 4, 2010 Author Share Posted May 4, 2010 sorry about the late reply, but thankyou for the help, this has worked superbly! Quote Link to comment Share on other sites More sharing options...
callesson Posted May 5, 2010 Share Posted May 5, 2010 Np, I havent been online so much lately either Glad I could help, good luck in the future! Quote Link to comment 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.