Jump to content

PHP Array Issues


AJLX

Recommended Posts

Hello guys.

 

I have a DB that has a

 

Project Id, Start_date, End_date in it. I want to be able to put an array that gives me the date range, and then have the project id associtated with it. I currently get the date range, by using this function:

function getDateRange($startDate, $endDate, $format="Y-m-d")
{
    //Create output variable
    $datesArray = array();
    //Calculate number of days in the range
    $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1;
    if($days<0) { return false; }
    //Populate array of weekdays and counts
    for($day=0; $day<$total_days; $day++)
    {
        $datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));
    }
    //Return results array
    return $datesArray;
}

$dateRange = array();
$result = mysql_query("SELECT * FROM Project_details WHERE Project_id ='$value'");
while ($row = mysql_fetch_array($result))
{
$project_id = ($row['Project_id']);
$startDate = date("d-M-y", strtotime($row['Start_date'])); // get start date for current project and format it to look correct
$endDate   = date("d-M-y", strtotime($row['End_date'])); // get start date for current project and format it to look correct

$dateRange[] = getDateRange($startDate,$endDate);
}

The start

 

This gives me a  the date range. I would like to now add the project id to each entry so that the array would look something like this:

 

 

date => 17.02.14 project_id => 15

date => 18.02.14 project_id => 15

date => 19.02.14 project_id => 15

date => 20.02.14 project_id => 15

date => 21.02.14 project_id => 15

 

I have had a tweak, but the project number always seems to increment instead.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/286320-php-array-issues/
Share on other sites

Use your date value as the key value of your array and assign the project id as the value of each item in the array

 

$dates_array['17-02-14'] = 15

$dates_array['18-02-14'] = 15

 

Pass the id to the function and change the function from:

        $datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));

to:

$k = date($format, strtotime("{$startDate} + {$day} days"));

$dates_array[$k] = $proj_id;

 

where $proj_id is passed in as a param

Link to comment
https://forums.phpfreaks.com/topic/286320-php-array-issues/#findComment-1469567
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.