Jump to content

Recommended Posts

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 by Janaka
Link to comment
https://forums.phpfreaks.com/topic/285838-clickable-url/
Share on other sites

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 by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/285838-clickable-url/#findComment-1467271
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/285838-clickable-url/#findComment-1467279
Share on other sites

 

 

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>";
Link to comment
https://forums.phpfreaks.com/topic/285838-clickable-url/#findComment-1467334
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.