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.
index.php
j8547Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

0
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.