Jump to content

How Do I Create A Link From Part Of The Text In A Database?


Recommended Posts

Hello,

 

First, I'm sorry my title probably doesn't make sense, I couldn't think of a better way to put it.

 

Basically I came up with an idea in my head that I now want to make happen. Here it is.

 

Let's say I enter some data into a database table using a regular html form, for example like the text below:

 

Here is some text, for more information, Contact Us!

 

What I want to do is somehow (magic?) make it so when I retrieve this same text out of the database, it makes Contact Us into a link to the contact page.

 

Crazy, right?

 

I know, I just thought it would be really cool if when my customer adds another item to their websites FAQ page, that any time they say "Contact Us" it would display as a link to their contact page.

 

Thoughts?

 

~FOX~

I'd say the best bet would be to write a function to replace the string "Contact Us" with the link to that page. Regex should do that for you.

If you're using a CMS you could list out the pages and find the page name and replace that with the <a href="contactus.php">Contact Us</a> structure.

Try this:

 

<?php
$query = mysql_query("SELECT * FROM table_name");
if(mysql_num_rows($query) != 0){
$row = mysql_fethc_assoc($query);
$contactUs = $row["pageContent"];
$convertContact = str_replace("Contact Us", "<a href='contactus.php'>Contact Us</a>", $contactUs);
$convertContact = $contactUs;
echo $contactUs;
}else{
echo "Nothing in database";
}
?>

Edited by White_Lily

I'm afraid that won't work, White_Lily, as you're overwriting the corrected string with the uncorrected.

Use this instead:

$query = mysql_query("SELECT * FROM table_name");
if(mysql_num_rows($query) != 0){
   $row = mysql_fethc_assoc($query);
   $contactUs = str_replace("Contact Us", "<a href='contactus.php'>Contact Us</a>", $row["pageContent"]);
   echo $contactUs;
}else{
   echo "Nothing in database";
}

 

Don't really need the $contactUs variable either, as you could use the pageContent index directly. That is, unless you need the unmodified string later on.

Edited by Christian F.

Hey! Thank you all so much, I appreciate you taking the time to reply!

 

I re-read my question and I don't think I explained myself very well, I remember being sort of fragmented at the time I wrote it. I think the str_replace function might be the ticket (magic) I was looking for, but I want to try to re-write the question again so I can make sure I'm being clear with what I'm trying to do. So...

 

Let's say I've got a web page for Frequently Asked Questions that I want to allow my customer to add new FAQ's to. With that in mind, I've made a CMS form that inserts new data into the database. The FAQ page then queries that database and prints the information out accordingly.

 

So far so good, easy peasy.

 

So let's say my customer, through the CMS form, inserts this text into the database:

 

"Yes, we can do that, please Contact Us for a quote."

 

What I want to do is make it so when the FAQ page queries the database, and prints out the text above, it replaces the words "Contact Us" with <a href="/contact-us/">Contact Us</a>. So it creates a link to the contact page without my customer having to enter any html into the CMS form.

 

There, I think that sounds a lot better. However I do believe the str_replace function will be the answer I was looking for, so I'm going to go and give that a try.

 

Thanks again for the help :)

 

~FOX~

Oh, how very cool! The str_replace function did exactly what I wanted it to, thank you so much for helping me get pointed into the right direction.

 

Here is what I did:

 

$contact_link = "<a href=\"/contact-us/\" class=\"inline\" target=\"_parent\">Contact Us</a>";
$answer = str_replace(array("contact us","Contact Us","Contact us") , array($contact_link , $contact_link , $contact_link), $row['answer']);
echo "<td>" . $answer . "</td>";

 

...and it works just exactly as I had imagined it, I am so happy :)

 

I would be interested to know if there is anything I missed, or if there is a cleaner way to run this...maybe in a function? Hmmm...I just thought of the function idea as I typed this, maybe I'll go work on that one next.

 

Thanks again everyone, I appreciate the help!

 

~FOX~

 

(not going to mark solved until I make sure it's complete)

Edited by FoxRocks

Thanks White_Lily, It's nice to hear I didn't make a mess of it :)

 

Also, I did try to make it into a function, but the function would have been bigger than just writing the code, so I decided to skip it.

 

Thanks again,

 

~FOX~

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.