Jump to content

How to make link from database result

Hikaru12

Here's my code for returning results from the database. I'd like the URL which is saved as a VARCHAR in the database to be displayed as a link. I can't seem to figure out how to embed the <a href="" element without creating a syntax error. Help is appreciated. Thanks!

 

$result = mysql_query("SELECT * FROM bookmarkstable WHERE (`BookmarkTitle` LIKE '%".$query."%') OR (`Category` LIKE '%".$query."%')ORDER BY BookmarkTitle ASC")or die(mysql_error());         if(mysql_num_rows($result) > 0)  {            while($results = mysql_fetch_array($result))   {                 echo "<tr align='center'><td>".$results['BookmarkTitle']."</td> <td>".$results['URL']."</td><td>".$results['Category']."</td> <td>".$results['Notes']."</td></tr>" ;            }

 

Link to comment
Share on other sites

Link to post
Share on other sites

well... knowing that a link is in the form

<a href = "URL"> text </a>

you just have to print that, using your URL instead of the placeholder

Link to comment
Share on other sites

Link to post
Share on other sites

well... knowing that a link is in the form

<a href = "URL"> text </a>

you just have to print that, using your URL instead of the placeholder

 

I tried using this in the echo to print it but it doesn't work:

<td>"<a href = ".$results['URL'].">Bookmark Link</a>"""</td>
Link to comment
Share on other sites

Link to post
Share on other sites

 

I tried using this in the echo to print it but it doesn't work:

<td>"<a href = ".$results['URL'].">Bookmark Link</a>"""</td>

 

What are all those double quotes doing in there? Hard to tell without seeing your complete code or at least a functional fragment.

Link to comment
Share on other sites

Link to post
Share on other sites

What are all those double quotes doing in there? Hard to tell without seeing your complete code or at least a functional fragment.

 

The syntax would break without wrapping it that many quotes. Here's what I have:

    $query = $_GET['query'];     $min_length = 3;  if(strlen($query) >= $min_length)  {         $query = htmlspecialchars($query);         $query = mysql_real_escape_string($query);  echo "<div class='container'>";  echo "<div class='table-responsive'>";  echo "<table class='table'>";  echo "<thead>";  echo "<br />";  echo "<br />";  echo "<br />";  echo "<tr><th>Bookmark Title</th><th>URL</th><th>Category</th><th>Notes</th></tr></thead>";  echo "<tbody>";             $result = mysql_query("SELECT * FROM bookmarkstable WHERE (`BookmarkTitle` LIKE '%".$query."%') OR (`Category` LIKE '%".$query."%')ORDER BY BookmarkTitle ASC")or die(mysql_error());         if(mysql_num_rows($result) > 0)  {            while($results = mysql_fetch_array($result))   {                 echo "<tr align='center'>				<td>".$results['BookmarkTitle']."</td> 				<td>".$results['URL']."</td>				<td>".$results['Category']."</td> 				<td>".$results['Notes']."</td>				</tr>" ;            }    echo "</tbody>"; 	

URL field is a VARCHAR(50) in the database. 

 

The HTML form looks like so, the URL field is entered in as plain text:

<form action="bookmarks.php" method="post">Bookmark Name: <input type="text" name="title"><br /><br/>Bookmark URL: <input type="text" name="url"><br /><br/>Category: <input type="text" name="category"><br /><br />Notes: <textarea name="notes" rows="3" cols="25"></textarea><br /><br/><input type="submit" name="submit" value="Save bookmark"></form>

As you can see, it's a very simple bookmarks script. I want the result to return as a link in that particular field. Makes it easier to click on then copying/pasting the returned field. Thanks for the help.

Link to comment
Share on other sites

Link to post
Share on other sites

snip

 

So what isn't working? Obviously I can't run this code without your database schema, so you'll have to work with me here.

Link to comment
Share on other sites

Link to post
Share on other sites

So what isn't working? Obviously I can't run this code without your database schema, so you'll have to work with me here.

 

The URL is returned simply as a table data element in plain text. If I try to encapsulate it like I did above in a a href element it either complains about the syntax because there's not enough quotes or when it is quoted correctly, it still continues to display the element as text rather than a link. 

 

I would think it would be simple enough to wrap the result in a <a href element> so that it could be clicked as a link but something with the syntax is iffy which is preventing it from displaying as such.

Link to comment
Share on other sites

Link to post
Share on other sites

The URL is returned simply as a table data element in plain text. If I try to encapsulate it like I did above in a a href element it either complains about the syntax because there's not enough quotes or when it is quoted correctly, it still continues to display the element as text rather than a link. 

 

I would think it would be simple enough to wrap the result in a <a href element> so that it could be clicked as a link but something with the syntax is iffy which is preventing it from displaying as such.

 

First thing I would do is stop trying to echo the content of the html page, which is leading to your quoting woes. Instead, include the html as a separate php file and echo the values of each variable where you want it in the layout like

<?php// Database query?><table><?php while ($row = mysql_fetch_assoc($result)): ?><tr>  <td><a href="<?php print $row['URL'] ?>"><?php print $row['BookmarkTitle'] ?></a></td></tr><?php endwhile ?></table>
Link to comment
Share on other sites

Link to post
Share on other sites

Just escape the double quotes inside the html elements:  

<?php$query = $_GET['query'];$min_length = 3;  if(strlen($query) >= $min_length)  {    $query = htmlspecialchars($query);    $query = mysql_real_escape_string($query);    echo "<div class='container'>";    echo "<div class='table-responsive'>";    echo "<table class='table'>";    echo "<thead>";    echo "<br />";    echo "<br />";    echo "<br />";    echo "<tr><th>Bookmark Title</th><th>URL</th><th>Category</th><th>Notes</th></tr></thead>";    echo "<tbody>";    echo "<tr align='center'>";    $result = mysql_query("SELECT * FROM bookmarkstable WHERE (`BookmarkTitle` LIKE '%".$query."%') OR (`Category` LIKE '%".$query."%')ORDER BY BookmarkTitle ASC")or die(mysql_error());    if(mysql_num_rows($result) > 0) {        while($results = mysql_fetch_array($result)) {                echo "<tr align='center'><td><a href=\"$results[url]\">$results[BookmarkTitle]</a></td>			    <td><a href=\"$results[url]\">$results[url]</a></td>				<td><a href=\"$results[url]\">$results[Category]</a></td>				<td><a href=\"$results[url]\">$results[Notes]</a></td>				</tr>" ;        }    }        echo "</tbody>";  }?>
Link to comment
Share on other sites

Link to post
Share on other sites

snip

 

Variable interpolation doesn't work in singly-quoted strings in PHP.

Link to comment
Share on other sites

Link to post
Share on other sites

Actually, I think that was the original problem the OP was having, trying to concatenate the variable inside a string with single quotes.

 

Do it like I suggested and all will be well.

Link to comment
Share on other sites

Link to post
Share on other sites

Actually, I think that was the original problem the OP was having, trying to concatenate the variable inside a string with single quotes.

 

Do it like I suggested and all will be well.

 

You are right i did not test my code. I have updated my original post with one that should work by simply ignoring the double quotes in the html elements.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×