Home › Forums › Add-ons › Repeater Field › Show field x if field y contains…
I’m using ACF to manage a booking availability form.
At present, all links go to webA, so the form is coded to suit webA’s input.
I have some clients who need bookings through webB so need an alternate format on their pages.
I’m trying to write something along the lines of:
if $bookingfield contains ‘booking.com’
echo “”
else if $bookingfield conatins ‘ezyrez.com’
echo””
else
don’t show the form
I’m happy with the if / else logic, but I’m not sure where to start with specifying that $bookingfiled contains “x” or “y” etc.
Would be veyr grateful for any advice
Thanks
webecho
What type of field is the $bookingfield and what is the name of the field?
Hi John
$bookingfield is a url.
field name is accom_booking_link
So depending which website the $bookingfield ponts to, I want to present a different url structure.
i.e. if booking.com show booking.com?checkin=1/1/18:checkout=3/1/18
if ezyrez.com show ezyrez.com?in=1/1/18for2nights
and so on.
Does that make more sense?
$bookingfield = get_field('accom_booking_link');
// full url as returned by acf will be needed in if statements
// I would use a switch because it's a url field the user could enter anything
switch ($bookingfield) {
case 'http://www.booking.com/':
// do something
break;
case 'http://www.ezyrez.com/':
// do something else
break;
default:
// not one of the sites
echo 'Invalid Booking Site';
}
I would not use a URL field here, the main reason being that you are only providing for 2 choices but the user can select from an unlimited number of urls.
I would us a select field, or a radio field so they can only select from the ones I’m prepared to support. In this case the logic would be the same because you can always add more choices later. You would just change out the cases in the switch statement base on the values of the select field.
Hi John
I’ve explained my problem badly I think.
The user can only choose a checkin and a checkout date and press ‘submit’.
PropertyA uses BookingSiteA
PropertyB uses BookingSiteB
both have different syntax for selecting dates
The user selects dates then the dates are input into a link ($bookingfield)
on SUBMIT they are sent to that URL.
So if the use is on PropertyA the syntax is
http://booking.com?checkin=1/1/18:checkout=3/1/18
If the user is on PropertyB the syntax is
http://ezyrez.com?in=1/1/18for2nights
PropertyA and PropertyB booking links are managed from the backend – the user can’t touch them – they’re set.
So what I’m trying to do is find a flexible way of checking what is contained in the booking link field and depending on the result, compiling the URL in a different way.
I’ve only given 2 examples of link syntax for simplicity, i’d like to be able to cover 7 or 8 different possibilities in an IF ELSEIF ELSE statement.
This is the current code, that ONLY goes to booking.com
<?php
$linkout = get_field('accom_booking_link');
$datein = (isset($_GET['checkin']) ? $_GET['checkin'] : null);
$dateout = (isset($_GET['checkout']) ? $_GET['checkout'] : null);
?>
<?php if (strpos($linkout, 'booking') !== false){ ?>
<form id="datepick" method="GET" action="<?php echo $linkout . ';checkin=' . $datein . ';checkout=' . $dateout . ';' ?>">
<h2>Availability</h2>
<ul>
<li><label>Check In </label><input type="text" id="checkin" name="checkin" value=""/></li>
<li><label>Check Out </label><input type="text" id="checkout" name="checkout" value="" /></li>
<li class="submit"><input type="submit" onClick="ga('send', 'event', 'BookingCom', 'availabiliity', '<?php echo $name; ?>' );"></li>
</ul>
</form>
<?php }
?>
You should look at the first code example that I gave then with the switch case based on the url field. A switch/case statement is the same as and if/elseif statement. The main difference is that it’s a lot cleaner.
Think of it like this
$bookingfield = get_field('accom_booking_link');
if ($bookingfield == 'first url') {
} elseif ($bookingfield == 'second url') {
} elseif ($bookingfield == 'third case') {
} elseif ($bookingfield == 'fourth case') {
} else {
// default case
}
This gets quite cumbersome after several elseif’s and is horrible to look at an manage, but can be done this way if you want. When dealing with many possibilities a switch statement can drastically reduce the amount of typing you need to do to code it.
Thanks John
I have to admit, I’ve never used Switch statements so I’ll have to take a proper look at them.
The code you’ve provided looks like it’ll do the trick.
My last issue is that each url has a unique property identifier on it.
i.e. booking.com?propID=1234
I’ll have to try it out and see if I can use wildcards / regex within ACF.
Thanks again for your help
Just to update.
of course it was simple in the end…
if (strpos($linkout, 'booking.com') !== false){ ?>
//do something
} elseif (strpos($linkout, 'ezyrez.com') !== false){ ?>
//do something else
}
Thanks for sending me in the right direction John
webecho
The topic ‘Show field x if field y contains…’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.