Exploring our generalized table rendering function

Description

Transcript

And so our next step is running output_as_table(), and we're supplying the items. And then we're just going to print out whatever is returned from output_as_table(). Now, again, in the previous version of this code the outputting as a table and the parsing of the data were all part of one function, but this time it's split up into two parts.

So let's go ahead and take a look at the output_as_table() function. I'm going to scroll up a little bit here, and here's our function. Output_as_table() takes one parameter which is an array of a certain structure that gets returned from our parse_data_file() function.

Our first step is dealing with what happens if there's actually no data that's being passed through items. So we're using a function called count() in order to count the number of items in the array. If it equals zero, it means there's nothing in there, and we're going to return our placeholder text that says "Hey, we don't have any data to display.

" Notice that we're doing this first above the other code which is a little different in the structure of our last code where we did it at the end, only if we didn't have data to return. By doing this at the top of a function, we can return a value and stop the execution of all the other code in this function. So notice that we have return here, but then after all of this code here we have another return.

Doing something like this is totally fine. It's a great way to check for certain conditions before you run the bulk of your code. So if the number of items is bigger than zero, which it will be in our case, what we're going to do is generate our table.

In the first part, we're generating a header for the table, and the second part we're generating some table content, and then finally we're pulling it all together in a table. Now, again, remember that we don't know exactly what the structure of this data is. We don't know if it has two columns or ten columns or a hundred columns and we don't know what the names of those columns are.

So what we're going to do is take one item from our array of items and we're going to pull out the keys. Now, the keys are the actual names of the columns. So when we run for each, the key here is going to be the name of the column and the value is going to be the value of that column.

Now, we don't need this value, but we have to use this structure in order to pull out the key. Now, notice that we're getting just the first item inside of our items array. Now, if you remember, the items array contains each record and so we're pointing to a specific record here by using the key of zero.

And so this value here is an associative array that includes all of the keys that correspond to the labels or column names of our data. So for each item here, we're adding to a string called table_heading by using our shorthand concatenation operator here, and we're just taking the heading text and we're wrapping a th or table header tag around it. The first time this foreach loop runs the heading will be name, the next time it runs it will be birth year, and so on until we have four table headings inside of our table heading variable.

Once we have those, we need to wrap a table row tag around it. So here we have that being wrapped around the table heading string. And we're assigning that to our the variable here.

In this next part, we're getting the content of the table which is the actual data. Now, on ...

When you have an active membership, you will be able to see your progress here.

Skill focus: ProgrammingSkill level: Beginner - Advanced

In this collection, we walk you through everything you need to know to build rich PHP applications and work with PHP code in Drupal. These videos are designed to bridge the gap between Drupal site builders and programmers. We guide you gently through the basics of PHP, and work our way into building more and more complicated scripts. In our final example we build a custom CMS, which will help you understand the architecture of Drupal while flexing your new PHP skills in a practical way.

To follow along with these videos, you will need a web environment set up on your computer. Follow the videos in Setting Up a Web Environment With Drupal to get an environment set up on Windows, OSX (Mac) or Linux.

Some of the key points we'll be covering include:

How to work with variables and functions

How to use logic in PHP like if…then statements and looping

How to build forms in PHP

How to store persistent data in sessions

How to work with MySQL

How to build a simple CMS from scratch

Who this collection is for

This collection is for those that want to learn PHP programming. You may want to learn PHP to work with Drupal in particular, but what you learn will be useful for any project you work on requiring a background in PHP.

It looks like there might be an issue playing videos in this browser. We're working on better cross-brower experience, but in the meantime please try the latest Chrome or Firefox browsers. Or, you may be able to watch the video directly without progress tracking or transcript: