If NO ENTRY - DISPLAY THIS

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 18, 2019   (RSS)

By csdesign - January 17, 2019

Hello! Thank you in advance for the assistance. I'm hoping it's just something easy and obvious that I'm overlooking. 

I'm posting job listings by location for 5 cities. I'm including the code for one since they are identical, except for the job_location name. 

What I'm trying to achieve is that if there are NO job listings for an individual location to post "No current job listings for this location. Please check back!"
On my screenshot, you can see that there are job listings in Miles City (works fine) but not in Billings - but I'm not getting "No current job listings..." text to show. 

I also tried using the code below and that actually worked but it listed "No current listings..." for all 7 jobs - so 7 times in a row. 

<?php if (strpos($record['job_location'],"\tMiles City MT\t") !== true) : ?>
No current listings for this location
<?php endif ?>

The code below is what I'm working with now. I used "title" in the if/else because that's required for each job listing but it could be job_location too if needed.

Thanks!!!  Tina

<!-- JOBLIST -->
<h2>JOBS IN MILES CITY, MT</h2>
<?php foreach ($careers_listRecords as $record): ?>
<?php if ($record['job_status'] == 'Active'): ?>
<?php if (strpos($record['job_location'],"\tMiles City MT\t") !== false) : ?>
							
	<div class="alert alert-info">
	<strong><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></strong> &nbsp;<a href="<?php echo $record['_link'] ?>"> <i class="icon-line-minus"></i> Job Details <i class="icon-hand-up"></i> </a>
	</div>
							
	<?php if($record['title']) :?>
        <?php else : ?>
	No current job listings for this location. Please check back!
	<?php endif; ?>
					
<?php endif; ?>
<?php endif ?>
<?php endforeach ?>
						

By daniel - January 18, 2019

Hi Tina,

Fortunately yes, the solution is fairly simple! The main thing is you'll want to check outside of the loop to see if it contains any of the elements you're looking for, rather than inside of the loop. For your current setup, I'd recommend something along the following lines:

<!-- JOBLIST -->
<h2>JOBS IN MILES CITY, MT</h2>
<?php $foundJob = false; ?>
<?php foreach ($careers_listRecords as $record): ?>
<?php if ($record['job_status'] == 'Active'): ?>
<?php if (strpos($record['job_location'],"\tMiles City MT\t") !== false) : ?>
	<?php $foundJob = true; ?>
	
	<div class="alert alert-info">
	<strong><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></strong> &nbsp;<a href="<?php echo $record['_link'] ?>"> <i class="icon-line-minus"></i> Job Details <i class="icon-hand-up"></i> </a>
	</div>
						
					
<?php endif; ?>
<?php endif ?>
<?php endforeach ?>

<?php if (!$foundJob): ?>
  No current listings for this location
<?php endif; ?>

The basic idea is to create a new variable before the loop and set it to false, then sets it to true any time the list outputs a job. Then after the loop, you can check if the variable is true or false, which will let you know if any jobs were output for that location. Just remember to set the variable back to false before each loop.

Hope that helps; let me know if you have any questions!

Thanks,

Daniel
Technical Lead
interactivetools.com