j8547
asked on
Change display of records in Table - PHP
This is the last question regarding this webpage. I have records from a database for the last 2 months being displayed on a webpage. It shows the older month on top. I tried to put tables side by side which worked but only 1 header would display for some reason. I am ok with the tables being display 1 on top of the other I would like the most recent month on top. Any time i try and change the display the code has an error and displays nothing. I am not sure if I have to break the data out when displaying. I have attached code and screenshot. I would like Jan on top and then December as there will be a lot of records so will probably have to scroll. Help is always appreciated.
Before.jpg
index.php
Before.jpg
index.php
ASKER
Will this not pull everything and sort by date. I just want to always only show current and previous month in 2 tables...as screenshot
The query against your DB stays exactly as you already have it. The code I posted just takes those returned records and re-arranges them so they're grouped by month. You end up with an array something like this:
$collection = array(
'17-12' = array(
13th December, 1598.3.2, Company 2, 1500
20th December, 1256.3.5, Company 1, 2500
),
'18-01' = array(
3rd January, 1254.0.2, Company 3, 20
3rd January, 4525.3.1, Company 4, 50
4th January, 6465.2.1, Company 1, 100
11th January, 7412.1.2, Company 4, 500
18th January, 1252.3.1, Company 2, 50
23rd January, 1522.3.2, Company 1, 100
)
);
Each key in the index gives you the month for your <H2> elements, and each key contains the records for that month, so you can create your separate tables.
ASKER
Sorry I am a bit confused. If I have 5 months will it not then pull all 5 months instead of the last 2. Does this code replace my code after the query. I have done that and no error, just nothing appears...
ASKER
Sorry i forgot my query is only pulling 2 months data....Jut not getting any error or output..
Post up your full code here and I'll take a look
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it seems to be working except for error Fatal error: Call to undefined function doHeaders(). The tables are printing out but not the headers....Thank you for getting me to this stage. appreciated
index.php
index.php
ASKER
Silly mistake. Got it to work. Just didnt have <?php
Think I have been looking at it too long!! Thank you
Think I have been looking at it too long!! Thank you
No worries. Glad you got it working :)
Probably the easiest way to do this is to build a new array to represent the data in a manner that suits your needs. I would loop through your records, creating a new key, based on the date, and inside that key, add the collection of records. You can then sort the new array by the key (the date) to get your months in the correct order. Another advantage of this is that you will automatically have an array that represents your data in a hierarchical way, so you don't need to check the dates to open and close the tables.
Have a look through this and see if it makes sense:
Open in new window