0

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
?>
  • Enclose the if elseif (one word) else in { } brackets if ($h < 7){ $img = 'fish.jpg'; } elseif ($h < 20){ $img = 'frogs.jpg'; } else { $img = 'potatoes.jpg'; } – Michael Oct 30 '15 at 14:47
  • have added php code in module block or model file? – Abdul Oct 31 '15 at 7:21
0

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 post – Mhernandez Nov 2 '15 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 again – Philipe Reichert Nov 3 '15 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 Nov 3 '15 at 14:38
  • I added this initially as a module, may that be the issue? – Mhernandez Nov 3 '15 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 Nov 3 '15 at 21:15
0

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 Oct 30 '15 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 Oct 30 '15 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.