Skip to content
Advertisement

PHP array break if the column exceeds specific number [closed]

Hello i tried modifying a code left by the dev i hired in 2018 but not being a coder is really taking a toll on me. What do i want to do here is have this code create another table if the first table loop have more than 3 cities and repeat the process to create tables instead of adding new columns in the existing table, the tables by default have raows equal to days in month but the columns are fethced from database as locations. here is the code.

JavaScript

I tried if else statements and continue break but to no avail i don’t even know what i am doing at this point. What it displays is below and i want it to break 3 cities in each loop image

Advertisement

Answer

Your question is a little vague. But if I understand you correctly, you want to limit the number of sales per city to the first three columns (city1, city2, city3) for every date. While keeping all the city headers in tact.

foreach might not be the best construct to do this, but seeing as you are out of your depth as it is, I will suggest something close to what you already have.

Try replacing the second occurrence of foreach ($locations as $location) { with:

JavaScript

This assumes that $locations is a simple array with a sequential numeric index. If this is not exactly what you’re trying to do, then please elaborate so I can provide a more accurate answer.

UPDATE

See this eval for a live demo of one possible approach. I used heredoc syntax for the building the HTML because it’s a little easier to read for the purpose of this example. Here’s the code. You can set $numColumns to control the number of table columns.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement