Jump to content

Need Help with extracting data from an Array


vincej

Recommended Posts

Hi - I have an array where one of the values delivers a string. Here is the output from a print_r($pickupdates):

 

Array
(
    [0] => Array
        (
            [location] => Clearwater
            [TheDates] => 1311746700,1321746641,1331668779
        )

    [1] => Array
        (
            [location] => Canmore
            [TheDates] => 1311746700,1321746641,1331746641
        )

    [2] => Array
        (
            [location] => Collingwood
            [TheDates] => 1311746700,1321746641,1331746641
        )

    [3] => Array
        (
            [location] => Varsity
            [TheDates] => 1321746641,1330746700,1331746700
        )

 

The challenge is to get the location and TheDates  into a nice HTML table which would look like this:

 

LOCATION          DATE 1            DATE 2              DATE 3

Clearwater         April 1              May 1                June 1 

Varsity               April 2              May 2                June 2 

Collingwood        April 3              May 3                June 3 


Canmore        April 4              May 4                June 4 

 

Ok - Now I am stuck, I need to be able to uniquely identify each date that is associated with the location such that I can run a foreach() on then so I can populate the table properly. I tried using list() and explode. List does not separate the dates and explode needs a string not an array.

 

I'm really stuck - a very big Heartfelt thanks to anyone who can steer me in the right direction.

 

MANY THANKS !

 

 

 

 

 

 

Link to comment
Share on other sites

$display = '<table>';
foreach ($arr as $item) {
    $theDates = explode(',' $item['TheDates']);
    $display .= '<tr><td>' . $item['location'] . '</td><td>' . $theDates[0] . '</td><td>' . $theDates[1] . '</td><td>' . $theDates[2] . '</td></tr>';
}

$display .= '</table>';

echo $display;

 

Should do what you want.

Link to comment
Share on other sites

Assuming those date values are timestamps then you would want to use the date() function when displaying them to get them in the format you described above.

date('F j', $theDates[0]);

 

However, I'm curious as to what the source of your array is. I'm assuming you are retrieving that data from somewhere (Database, XML feed, flat file, etc). There may be better ways to get the data in a better format to begin with - or even output the data at the time you are reading it rather than dumping into an array to have to process again.

Link to comment
Share on other sites

HI Premiso  -  You are MEGA !!!!!! If I could reach down the net and give you 2 big kisses I would !!!!

 

I have been battling with this for 3 days !!!!

 

THANK YOU THANK YOU !!

 

Psycho: The data comes off an MySql DB  and I converted the dates into strings with strftime() .. Thanks for your input ! I got this far by getting some help off the Mysql side of PHP freaks ... I can tell you this who thing has been a struggle !

 

 

regards Vincej  !

Canada

Link to comment
Share on other sites

Psycho: The data comes off an MySql DB  and I converted the dates into strings with strftime() .. Thanks for your input ! I got this far by getting some help off the Mysql side of PHP freaks ... I can tell you this who thing has been a struggle !

 

OK, then there is probably no need to jump through the hoops you are doing now to get the results you want. You can just as easily generate this output when you extract the results from the query. Creating an array is probably pointless. Can you show the code you have for running and processing the query?

Link to comment
Share on other sites

HI Pysho - Sure, Here is the code which creates the array $deliverydates

 

SELECT `location` , GROUP_CONCAT( pd.dates
ORDER BY pd.dates ) AS TheDates
FROM locations AS lo
LEFT JOIN pudates AS pd ON lo.locationid = pd.locationid
GROUP BY lo.location
LIMIT 0 , 30

 

 

The history behind this challenge starts in the SQL side. Credit to 'mikosiko' was the one person to understand my challenge and offer up the sql solution. The history is top of the SqL Froum : http://www.phpfreaks.com/forums/index.php?topic=355725.0

 

I'm still learning to become expert - so if there is a better way ESPECIALLY a easier way of doing it .. I'm all ears !

 

MANY thanks Vincej 

Link to comment
Share on other sites

 

@vincej, I think that is just fine for what you are doing, however, you can format the dates prior to it hitting php, so you do not have to worry about formatting them later:

 

SELECT `location` , GROUP_CONCAT( DATE_FORMAT(pd.dates, '%M %d'),
ORDER BY pd.dates ) AS TheDates
FROM locations AS lo
LEFT JOIN pudates AS pd ON lo.locationid = pd.locationid
GROUP BY lo.location
LIMIT 0 , 30

 

And then you don't have to worry about formatting it with php at all.

 

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

 

For the items you can use in place of %M and %d

Link to comment
Share on other sites

Thanks Premiso !! I'm learning alot !  I tried the date format mod and now I get:

 

import.php: Missing parameter: import_typeDocumentation

import.php: Missing parameter: formatDocumentation

 

I've checked the php.ini files as recommended by the documentation - but to no avail. I have had this problem in the past with own queries - usually just a refresh fixes it - but not this time.

 

Secondly, if I may prey on your generosity, I need to be able to update the DATES value from my html form.  My POST is sending data up, but so far I have not managed to overcome the challenge that the dates are stored as timestamps yet they are presented as Human readabe strings. My user will alter the value in the table, then they need updating back into the DB as Unix.

 

Any Help you can offer is HUGELY  appreciated !

 

Many thanks !!

Link to comment
Share on other sites

Hi - Ok I got rid of the "missing parameter" error, however, if I run your modified code as is I get a syntax error:

 

ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY pd.dates ) AS TheDates FROM locations AS lo LEFT JOIN pudates AS pd' at line 2

 

I'll try to refine things further,  Many thanks for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.