CodeIgniter Forums
Storing image locations using constants. - 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: Storing image locations using constants. (/showthread.php?tid=44143)



Storing image locations using constants. - El Forum - 08-04-2011

[eluser]XstreamINsanity[/eluser]
I want to use the constants.php file to store common image locations. I have a menu and each item has an image of it's own, and currently for each image in the view, I have:

<?php echo $this->config->item('base_url'); ?>/images/database.png"/>

Or whatever the filename is. What I want to do is store a constant "DB_IMG" in this instance, and simply be able to say in my view:

<?php echo DB_IMG; ?>

One question I have is can I use the $this->config->item('base_url') inside the constants.php file? Secondly, is it a good practice to store image names as constants in a constants file? Thanks for all of the help.


Storing image locations using constants. - El Forum - 08-04-2011

[eluser]John_Betong_002[/eluser]
 
Did you try it?
 
Did it work?
 
If it did not give the expected result did you consult the "User Guide"?
 
 
 


Storing image locations using constants. - El Forum - 08-09-2011

[eluser]XstreamINsanity[/eluser]
No, it didn't work. I looked at the User Guide some, but I didn't see anything specifically that shows how to add your own constants. I'm still looking though.


Storing image locations using constants. - El Forum - 08-09-2011

[eluser]John_Betong_002[/eluser]
Try this:

./application/config/constants.php
Code:
$base_url= $_SERVER['SERVER_NAME'];
define
(
  'DB_IMG',
  "<img src='http://" .$base_url .'  class='db_img' alt='#' />"
);
&nbsp;
You could also take a look at creating an additional config.php file.
&nbsp;
&nbsp;&nbsp;
&nbsp;

edit: problem due to square brackets, code got truncated so rewrote, now displays OK.



Storing image locations using constants. - El Forum - 08-09-2011

[eluser]XstreamINsanity[/eluser]
What got truncated on the $SERVER part of the code? One thing I've done so far is put the parts of the source that aren't a part of the base url into the database. But that is only a temporary fix. I did that for my menu but I'd rather it not be in the database. Thanks for the help so far.