PHP - print first 4 values of array for Column headings

I have the entire table printing out with column names and data. It displays 8 columns but i just want it to display the first 4 columns names/headers only and want the last 4 headers to show the next 4 dates from today (today, tomorrow....) .

For the data I just want o show the first 4 columns of data and logic/formula for the last 4.

Can you help me to print out only 4 header from array and static for the last 4 headers?

I have uploaded the code.
Who is Participating?
Jan LouwerensSoftware EngineerCommented:
This will print the first 4 columns.
$index = 0;
foreach($data as $column => $data) {
    if ($index++ < 4) {
        echo "<th>{$column}</th>";

Open in new window

Then, afterwards, just manually add the 4 static headers you want.
echo "<th>Column 5</th>";
echo "<th>Column 6</th>";
echo "<th>Column 7</th>";
echo "<th>Column 8</th>";

Open in new window

Julian HansenCommented:
I have a question
Why not just amend your SQL query to only return the columns you want - why get all the columns and then add code to only use 4 of them?

Another option is, in your code you are already looping over the rows to build the $collection array - why not simply strip out the columns there?

If there is a valid reason for the above then take a look at the array_splice() function

foreach(array_splice($yourarray,4) as $item) {

Open in new window

j8547Author Commented:
I am pulling all the columns from the array as i want to use the dates for figure out that status for the last 4 columns. I will look into the array_splice though. Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.