Welcome Guest, Not a member yet? Register   Sign In
constant will not echo in a function
#1

[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() {

            $sql_cnst = "SELECT
                        *
                    FROM
                        website
                    WHERE
                        site_id=?
                        ";

            $rs_cnst = $this->db->query($sql_cnst, array(SITE_ID));

            foreach ($rs_cnst->result() as $row) {
            define('SITE_LOGO_ID', $row->site_logo_id);
            define('NAV_IMG_ID', $row->nav_img_id);
                   }
               }//end function
Using the constant SITE_LOGO_ID, I fetch the logo/banner/branding image
information
Code:
function fetch_logo_image() {
            $sql_logo = "SELECT
                    image_filename,
                    image_caption
                FROM
                    image_library
                WHERE
                    image_id=?";

        echo "SITE_LOGO_ID 2= " . SITE_LOGO_ID . "<br>";
            $rs_logo = $this->db->query($sql_logo, array(SITE_LOGO_ID));
                    
                foreach ($rs_logo->result() as $row) {
            define('LOGO_FILENAME', $row->image_filename);
            define('LOGO_CAPTION', $row->image_caption);
            }
            
            echo "<p>LOGO_FILENAME:" .LOGO_FILENAME. "<br />";
            echo "LOGO_CAPTION:" .LOGO_CAPTION. "</p>";
    
    }//end function
Yet using exactly the same kind of routine, I can't get the constant NAV_IMG_ID to fetch the navigation image information
Code:
function fetch_nav_image() {
            $sql_nav_img = "SELECT
                    image_filename,
                    image_caption
                FROM
                    image_library
                WHERE
                    image_id=?";

        echo "NAV_IMG_ID 2= " . NAV_IMG_ID . "<br>";
            $rs_nav_img = $this->db->query($sql_nav_img, array(NAV_IMG_ID));
                    
                foreach ($rs_nav_img->result() as $row) {
            define('NAV_IMG_FILENAME', $row->image_filename);
            define('NAV_IMG_CAPTION', $row->image_caption);
            }
            
            echo "<p>NAV_IMG_FILENAME:" .NAV_IMG_FILENAME. "<br />";
            echo "NAV_IMG_CAPTION:" .NAV_IMG_CAPTION. "</p>";
    
    }//end function
I can't even echo the query within function fetch_nav_image(). What’s really strange is that I have no such problems with SITE_LOGO_ID., as you can see from the test echo statements and the way the site logo works in the header banner. What am I missing?
http://clickbasicsdemo.com/index.php
#2

[eluser]paulcj2[/eluser]
I'm getting the echo to work, but not the query, which echoes fine:
Code:
function fetch_nav_image() {
    //echo "<p>Something at the beginning of function fetch_nav_image</p>";
            $sql_nav_img = "SELECT
                    image_filename,
                    image_caption
                FROM
                    image_library
                WHERE
                    image_id=?";
                //check query
            /*$sql_check_query = @mysql_query($sql_nav_img) or die(mysql_error());
echo "<p>Echo check nav img query : $sql_nav_img</p>";*/

        echo "NAV_IMG_ID 2= " . NAV_IMG_ID . "<br>";
            $rs_nav_img = $this->db->query($sql_nav_img, array(NAV_IMG_ID));
                    
                foreach ($rs_nav_img->result() as $row) {
            define('NAV_IMG_FILENAME', $row->image_filename);
            define('NAV_IMG_CAPTION', $row->image_caption);
            }
            
            echo "<p>NAV_IMG_FILENAME:" .NAV_IMG_FILENAME. "<br />";
            echo "NAV_IMG_CAPTION:" .NAV_IMG_CAPTION. "</p>";
#3

[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?
#4

[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.
#5

[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.
#6

[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.
#7

[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.
#8

[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.




Theme © iAndrew 2016 - Forum software by © MyBB