Jump to content

Echo Background Image Breaking site


CDG

Recommended Posts

I am trying to add a background image via a custom style, but it breaks my site. I am a php novice, can someone point out my error please?

The echo is being used other places on this page, so I know this code is ok:

<?php echo $event->data->thumbnails['medium']; ?>

but I am missing something here, this keeps breaking the page:

<?php
        echo '<article data-style="'.$label_style.'" style="background-image:url(\<?php echo $event->data->thumbnails['medium']; ?>\")" class="mec-event-article mec-clear '.$this->get_event_classes($event).'"' . $colorful_bg_color . '>';
?>

Thanks for any help, as I learn!

Link to comment
Share on other sites

Try it without the <?php echo ... ?>

You are already inside a php block of code

echo "<article data-style='$label_style' style='background-image:url({$event->data->thumbnails['medium']})' class='mec-event-article mec-clear {$this->get_event_classes($event)} $colorful_bg_color>";

 

Link to comment
Share on other sites

I think those single quotes will make that fail as well. This might work:

echo "<article data-style=\"$label_style\" style=\"background-image:url($event->data->thumbnails['medium']\") class=\"mec-event-article mec-clear $this->get_event_classes($event) $colorful_bg_color\"";

 

Link to comment
Share on other sites

Thanks for all the help!  I have narrowed it down to this part of the line, as this is what I added and started screwing things up.

style="' . background-image:url(<?php echo $event->data->thumbnails['medium']; ?> . '"

I added the concatenate to this...but, I am not really sure how to do it correctly. 

 

Link to comment
Share on other sites

One CANNOT post a very small (and out of context) snippet of code and expect it to be debugged.  If you have a problem with a line of code post that line.  Not a piece of it.  As was pointed out already, you were using php tags inside of php tags and that is part of the problem.

As a php noob, you need to recognize that one does not need to switch into and out of php mode with every line of code.  Good programming practice is to write all of your php code in php mode and all of your html outside of it.  Don't mix and match except for the parts that need to be "built" from a combination of php logic and html presentation, such as a complex link statement or an html table row, etc.  Personally I write my php scripts with very few php on/off tags - maybe 2-3 pairs in scripts that may be 300+ lines long.  Get the idea?

 

So let's see this problem line in its entirety.

Link to comment
Share on other sites

Thanks ginerjm,

 

This is the entire line with my attept at adding concatenation and removing the second call to turn php on.  You post is really helpful as I learn!

        <?php
        echo '<article data-style="'.$label_style.'" style="background-image:' .url($event->data->thumbnails['medium']);. '" class="mec-event-article mec-clear '.$this->get_event_classes($event).'"' . $colorful_bg_color . '>';
        ?>

 

Edited by CDG
Link to comment
Share on other sites

I guess my intention with pointing out that section of the line is, that if I remove it all together, then the site works, but without the modification I am trying to achieve.

(below is a slight different version, as I am blindly fumbling about, so if i remove this the page loads fine, so something in here is screwy :

style="'.background-image:url($event->data->thumbnails['medium']).'"

 

Link to comment
Share on other sites

Sometimes our desires to use OO coding inside other pieces of code (and other complex notation) can get us into trouble.  Here's my attempt at resolving that issue here.  I wasn't sure about that last piece of your statement but assumed it was a class name so this is what I did:

$url = $event->data->thumbnails['medium'];
$cls = $this->get_event_classes($event);
 echo "<article data-style='$label_style' 
			style='background-image:url($url)' 
			class='mec-event-article mec-clear $cls $colorful_bg_color'>";

Note the lack of php mode tags - I am assuming that this part of your script is already in php mode.  And notice how using a double quote as the outermost quote of a string allows you to avoid using the php tag in the middle of your string, as well as removing all of the concatenation too.  Remember - using double quotes in this fashion will allow your embedded php vars to be properly interpreted even though they may themselves be wrapped inside single quotes.  An important rule to learn as you improve your php skills.

Please examine it carefully.  I may have mis-interpreted your intent, but I do think that you can correct any of that without changing my line very much.  If you study it and try to understand how it works, it will be a learning experience.  I may have the background image url coded wrongly - there may need to be quotes in there somewhere so if so this may be the corrected method:

style=\"background-image:url('$url')\"

Edited by ginerjm
correct background image
Link to comment
Share on other sites

FYI - the following line of code has an extra semi-colon in the middle, after the call to url():

<?php
echo '<article data-style="'.$label_style.'" style="background-image:' .url($event->data->thumbnails['medium']);. '" class="mec-event-article mec-clear '.$this->get_event_classes($event).'"' . $colorful_bg_color . '>';
?>

 

With that said, I think ginerjm's suggestion for creating variables like $url will make the code easier to read down the road, when you're less familiar with the code.

Link to comment
Share on other sites

Thanks everyone...  I am playing and learning!  cyberRobot...thanks for pointing that out, i did finally see that...  Ill report back as I progress. ginerjm's suggestion is great...

Edited by CDG
Link to comment
Share on other sites

I just noticed that the call to url() should go with your CSS. So it should be enclosed in your single quotes.

<?php
echo '<article data-style="'.$label_style.'" style="background-image:url(' . $event->data->thumbnails['medium']. ');" class="mec-event-article mec-clear '.$this->get_event_classes($event).'"' . $colorful_bg_color . '>';
?>

Once it's enclosed, the semi-colon is fine. Of course, the point is probably moot if you're using ginerjm's code. ?

Link to comment
Share on other sites

Victory, although i studied other templates in this plugin that were working...thanks for getting me some new understanding of php.  This is what did it, the first bit is farteher up in the page, where the calls are specifed:

$event_thumb_url = $event->data->featured_image['large'];

<article class="mec-event-cover-modern <?php echo $this->get_event_classes($event); ?>" style="background: url('<?php echo $event_thumb_url; ?>'); height: 678px;background-size: cover;">

 

Link to comment
Share on other sites

In case you're interested, there's a tag for opening PHP and echoing at the same time.

<article class="mec-event-cover-modern <?=$this->get_event_classes($event)?>" style="background: url('<?=$event_thumb_url?>'); height: 678px;background-size: cover;">

I don't remember what the tag is called.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.