constant will not echo in a function |
[eluser]paulcj2[/eluser]
Using CodeIgniter, I am building constants within a function for a navigation image. For some reason what works in one function does not work in another. I have defined two constants: one for a logo image to display in a banner and one for a navigation image to display just below the banner. Code: function fetch_site_constants() { information Code: function fetch_logo_image() { Code: function fetch_nav_image() { http://clickbasicsdemo.com/index.php
[eluser]paulcj2[/eluser]
I'm getting the echo to work, but not the query, which echoes fine: Code: function fetch_nav_image() {
[eluser]GSV Sleeper Service[/eluser]
why are you defining constants in a loop? wouldn't you be better off storing them as session vars instead?
[eluser]xwero[/eluser]
As GSV Sleeper Service mentioned constants in a loop are not good because a constant can't be redefined, this will result in an error. But after taken a closer look you query only one row. Then you don't need to use result but row which gives you the possibility to remove the loop. I think they way you use constants is error_prone. You define them to use it in another function so this means fetch_site_constants always has to be called before calling fetch_logo_image. But a developer doesn't see the two functions are connected looking over the code. There are two ways that are better. The fetch_site_constants returns an array of constants, $query->result() could be enough. And you either add the fetch_site_constants function as a parameter to the fetch_logo_image function or more tightly coupled you call the fetch_site_constants in the fetch_logo_image function. In the fetch_logo_image function there is no reason to define constants because you are outputting the query result already. I don't think you are going to use NAV_IMG_FILENAME somewhere else. The rule of thumb for defining functions is see that they are used site wide. if they aren't you are better of using another way of transporting data from one function to another.
[eluser]paulcj2[/eluser]
Quote:The rule of thumb for defining functions is see that they are used site wide. if they aren’t you are better of using another way of transporting data from one function to another. All these constants are used site-wide.
[eluser]xwero[/eluser]
I find that hard to believe because a site has more than one section which means that you have more than one image.
[eluser]paulcj2[/eluser]
This image is part of the navigation, which is site-wide. The other image is the banner, which is also site-wide. All other constants defined on this page are site-wide.
[eluser]paulcj2[/eluser]
Turns out the issue had to do with the way the image was loaded into the database. The script works fine. Thanks for your feedback. |
Welcome Guest, Not a member yet? Register Sign In |