enter image description hereSo I'm trying to add a few lines of custom PHP code to a site that I am using Magento with. I have some understanding that I would need to create a module and the necessary files and directories but every thing I've has either resulted in a broken page or my result just not showing up at all. I want to be able to create the code as well as maybe call to it in the phtml file I want it to show on, how can I do this? Also here is the code I want to put in:
<?php
$h = date('G'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
if ($h < 7) $img = 'fish.jpg';
else if ($h < 20) $img = 'frogs.jpg';
else $img = 'potatoes.jpg';
//if it's before 7am, use fish image
//if not and it's before 8pm, use frogs
//otherwise, potatoes
?>
2 Answers 2
So copy this code to your .phtml file. Make sure to edit the path to your image files.
<?php
$h =date('G');
if($h < 7) {
$img = '/path/to/your/fish.jpg';
} else if($h < 20) {
$img = '/path/to/your/frog.jpg'
} else {
$img = '/path/to/your/potatoes.jpg'
}
?>
<div class='server-date'> <img src='<?php echo $img ?>'/> </div>
Remind the image will only change when the user refresh the page because php is a server side language.
-
I added this code to my phtml file in phpstorm but I'm getting some errors and I'm not quite sure how to fix them I will add them to the postMhernandez– Mhernandez2015年11月02日 13:30:55 +00:00Commented Nov 2, 2015 at 13:30
-
My mistake i missed a quote in date() if there still some errors post me the messages or try the code above againPhilipe Reichert– Philipe Reichert2015年11月03日 07:01:46 +00:00Commented Nov 3, 2015 at 7:01
-
Thank you, the errors are no longer there, but when I save the changes the image appears broken, do you know why that may be?Mhernandez– Mhernandez2015年11月03日 14:38:16 +00:00Commented Nov 3, 2015 at 14:38
-
I added this initially as a module, may that be the issue?Mhernandez– Mhernandez2015年11月03日 15:28:19 +00:00Commented Nov 3, 2015 at 15:28
-
I seem to have gotten the image to display, but it only displays the image used in the second $img function rather than changing at a specific time, do you see anything that I could change to actually cause it to change?Mhernandez– Mhernandez2015年11月03日 21:15:54 +00:00Commented Nov 3, 2015 at 21:15
Where do u want to place this code? and what do you want to do with it?
<?php
$h =date('G);
if($h < 7) {
$img = 'fish.jpg';
} else if($h < 20) {
$img = 'frog.jpg'
} else {
$img = 'potatoes.jpg'
}
?>
-
That code is a separate php file, I would like it to display on my web page by somehow calling to in my phtml file, yet I don't fully understand how to do it, I've used <? php include('') ?> with the name of the php file in the parenthesis yet it does not appear when I reload the page. I would like to put it in between this div class on my phtml file <div class="server-date"> </div>Mhernandez– Mhernandez2015年10月30日 17:20:23 +00:00Commented Oct 30, 2015 at 17:20
-
Also for more clarity I created a module for this php file as well as all of the necessary directories for a module, I'm not sure if that is entirely necessary for what I'm trying to achieve here, but from what I've gathered that's what I've done.Mhernandez– Mhernandez2015年10月30日 17:26:30 +00:00Commented Oct 30, 2015 at 17:26
Explore related questions
See similar questions with these tags.
if ($h < 7){ $img = 'fish.jpg'; } elseif ($h < 20){ $img = 'frogs.jpg'; } else { $img = 'potatoes.jpg'; }