CodeIgniter Forums

Full Version: running a script in a php module
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a php module  and in it I am trying to show a different logo depending on the "branding". I have the code below in a footer and I would expect at least one of the logos to appear. But none do. What am I missing? 

Code:
         <div class="row">
            <div class="span6 b12">
                <script>
                    switch(1) {
                        case 1:
                            <img src="<?php echo base_url(); ?>assets/themes/default/images/Logo - Research Study Toolkit(3).jpg"
                                 style="float:left;margin-top:5px;z-index:5" alt="logo"/>
                            break;
                        case 2:
                            <img src="<?php echo base_url(); ?>assets/themes/default/images/Logo - Original2.jpg"
                                 style="float:left;margin-top:5px;z-index:5" alt="logo"/>
                            break;

                        case 3:
                            <img src="<?php echo base_url(); ?>assets/themes/default/images/Logo - Pharma Research Study Toolkit2.jpg"
                                 style="float:left;margin-top:5px;z-index:5" alt="logo"/>
                            break;

                        case 4:
                            <img
                                src="<?php echo base_url(); ?>assets/themes/default/images/Logo - Climate Research Study Toolkit.jpg"
                                style="float:left;margin-top:5px;z-index:5" alt="logo"/>
                            break;

                        default:
                            <img
                                src="<?php echo base_url(); ?>assets/themes/default/images/Logo - Research Study Toolkit(3).jpg"
                                style="float:left;margin-top:5px;z-index:5" alt="logo"/>
                    }
                    </script>
            </div>
        </div>
It's not working because you're trying to output HTML image tags in a javascript tag (<script>).
Is there an html version of a case statement? Perhaps I should have a php case statement instead?
No there isn't, HTML doesn't support logic.

If all you need to do is selectively display an image, you should use PHP.

Code:
<?php
switch (1) {
    case 1: $img = 'Logo - Research Study Toolkit(3).jpg'; break;
    case 2: $img = 'Logo - Original2.jpg'; break;
    // case 3 + 4 omitted for brevity
    default: $img = 'Logo - Research Study Toolkit(3).jpg';
}

$src = base_url("assets/themes/default/images/{$img}");
$img = "<img src='{$src}' style='float:left;margin-top:5px;z-index:5' alt='Logo'>";
echo $img;
?>

(Also, creating a switch() on the value of "1" will always result in the same image appearing)
(01-30-2021, 05:00 AM)craig Wrote: [ -> ]No there isn't, HTML doesn't support logic.

If all you need to do is selectively display an image, you should use PHP.

Code:
<?php
switch (1) {
    case 1: $img = 'Logo - Research Study Toolkit(3).jpg'; break;
    case 2: $img = 'Logo - Original2.jpg'; break;
    // case 3 + 4 omitted for brevity
    default: $img = 'Logo - Research Study Toolkit(3).jpg';
}

$src = base_url("assets/themes/default/images/{$img}");
$img = "<img src='{$src}' style='float:left;margin-top:5px;z-index:5' alt='Logo'>";
echo $img;
?>

(Also, creating a switch() on the value of "1" will always result in the same image appearing)
-yes, this is just for testing!
I copied the code exactly.

$src = base_url("assets/themes/default/images/{$img}");
this is what is being generated:
<img src='http://localhost/assets/themes/default/images/Logo - Research Study Toolkit(3).jpg' style='float:left;margin-top:5px;z-index:5' alt='Logo'>

And here is the attempt to print it:
echo $img;

This does not give an error but the logo just shows up as a broken  "Logo". I think the problem is that path is wrong. I tried copying the path directly into the browser, but that also fails. Is there a better way to get the path to the file?
Where is "Logo - Research Study Toolkit(3).jpg" located?
It is in a docker container (as is the rest of my code). Here is the full path to it on my hardrive.

/opt/docker-substantiator2/app/assets/themes/default/images/Logo - Research Study Toolkit (3).jpg
(01-31-2021, 06:48 AM)richb201 Wrote: [ -> ]It is in a docker container (as is the rest of my code). Here is the full path to it on my hardrive. 

/opt/docker-substantiator2/app/assets/themes/default/images/Logo - Research Study Toolkit (3).jpg

And where is CI's index.php?

To display assets properly, they must be in a directory accessible by the web server.
It is at /opt/docker-substantiator2/app/index.php
Pages: 1 2