From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Display a repeating value only once

Display a repeating value only once

From the course: PHP Tips, Tricks, and Techniques

Display a repeating value only once

- [Instructor] Hi I'm David Powers, and welcome to this week's edition of PHP Tips and Tricks. Designed to help you become a smarter, more productive PHP developer. When displaying the results of a database query, presentation is all important. In this example having the Make repeated in each row like this impedes readability. The table would be much easier to read if each Make were displayed just once. This week's tip shows how to suppress the duplicate values. It's a very simple trick, so let's get to work. I'm going to switch to my editing program, and the database result is displayed here in this foreach loop on lines 25-31. We're just displaying three values from the database result, Make, pricef and description. And it's this make that we need to suppress if it's going to be repeated. To prevent a value from being repeated, all that's necessary is to compare it with the value from the previous row. But before the loop begins, there is no previous value. So we need to initialize…

Contents