CodeIgniter Forums
How do I save anchor links in MySql DB ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How do I save anchor links in MySql DB ? (/showthread.php?tid=53377)



How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]vincej[/eluser]
Hi - I have created a very rudimentary CMS within my CI app. It allows the user to update the text on their pages. The content is saved to MYSQL as text.

I need to be able to now also save a hyperlink. However, when I add the link to the page content it is stored as pure text and not as a link and is presented on the page as pure text. The same applies if I use the CI anchor function. Looking into the DB it is also stored as pure text.

I have a feeling this might be a real newb question - apologies if it is. I have googled around with no success so far.

Many Thanks !




How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]TWP Marketing[/eluser]
Rather than storing a "link", only store the information needed to create the link code:

The path as the uri segments,
the anchor text and
any attributes you need.

Then when you read the db, use a generic piece of code to write your link:

Code:
...
echo anchor($uri, $anchor_text, $attribs);
...



How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]vincej[/eluser]
Many Thanks - I get what you are saying, however, I still have the same problem, the code is saved as echo anchor($uri, $anchor_text, $attribs); and it is echoed out as 'echo anchor($uri, $anchor_text, $attribs);'

it is not being interpreted. it is being saved as text and comes out as text.


How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]TWP Marketing[/eluser]
Did you place it inside the php tags:
Quote:<?php
...
echo anchor($uri, $anchor_text, $attribs);
...
?>



How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]TWP Marketing[/eluser]
I don't believe you understand what I meant.

Your database is used to save four variables:

1) the text that the user enters
2) the text of the url segments, for instance: my_controller/the_function
3) the text which will appear at the anchor text. for instance: 'click here'
4) any attributes needed by the html <a> tag, for instance: a css class, image width and height etc.

You do NOT save the actual text of the CI anchor statement. That goes in your view file.

In your model you create a function to read the database record and pass that information to a view
In your view you will place the code to generate an html anchor statement. For instance, using CI I would use the anchor() helper function (as shown in my previous post).



How do I save anchor links in MySql DB ? - El Forum - 07-21-2012

[eluser]vincej[/eluser]
I think I kind of understand what you are saying .. but let's assume I don't. Let me explain a little more. My rudimentary cms does nothing more that capture text and save it in the DB as text. I recover the text out of the DB through a model as an array and present it in a view using a variable.

All good. So suppose I save the text, "Mary had a little lamb" and I want to make "lamb" a hyperlink. I think you are telling me to place the CI anchor function in the view. The anchor function needs a destination and a text description ie anchor($destination, 'lamb'). I think you are telling me to save $destination in the DB, recover it with a model adn pass it into the view. Ok, what I do not get is that everything in my cms text box is saved as text. This is where I loose you. Nothing that I save gets interpreted. I don't understand how to bridge from text into an interpreted variable.


How do I save anchor links in MySql DB ? - El Forum - 07-22-2012

[eluser]TWP Marketing[/eluser]
Ok, I see now what you are asking.
I can think of a way which night work, but I have not tested it.
You may be able to save the html code itself, not the CI code, but the raw html code.
For example, this is how the text would appear in the html document:
Code:
...
mary had a little <a href="http://yoursite.com/controller/function" title="click here to learn more about lambs">lamb</a>.
...

The whole string can be stored in a varchar or text field in the db, but be sure to properly escape the html code itself. That means using & lt: and & gt; for the carats and escaping the slashes in the URL.
You MIGHT need to experiment with htmlspecialchars().