PHP help needed on very simple issue

Permalink
I want to pull a page attribute (image) to load into a css object in the header. The css code looks like this:

<style>
   #content: background-image: url(images/art2.jpg);
</style>


and my php, like this:

<style>
   <?php
      echo '#content{ background-image: url(';
      $c->getAttribute('background')->getVersion()->getRelativePath();
      echo ', {speed: 500}); }';
   ?>
</style>


and it doesn’t work. I've tried, I really have. I've analyzed the semicolons and the parens and Im lost. Im calling it in the header, of course.

Help,
steve

tallacman
 
triplei replied on at Permalink Reply
triplei
Just a random guess (I haven't tried and verified it) but, would adding an echo on attribute work?
<style>
   <?php
      echo '#content{ background-image: url(';
      echo $c->getAttribute('background')->getVersion()->getRelativePath();
      echo ', {speed: 500}); }';
   ?>
</style>
tallacman replied on at Permalink Reply
tallacman
That gets me a blank page.
12345j replied on at Permalink Reply
12345j
try
<style>
   #content{ background-image: url(<?php echo $c->getAttribute('background')->getVersion()->getRelativePath());?>)
   }
</style>
are you sure you're using the correct syntax for loading the javascript? do you have a link we could see?
tallacman replied on at Permalink Reply
tallacman
javascript?

I thought it was php.
12345j replied on at Permalink Reply
12345j
don't think there is a {speed} declaration in css or php, so I assubmed you were using supersized or something like that.
tallacman replied on at Permalink Reply
tallacman
The url is:http://greenpedals.c5themepark.com/...
this is without the code because if I put that in the page doesn’t render.

steve
tallacman replied on at Permalink Reply
tallacman
this works:

<style type="text/css">
#content{background-color: red;}
</style>


but that’s not what I need. An image would have to be called from its attribute.
12345j replied on at Permalink Reply
12345j
<style>
   #content{ background-image: url(<?php echo $c->getAttribute('background')->getVersion()->getRelativePath();?>)
   }
</style>
works for me.
tallacman replied on at Permalink Reply
tallacman
Thanks, but blank page.
BTW, Im loving your new blocks.

steve
triplei replied on at Permalink Reply
triplei
if you view source on the blank page, are there any php errors being output?
tallacman replied on at Permalink Reply
tallacman
Yes! finally:
Fatal error</b>:  Call to a member function getVersion() on a non-object in <b>/home/harris7/public_html/greenpedals/themes/theme_greenpetals/elements/header.php</b> on line 17

Line 17 is the code line between the style calls.

Thanks,
steve
12345j replied on at Permalink Reply
12345j
that error should only happen if the attribute is not defined on that
page or the attribute isn't installed- can you check that?

On 5/12/11 10:18 PM, Concrete5 Community wrote:
TimDix replied on at Permalink Best Answer Reply
TimDix
A little page I made to help some of my co workers work with attributes, the idea was to quickly easily copy the snippets from this file.

http://cantanogames.com/c5/attributes.html...

For your example, all you should need is this:

<? if($file = $c->getAttribute('background') ) { ?>
<style>
   #content{ 
      background-image: url(<?=$file->getRelativePath();?>);
   }
</style>
<? } ?>


You don't need getVersion()->, because if you take a look at getRelativePath, it automatically takes care of it for you.

Also note, that I test to make sure that the attribute exists before using it. Very important you do that with all code.

Yes, I use short tags :p
tallacman replied on at Permalink Reply
tallacman
Thanks to all of you for your help. And thanks to Raverix for the correct way of doing this and the mini-tut.

So I'm taking it that if you call an attribute you have to test as to whether it exists or not. I thought that if it didn’t find anything it would just skip it.

I understand php a little better since starting project.

Thanks again,
steve