• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
running a script in a php module

#1
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>
proof that an old dog can learn new tricks
Reply

#2
It's not working because you're trying to output HTML image tags in a javascript tag (<script>).
Reply

#3
Is there an html version of a case statement? Perhaps I should have a php case statement instead?
proof that an old dog can learn new tricks
Reply

#4
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)
Reply

#5
(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!
proof that an old dog can learn new tricks
Reply

#6
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?
proof that an old dog can learn new tricks
Reply

#7
Where is "Logo - Research Study Toolkit(3).jpg" located?
Reply

#8
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
proof that an old dog can learn new tricks
Reply

#9
(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.
Reply

#10
It is at /opt/docker-substantiator2/app/index.php
proof that an old dog can learn new tricks
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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