Jump to content

If...Elseif...Else statement with a loop


cjc1867

Recommended Posts

Hi

My PHP skills aren't that good but I know what I want to do as I have used other programming languages before.

I run a WordPress website about a Church and its Cemetery. I have been researching the graves for the last 2 years and have added each grave to the website via a Custom Post Type with the buried occupants.

In WordPress I have used a plugin called Advanced Custom Fields and have set each grave to allow at least 5 people buried there.

So the following code below is used to check if the fields have any value which would represent each person in that grave and add it to the page. I don't know how to add it as a database table so this is the easiest way I could think of.

echo '<div id="section-grave">';
echo "<h3>Who's buried in this Grave?</h3>";
//echo '<p>Listed below, are the people buried in this grave.</p>';
if(get_field('is_burial_date_known')) {
    // start For Loop
    for ($x = 1; $x <= 5; $x += 1) {
        $name_of_deceased = 'name_of_deceased_' . $x;
        $burial_date = 'burial_date_' . $x;
        $clergy_name = 'clergy_name_' . $x;
        if(get_field($name_of_deceased)) {
           echo '<p>' . $x . ') ' . get_field($name_of_deceased) . '</p>'; 
        }
        if(get_field($burial_date)) {
           echo '<p>Burial Date: ' . get_field($burial_date) . '</p>'; 
        }
        if(get_field($clergy_name)) {
           echo '<p>Buried By: ' . get_field($clergy_name) . '</p>';
           echo '<hr>';
        }
    } // end For Loop
} else {
     echo '<p>Nobody buried here.</p>';
}
echo '</div>';

I want to add a paragraph above which I have commented out to show how many people are buried in that grave. So if it was only one person then it should say 1 person as oppose to people. So I need a way of counting the names but I can't see a way how to do it.

It will probably based on an IF...Elseif...Else statement and which ever condition is right it just displays the correct line or paragraph. I need a way of counting thetotal number of people per grave but outputting the total relies on the loop which is after the paragraph or line.

I hope I have explained it properly so can anyone see how I can do it please?

Colin

Link to comment
Share on other sites

Hi

I need to know the amount of people so that the text says either 1 person or 2 or more people buried and it should come before I get to the loop.

At the moment I have just added another field to add the total and then used an If Elseif statement but I can't see how I can count the number instead based on the loop.

$people_buried = get_field('how_many_buried_in_this_grave');
if($people_buried) {
   if($people_buried == 1) {
      echo '<p>Listed below, is 1 person buried in this grave.</p>'; 
   } else if($people_buried == $people_buried) {
      echo '<p>Listed below, are ' . $people_buried . ' people buried in this grave.</p>'; 
   } else {
       echo 'Nobody buried here';
   }
}

I suppose I will have to leave it like that as at the most is going to be 4 or 5.

Cheers

Colin

Link to comment
Share on other sites

If you didn't need an actual number then this is pretty easy: if name_of_deceased_2 has a value then you have at least two people, else if name_of_deceased_1 has a value then you have one person, else there is nobody in the grave. Assuming you fill those fields in order.

The simplest way to get a count would be to do another for loop, except this one counts when the name_of_deceased_N field is filled instead of outputting something. A bit wasteful, having to get those fields multiple times.

The clean way is to get all those fields into one array variable, such as one that looks like

array(
	array(
		"name_of_deceased" => name,
		"burial_date" => date,
		"clergy_name" => name
	),
	...
)

One for loop to get all the data into a better format than you have now, adding people to the list if the name is set, then you can get the count() of people easily and foreach on it to display.

The ideal way is to not need to have 15 fields to cover 5 people in a grave. Are there ever graves with more than five people? What will you do then - add more fields? Having to add them like that every time is a sign of bad database design.
What should be set up is a single table that has four fields: a grave identifier, the person's name, the burial date, and the clergyman's name. Then you get all burials for the grave, there's no limit on how many you can cover, and getting counts is easy.

Link to comment
Share on other sites

That's my problem, I don't understand how databases work in WordPress as it just looks like one table for all the posts and pages. Also I don't know how to create a table or series of in MySQL and pull values from it using PHP.

I did databases at Uni but that's 20 years ago and I have forgotten most of it as I haven't done any since.

The burial register has a set number of fields and I have been trying to put a database together in Access just to see how it works and then convert it using MySQL and PHP but haven't got very far.

When I first started this project I just based each Custom Post Type on each Grave and then added the people who are buried in that grave but I can see now that I am missing information as I used Taxonomies to select the Vicar. So if I have several burials in a grave then I can only select the vicar once even if he buried all of them. I should have based it on the person buried and not the grave.

I will look into creating databases in WordPress and yes you are right I could have retrieved the count quite easily and other information. 

Thanks for your comments.

Colin

Link to comment
Share on other sites

WordPress isn't what we in the software development industry would call "good" or "well-designed", but at the very least it should be capable of dealing with custom database tables.

Worst case you can use another for loop: it's fast and easy, even if inefficient. Any additional amount of time you want to invest in this is up to you.

Link to comment
Share on other sites

Wordpress can handle custom tables, but it doesn't like it. If you're using ACF you can create arrays of data - honestly, even when I was dealing with WP daily I never used ACF, but I knew a lot of people that did. I think it's called a 'repeater' - check this: https://www.advancedcustomfields.com/resources/code-examples/. Doing this will negate the need to append a number to the variable name, and allows you to use count() to do what you're looking to do. It also allows you to not loop exactly five times regardless the amount of data present, and to track a grave with more than 5 bodies.

Link to comment
Share on other sites

Hi

The 'Repeater' would have been ideal but that comes with the Pro version and therefore you have to buy it and I can't afford it at the moment.

Another Plugin I found is called 'Toolset' and that is ideal as it has a tutorial and it looks like it will do the job but when I tried to download it I couldn't because you have to buy it and there isn't a free version.

At the moment what I have works but not ideal so I will have to look for another solution.

Thanks for replying.

Colin

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.