Jump to content

Unchecked checkboxes issue


onjegolders
 Share

Recommended Posts

Hey guys, I'm having an issue where I have a checkbox for deciding whether a link is shown to a website. I have a field to hold the url and the checkbox field.

My code is:

if ($page->project_url AND $page->show_url = 1) { ?>
<a href="<?php echo $page->project_url; ?>" target="_blank" class="project_link flr tar">←  Visit Site</a>
<?php }

I have also tried in parentheses:

if ($page->project_url AND ($page->show_url = 1)) { ?>
<a href="<?php echo $page->project_url; ?>" target="_blank" class="project_link flr tar">←  Visit Site</a>
<?php }

The problem is, the links are displaying regardless.

Any ideas what I may have done wrong? I'm still wearing my dunce's hat by the way...

Link to comment
Share on other sites

A handy bit of info on comparison operators: http://php.net/manual/en/language.operators.comparison.php

In your case you're making $page->show_url equal to 1 regardless of it's true value by setting it to 1 using just one '=' whereas you need two '==' to compare the field value against another value.

Granted, PW's selectors only use one = but they're a bit special like that ;)

Link to comment
Share on other sites

Thanks both of you, it doesn't seem to work though with "==" either.

I'm sure I've seen checkboxes with single "=" too as Pete mentions.

The field is definitely called show_url so not too sure what is happening here.

Is there any way to debug?

Thanks.

Link to comment
Share on other sites

Just an idea, what do you get doing the following:

var_dump($page->project_url);

Because if $page->project_url is null/false or an empty String '', the if condition will never be true.

Empty strings are converted to false by PHP.

Edit:

Oops just realized that your problem's the opposite. :rolleyes:

You must use the "==" operator.

What do you get when you're doing this on a page where show_url should be false:

var_dump($page->show_url);
Link to comment
Share on other sites

A single "=" will only work in PW's selectors like $page->find('something=something') and so on - you must have double == to compare values in normal PHP as per the link I posted.

Strange that that's not working either though, but a single "=" is definitely wrong.

Just a thought - what is the expected value of $page->project_url? If it's just a text field then you might want to check it's not empty with if (!empty($page->project_url as it would exist whether empty or not, so simply doing if ($page->project_url would always be true I think. If on the other hand it is a Page fieldtype, then try if ($page->project_url->id instead.

Link to comment
Share on other sites

Hi Pete, sorry we must have posted at the same time!

Thanks for clearing up the "=" thing.

The value of project_url is a url (I guess a text string) but my if statement specifies project_url AND show_url so both criteria ought to be met before outputting the url.

I'm guessing the int(0) means that show_url is unchecked?

Which is sort of good news I suppose except that it's still displaying, I will have to go over it again to see if I'm missing something stupid!

Link to comment
Share on other sites

Wanze, I've just copied and pasted your if statement and I don't believe it because it works! So strange as I have tried (or so I thought), every permutation with both single and double "=".

I must have missed something out, though I was sure I tried this exact statement! Strange....

Anyway, a big thank you for all of your help.

Link to comment
Share on other sites

I guess you got this thing because you used AND operator, not &&. AND has less priority then && and combined with = (assign operstor) instead of == (equality comparison operator) it gave you this strange behaviour.

I guess you could also do this, granted your $page->show_url takes values only of 1 and 0:

if ($page->project_url && $page->show_url) {
//do your thing
}
  • Like 1
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...