Jump to content

Question about checkbox field.


3.14159rate
 Share

Recommended Posts

Hey, so I have some code that goes out and finds all fields on a page that are labeled with "skill", all of these fields are checkboxes. I then want to display some text if the checkbox is checked, but I can't seem to figure out how to check if the checkbox is checked.

$skills = $curpage->fields->find("label=Skill");
foreach($skills as $skill)
{
if($skill == "1")
{
 echo"{$skill->name} ";
}
}

what am I doing wrong? I normally write in C and not php (and not web stuff) so I don't really know the language super great.

Link to comment
Share on other sites

I think this should do it. A check box is either 1 (true), or 0 if not checked (false). Also if you get fields like this you have to get the field of the page afterwards. Let me know if that works.

$skills = $curpage->fields->find("label=Skill");
foreach($skills as $skill){
 if($curpage->get($skill)){
   echo"{$skill->name} ";
 }
}
  • Like 1
Link to comment
Share on other sites

Greetings,

I use this logic extensively in my sites to display only those articles that have a certain checkbox checked. I think of this as a way to create "instant categories"!

Here is an example of the code I've been using. In this case, the organization has several "events" (each with its own checkbox). In the example below, the template displays a series of "intro images" only for "annual festivals" (in other words, the user checked "annual_festival."):

<?php
$eventlist = $pages->find("template=event, annual_festival=1, sort=-date, limit=50");
foreach($eventlist as $event)
{
?>
 <div id="listings_box">
   <img src="<?php echo $event->intro_image->size(300, 225)->url ?>" />
 </div>
<?php
}
?>

It works great! In your case, once you find the pages that are checked according to your requirements, you can then call the relevant text field.

Thanks,

Matthew

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...