• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
codeigniter is not parsing html from db for php tags...?

[eluser]Colin McCubbin[/eluser]
I'm new to codeIgniter and an struggling with this.. :-S

If I put some php code in <?php...?> tags in amongst the html in a view file, codeigniter parses and acts on the php before sending the page to the browser with no problem.

But if I put that same block of mixed html and php in a database field and then get codeigniter to get it from the db, place it in a variable and output it, the php is not parsed and just shows up as text..

EG. I load the html helper in my controller, and in my view.php file have
<?php echo img('system/application/pics/Aragon.jpg');?>
The image shows up fine in the browser.

If however I place the same line in my db field and get that and output it, it jus appear as a text string... So I get no image in the page, and if I 'view source' I see the php tags in the source..

What am I doing wrong here? Do I have to pass the db contents back into the php parser somehow? I'd have thought that codeigniter would have done that..


[eluser]Armchair Samurai[/eluser]
If you fully intend to do it this way or have no other choice, you'll need to use eval().

This of it this way: at the moment, you're essentially doing this:

<?php echo "foobarfoobarfoobar <?php echo img('system/application/pics/Aragon.jpg');?> foobarfoo bar foobar";?>

... which, of course, will output that exact string.

[eluser]Colin Williams[/eluser]
Doesn't sound like such a good idea. Don't just eval() it. You ought to put in some precautions.

[eluser]Colin McCubbin[/eluser]
Thanks for the info.. I've realized that yes, ci is just outputting the contents of a variable, so doesn't do any parsing on it.

And Colin, thanks , I'll check out the template library .

I fixed my problem for now, the php code was just referencing images, and after trying various paths I found that if I moved my 'pics' folder to be a sibling to 'system', (ie under the site root) I just had to replace all refs in my database from code such as:
<?php echo img('system/application/pics/Aragon.jpg');?>

to a straight html:
<img src="../pics/Aragon.jpg" />
and everything works fine.

I'm sure there is 'better way' to do this, but I'm learning oop and ci hand in hand and if everything worked first time I'd probably learn less! I had thought that paths would be relative to the file they were 'in', eg that pics would be referenced as a sibling of the view folder that contained the view, but it turned out that it had to be referenced to the original controlling index.php file (I guess??)

[eluser]Colin McCubbin[/eluser]
Aaagh! Something is still wrong with my understanding of the paths involved.

menu.php is my controller, and menu_view.php is the view, in their respective folders.

1. If I type
the expected page with my images appear.

But to set menu as my default controller, in routes.php I set
$route['default_controller'] = "menu";
and now:

2. When I type
my page appears with the text from the db, so menu is working, but no pictures...

I've tried adding a duplicate 'pics' folder as a sibling to 'controllers' and 'views' but still no pictures.

I'm currently at a loss to under stand the relationship 'pathwise' between html in view files and folders elsewhere on the site, especially as it seems to alter as described above.

In the first instance where the picture displays, if I do view picture in my browser the url is as expected, namely

In the second, no picture, if I do view picture in my browser, the url is:
so I'll have to go into my db again and change the html links from "../pics/Aragon.jpg" to just "pics/Aragon.jpg"

Edit: Tried this and it works.. :lol:

If anyone can take the time to explain why the paths changed to me I'd be very much in your debt!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.