I need php help

Permalink
I have created a page checkbox attribute called 'no_header_image' and Ive successfully displayed it with:

  <?php $c ->getCollectionAttributeValue('no_header_image');
 echo $c ->getCollectionAttributeValue('no_header_image'); ?>


But I want to execute an echoed CSS statement if it is true (checked) and not really display it. Can anyone help me with that piece of code. I got the existing code from Weblicating, but I cant make it work.

Thank you,

Steve

tallacman
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
A great general programming tip is that when things start getting too complicated to understand, start "encapsulating" them into simpler things. For example, if you have a bunch of code, it helps to put it into a function so you can just call all of that code with a single function call.

In your case here, I would "encapsulate" that long-winded call to getCollectionAttributeValue into a variable, which then will make it clearer how to proceed. So first do this:
<?php
$yes_header_image = !$c->getCollectionAttributeValue('no_header_image');
?>

Note that I've "flipped" the value from a negative to a positive (in the English language grammatical sense) -- it is much easier to think about things when you avoid negatives and double negatives (e.g. "is this not not showing a header image?").

So next what I'd do is this:
<?php if ($yes_header_image) { ?>
.my_class {
   bacground-image: url(path/to/image.jpg); /* or whatever */
}
<?php } ?>


Sorry for the long-winded answer, that's just my thought process on these things.

Let me know if that makes sense or not. Or if you post the actual style you want with and without the header image I can just give you the exact code to use.

-Jordan
tallacman replied on at Permalink Reply
tallacman
jordanlev,

I really appreciate your walk thru on this. It works perfect and helps me understand a little more.

Is there a difference between

$c-> and $c -> ?
And if you wouldn’t mind explaining; what does it mean?

Im also confused about => and what it means.

A lot of other stuff is clear but I never see anyone explain these.

Thanks,
Steve
jordanlev replied on at Permalink Reply
jordanlev
Hi Steve,
My pleasure -- I find that explaining things to people actually helps me solidify concepts in my own head, so it works out well for both of us :)

"$c ->" (with the space in it) is invalid code -- there is no such thing, and you will get an error if you have that (although you might not see the error, if for example C5 is in production mode).
What "$c->" (without space) means is that there's an object that has been assigned to the variable $c and we want to call one of its functions. Similar to my above example, objects are another way to "encapsulate" code (to group stuff together so it's out of your way and easier to understand). An object is like a variable that contains tons of other variables and also functions as well. So somewhere deep in the bowels of concrete5, there is some code that assigns all of the data about the page you're on to a variable called $c. One of the many (many) functions included in the "page" (also called "collection" in Concrete5) object is the "getCollectionAttributeValue()" function, which as you can guess gets the attribute value for that page that is currently being displayed (as opposed to some other page in the site).

"=>" does a couple of different things depending on the context it's used in. Two of the most common places it's used are:
1) Assigning values to an array:
$favorite_colors = array( 'john' => 'red', 'jim' => 'blue', 'mary' => 'green');

It's really the same thing as an equal sign, and honestly I'm not sure why they didn't just use an equal sign, but as with any language (human, programming, or otherwise) there are inconsistencies that arise for various technical and historical reasons.

2) In a "for" loop:
for ($favorite_colors as $key => $val) {
   echo $key . "'s favorite color is " . $val;
}

In this context, the "=>" is what you use to show that you want both the key and the value of the array to be available as you loop through it. Not sure why it's used here, it's completely arbitrary, but that's just the way it is.

BTW, PHP is an exceptionally inconsistent language -- this is one of the many gripes that some programmers have with it (but that's a whole other topic).

Hope that helps!

-Jordan
TheRealSean replied on at Permalink Reply
TheRealSean
Really good explanation, thank you.

It was not until I started working with C5 that I realised that you can have objects within objects.

ie $this->controller->getTask()

it took me a while to call specific variables when I first started as I was only ever used to using array variables within objects.