Janaka Posted January 31, 2014 Share Posted January 31, 2014 (edited) hi, the question i have is , i am extracting data from a database and displaying it on a webpage created via php. The database has a colum which stores URLs. The problem i have is when i extract URL from the database and display it on the webpage i want it to be clickable. <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("",$con); $stateinput = $_POST['state']; $careerinput = $_POST['career']; $graduatelevelinput = $_POST['graduatelevel']; $stateinput = mysql_real_escape_string($stateinput); $careerinput = mysql_real_escape_string($careerinput); $graduatelevelinput = mysql_real_escape_string($graduatelevelinput); $sql = "SELECT * FROM College WHERE State = '$stateinput' AND career_Name= '$careerinput' AND Program = '$graduatelevelinput ' "; $result = mysql_query($sql)or die(mysql_error()); echo "<table>"; echo "<tr><th>University</th><th>Program</th><th>Website</th><th>State</th><th>City</th><th>Career</th><th>Graduate Level</th></tr>"; while($row = mysql_fetch_array($result)){ $University = $row['University']; $NameOftheProgram = $row['Name_of_Program']; $Website = $row['Website']; $state = $row['State']; $city = $row['City']; $career = $row['Career_Name']; $graduatelevel = $row['Program']; echo "<tr><td>".$University."</td><td>".$NameOftheProgram."</td><td>".$Website."</td><td style='width: 100px;'>".$state."</td><td style='width: 300px;'>".$city."</td><td>".$career."</td><td>".$graduatelevel."</td></tr>"; } echo "</table>" ?> i have bolded the part in the code which pulls the URL from the database. Please let me know how to make the URL clickable and also how to validate so that a message pops up when no results are found. I am a beginner and i did this just by referring but i could not find further articles to finish this up. Appreciate if sum one cud help me and Thank you very much. Edited January 31, 2014 by Janaka Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 31, 2014 Share Posted January 31, 2014 (edited) You need wrap it within an anchor tag. Also note variables are expanded within double quotes so you dont need to concatenate them echo "<tr> <td>$University</td> <td>$NameOftheProgram</td> <td><a href=\"$Website\">$Website</a></td> <td style='width: 100px;'>$state</td> <td style='width: 300px;'>$city</td> <td>$career</td> <td>$graduatelevel</td> </tr>"; also how to validate so that a message pops up when no results are found Use mysql_num_rows to check how many results where returned if(mysql_num_rows($result) > 0) { // code for displaying results } else { // no results display message here echo 'No results to display'; } Edited January 31, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Janaka Posted January 31, 2014 Author Share Posted January 31, 2014 hi Ch0cu3r I really appreciate you help. The URL works perfectly fine . But for the validation i have a small problem, the 'No results to display' message shows up when you access the URL for the first time before you even search for a record and also it does not pop up as a message box. I tried to play with it but couldnt get it . Any ideas Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 1, 2014 Share Posted February 1, 2014 But for the validation i have a small problem, the 'No results to display' message shows up when you access the URL for the first time You only want to perform a search on a the database when a POST request is made. if($_SERVER['REQUEST_METHOD'] == 'POST') { // get user input // perform database search // check for results if(mysql_num_rows($result) > 0) { // display results } else { // no results display message here } } // display search form does not pop up as a message box. You'll need to output the relevant html for this. Example code for a simple JavaScript alert window echo "<Script>alert('No results to display');</script>"; 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.