Array results are not displayed incorrectly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grace01
    New Member
    • Sep 2007
    • 6

    Array results are not displayed incorrectly

    Hello,

    My php program cannot display result correctly. After run array_push($y_o ptions,$tmp_yr) , $y_options doesn't show 2005, 2006, 2007, 2008, 2009. $y_options only show 2009, 2009, 2009, 2009, 2009.

    Same for $m_options. It only show December rather than January to December. My source code is as follow:

    [code=php]

    $array_month = array("1"=>"Jan uary","2"=>"Feb uary","3"=>"Mar ch","4"=>"April ","5"=>"May","6 "=>"June"," 7" =>"July","8"=>" August","9"=>"S eptember","10"= >"October","11" =>"November","1 2"=>"December") ;
    $array_year = array("2005","2 006","2007","20 08","2009");
    $m_options = array();
    while (list($indx,$mt h) = each ($array_month)) {
    $tmp_mth->m_title = $mth;
    $tmp_mth->m_value = "?month=".$indx ."&year=".$year ;
    if ($indx == $month) {
    $tmp_mth->selected = "selected";
    }
    array_push($m_o ptions,$tmp_mth );
    $tmp_mth->selected = "";
    }
    $y_options = array();
    foreach ($array_year as $yr) {
    $tmp_yr->y_title = $yr;
    $tmp_yr->y_value = "?month=".$mont h."&year=".$y r;
    if ($yr == $year) {
    $tmp_yr->selected = "selected";
    }
    array_push($y_o ptions,$tmp_yr) ;
    $tmp_yr->selected = "";
    print_r ($tmp_yr->y_title);
    print_r ($y_options);
    }

    [/code]

    The url is http://ritc.view2offer.com.sg/course_calendar.php

    I am not sure where is the possible problem. Would appreciate if anybody can help.
    Last edited by Atli; Sep 7 '07, 03:08 PM. Reason: Changed [code] tags to [code=php] tags.
  • Purple
    Recognized Expert Contributor
    • May 2007
    • 404

    #2
    Hi,

    I have run your script with the following output:

    Code:
    y_options for year 2005
    Array ( [0] => stdClass Object ( [y_title] => 2005 [y_value] => ?month=August&year=2005 [selected] => ) ) 
    y_options for year 2006
    Array ( [0] => stdClass Object ( [y_title] => 2006 [y_value] => ?month=August&year=2006 [selected] => ) [1] => stdClass Object ( [y_title] => 2006 [y_value] => ?month=August&year=2006 [selected] => ) ) 
    y_options for year 2007
    Array ( [0] => stdClass Object ( [y_title] => 2007 [y_value] => ?month=August&year=2007 [selected] => ) [1] => stdClass Object ( [y_title] => 2007 [y_value] => ?month=August&year=2007 [selected] => ) [2] => stdClass Object ( [y_title] => 2007 [y_value] => ?month=August&year=2007 [selected] => ) ) 
    y_options for year 2008
    Array ( [0] => stdClass Object ( [y_title] => 2008 [y_value] => ?month=August&year=2008 [selected] => ) [1] => stdClass Object ( [y_title] => 2008 [y_value] => ?month=August&year=2008 [selected] => ) [2] => stdClass Object ( [y_title] => 2008 [y_value] => ?month=August&year=2008 [selected] => ) [3] => stdClass Object ( [y_title] => 2008 [y_value] => ?month=August&year=2008 [selected] => ) ) 
    y_options for year 2009
    Array ( [0] => stdClass Object ( [y_title] => 2009 [y_value] => ?month=August&year=2009 [selected] => ) [1] => stdClass Object ( [y_title] => 2009 [y_value] => ?month=August&year=2009 [selected] => ) [2] => stdClass Object ( [y_title] => 2009 [y_value] => ?month=August&year=2009 [selected] => ) [3] => stdClass Object ( [y_title] => 2009 [y_value] => ?month=August&year=2009 [selected] => ) [4] => stdClass Object ( [y_title] => 2009 [y_value] => ?month=August&year=2009 [selected] => ) )
    to make it run I set:

    [PHP]$month = "August";
    $year = "2006";

    // with the output generated using :

    echo "<br>y_opti ons for year ".$yr."<br> ";
    print_r ($y_options);

    [/PHP]

    can you give more details on what the problem is ?

    Regards Purple

    Comment

    • grace01
      New Member
      • Sep 2007
      • 6

      #3
      Thanks for your reply. Can you click http://ritc.view2offer .com.sg/course_calendar .php?

      When you click drop down menu Month and Year, it always show December and 2009. The correct result of drop down menu should be January - December, 2005 to 2009.

      Regards
      Huifen

      Comment

      • Weisbartb
        New Member
        • Aug 2007
        • 36

        #4
        Originally posted by grace01
        Hello,
        [code=php]

        $array_month =
        while (list($indx,$mt h) = each ($array_month)) {
        $tmp_mth->m_title = $mth;
        $tmp_mth->m_value = "?month=".$indx ."&year=".$year ;
        if ($indx == $month) {
        $tmp_mth->selected = "selected";
        }
        array_push($m_o ptions,$tmp_mth );
        $tmp_mth->selected = "";
        }
        foreach ($array_year as $yr) {
        $tmp_yr->y_title = $yr;
        $tmp_yr->y_value = "?month=".$mont h."&year=".$y r;
        if ($yr == $year) {
        $tmp_yr->selected = "selected";
        }
        array_push($y_o ptions,$tmp_yr) ;
        $tmp_yr->selected = "";
        print_r ($tmp_yr->y_title);
        print_r ($y_options);
        }

        [/code]
        Your over writing your variables in your code. use something like this

        [php]
        $tmp_mth->m_title[] = $mth;
        [/php]
        Instead. That will place them in an array for you that will auto increment.

        Comment

        Working...