CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Creating variables dynamically as output of loop

Print topic Send  topic

Author Message
rado

Posts: 221
Posted: 07/08/2009, 5:07 PM

HI everybody,

I have need to extract the months from date range and I found on the web very nice function that is doing almost what I'm looking for. I think that this function could help someone else so here is how looks like:
==============================================================================
function get_months($startstring, $endstring)
{
$time1 = strtotime($startstring);//absolute date comparison needs to be done here, because PHP doesn't do date comparisons
$time2 = strtotime($endstring);
$my1 = date('mY', $time1); //need these to compare dates at 'month' granularity
$my2 = date('mY', $time2);
$year1 = date('Y', $time1);
$year2 = date('Y', $time2);
$years = range($year1, $year2);

foreach($years as $year)
{
$months[$year] = array();
while($time1 < $time2)
{
if(date('Y',$time1) == $year)
{
$months[$year][] = date('m', $time1);
$time1 = strtotime(date('Y-m-d', $time1).' +1 month');
}
else
{
break;
}
}
continue;
}
return $months;
}

$montharr = get_months('2003-01-04', '2005-09-18');
foreach(array_keys($montharr) as $year)
{
foreach($montharr[$year] as $month)
{
print "{$year}-{$month}\n";
}
}
=============================================================================

My question here is how I can extend this script and instead of printing the result on screen I would like to assign the value of each month to separate variable. (Like mounth_1; mounth_2; month_3 .....)

And one more thing. Is there any PHP command that could give me the name of the month instead number of month.

Thanks a lot for any king of help.

Rado
View profile  Send private message
rado

Posts: 221
Posted: 07/08/2009, 6:11 PM

It looks like whole forum members are on vacation :)
View profile  Send private message
damian

Posts: 838
Posted: 07/08/2009, 6:37 PM

you waited 65 minutes before prompting?

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
rado

Posts: 221
Posted: 07/08/2009, 7:19 PM

Thanks Damian, even criticism is better then nothing :)

I tried with this way but it doesn't return anything:

===================================================================
$montharr = get_months('2003-01-04', '2005-09-18');
foreach(array_keys($montharr) as $year)
{
foreach($montharr[$year] as $month)
{
$date = "{$year}-{$month}";
foreach($date as $key => $value){
$_SESSION[$key] = $value;
print $_SESSION[$key];
}
}
}
==========================================================================

Rado
View profile  Send private message
damian

Posts: 838
Posted: 07/08/2009, 9:43 PM

im sorry rado - but this is out of my reach!

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
rado

Posts: 221
Posted: 07/09/2009, 4:09 AM

Thanks damian in any way.

Rado
View profile  Send private message
feha


Posts: 712
Posted: 07/09/2009, 12:17 PM

Hi
try print_r( $_SESSION[$key]);

as it is an array
:-)
And I really don't understand why you want ot add it that way into an session as array ...

This way you are creating so many session variables as $values ...

remember not print but print_r( XXX );
if XXX is an array

Also check
http://www.php.net/manual/en/ref.calendar.php

and

http://se2.php.net/manual/en/function.date.php

Hope this helps :-)
_________________
Regards
feha

www.vision.to
feedpixel.com
View profile  Send private message
rado

Posts: 221
Posted: 07/09/2009, 1:25 PM

Thanks feha,
My original problem that I'm trying to solve is this:
http://forums.yessoftware.com/posts.php?post_id=106985

Where I have to capture all moths from date range, assign them to session variables which I will use to assign the value for my labels.

Rado
View profile  Send private message
jjrjr1


Posts: 942
Posted: 07/10/2009, 7:20 AM

Geez

This seems so convoluted.

I think I understand What you are trying to do here.

Did you resolve the first requirement of inserting records for each month??

Why are you sending the months in session variables??

Would it maybe be easier to just set the start date and end date as session variables then run the month determination routine on those session variables building your montg array in each pagr that needs it???

Just some thoughts

_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message
jjrjr1


Posts: 942
Posted: 07/10/2009, 3:31 PM

Hi

I found this little code snippet for you.

  
function get_months($date1, $date2) {   
   $time1  = strtotime($date1);   
   $time2  = strtotime($date2);   
   $my     = date('mY', $time2);   
  
   $months = array(date('F', $time1));   
  
   while($time1 < $time2) {   
      $time1 = strtotime(date('Y-m-d', $time1).' +1 month');   
      if(date('mY', $time1) != $my && ($time1 < $time2))   
         $months[] = date('F', $time1);   
   }   
  
   $months[] = date('F', $time2);   
   return $months;   
}   

So why dont you make this function a common function and put your start and stop dates into session values. Then just run this on any page that needs the $months[] array.

Example:
In the date setting part of your app
  
  
CCSetSession("StartDate","2009-12-12");  //Or a string is it is set dynamically  
CCSetSession("EndDate",$YourEndDateString);  
  

Then wherever you need the month array simply call like this:

  
  
$months=get_months(CCGetSession("StartDate,""),CCGetSession("EndDate",""));  
  

Now you have only used 2 session variables and you can get your list of months any time you want.

Just a thought.

Let me know if this works since I did not test that get_months() function


_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message
rado

Posts: 221
Posted: 07/10/2009, 5:50 PM

John, Thank You very much. I just finished first (for me most important) peace) few minutes before you post. This is what I end up with:

Originally the date range set in script is just example date range, but I need maximum 12 moths. So I created controls named like month_1, month_2......month_12. Then I before show of form I run this code:
function get_months($startstring, $endstring) {  
         $time1 = strtotime($startstring);  
         $time2 = strtotime($endstring);  
         $my1 = date('mY', $time1);   
        $my2 = date('mY', $time2);  
         $year1 = date('Y', $time1);  
         $year2 = date('Y', $time2);  
         $years = range($year1, $year2);  
  
          foreach($years as $year) {   
                $months[$year] = array();  
                while($time1 < $time2) {  
                         if(date('Y',$time1) == $year) {  
                                 $months[$year][] = date('M', $time1);  
                                 $time1 = strtotime(date('Y-m-d', $time1).' +1 month');  
                         } else {  
                                 break;   
                        }   
                }   
                continue;  
         }  
         return $months;  
 }    
  
$montharr = get_months('2005-03-04', '2006-02-18'); //I will set here real date range  
$count = 0;   
foreach( $montharr as $year => $months ) {  
         foreach( $months as $thisMonth ) {  
                 $count = $count + 1;  
 				$control_name = 'month_' . $count;  
 				$control_value = $year . '-' . $thisMonth;  
 				$Container->$control_name->SetValue($control_value);   
        }   
}

End everything works just fine. I have to add more stuff (like price per moth etc.) HOWEVER, one thing that I don't know how to do it is how I can insert all 12 records (each month is one record) in same time. The editable gris is suitable for this but I can't manage control name since the editable grid has practically just one control name in multyple instances.

Since you really offer me concrete solution, what do you think about what I have done and how to approach to second problem (inserting multiple records in one shot).

Thanks again for help and sorry that I didn't reply before (I couldn't since I was in precess to getting the solution that I post here)
View profile  Send private message
rado

Posts: 221
Posted: 07/10/2009, 6:06 PM

Additionally in the previous attempts I used session variable just to verify that whole script is working fine. The end result is populating controls with values.

Again my original task that I wont to achive is here:

http://forums.yessoftware.com/posts.php?post_id=106985.

Thanks again John for your help.

Rado

View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.