0

Hi I am using the Calendar Code u can find down in the code. Now I am using Sql to get some days I want to mark in the calendar. In the foreach loop u can see me using the array $tage.

When I print_r($tage) it gives me following Information: Array ( [0] => 9 [1] => 8 [2] => 8 [3] => 8 [4] => 8 [5] => 11 ) Maybe there is a Problem with the returning values, but using only the unique values was not possible either. Fyi the only highlight I always get is the last value 11. I think the whole issue comes down to the combination of while foreach and elseif. I can't find any Solution to this. Maybe someone figures it out. Thanks :)

<?php
error_reporting();
ini_set('display_errors', 1);
setlocale(LC_TIME, "de_DE");    

class Calendar{
    private $month;
    private $year;
    private $daysofweek;
    private $numdays;
    private $date_info;
    private $day_of_week;
    private $tnr;


    public function __construct($month,$year,$days_of_week =array('So','Mo','Di','Mi','Do','Fr','Sa')){

        $this->month =$month;
        $this->year =$year;
        $this->days_of_week =$days_of_week;
        $this->num_days =cal_days_in_month(CAL_GREGORIAN, $this->month, $this->year);
        $this->date_info =getdate(strtotime('first day of', mktime(0,0,0,$this->month,1,$this->year)));
        $this->day_of_week =$this->date_info['wday'];
        $this->monat = date("n", mktime(0, 0, 0, $this->month, 1, $this->year));

    }

    public function show($tnr) {
        $monate = array(1=>"Januar",
                        2=>"Februar",
                        3=>"M&auml;rz",
                        4=>"April",
                        5=>"Mai",
                        6=>"Juni",
                        7=>"Juli",
                        8=>"August",
                        9=>"September",
                        10=>"Oktober",
                        11=>"November",
                        12=>"Dezember");

        include '../include/myt.php';
        // WERT 0 entfernen

        // GRÖße des ARRAY
        $size =sizeof($tnr);

        $now_mon= $this->monat;
        foreach($tnr as $tnrs){
            $sql= "SELECT * FROM termin WHERE nr = '$tnrs' AND monat ='$now_mon' ";
            $query = mysqli_query($conn, $sql); 
            $row = mysqli_fetch_array($query);
            $tagdata = $row['tag'];
            if($tagdata == ""){
                "";
            } else {
                $tage[] = $tagdata;
            }

        }   

        $output= '<table id="table'. $this->monat . '" class="table table-striped">';
        $output .= '<thead id="head'. $this->monat . '" style="text-align:center">'. $monate[$this->monat] . ' ' . $this->year . '</thead>';
        $output .= '<tr>';

        foreach( $this->days_of_week as $day)
        {
            $output .= '<th class="header center">' . $day . '</th>';
        }

        $output .= '</tr><tr>';

        if($this->day_of_week > 0){
            $output .= '<td colspan="' . $this->day_of_week . '"></td>';
        }
        $current_day =1;

        while ( $current_day <= $this->num_days){
            if($this->day_of_week ==7){
                $this ->day_of_week =0;
                $output .= '</tr></tr>';
            }
            ///PROBLEM 


            print_r($tage); 

            foreach($tage as $tag){

                if($current_day == $tag){
                    $current='style ="background: black;"';
                } else {
                    $current= '';
                }
            } 

///PROBLEM
            $output .='<td class="day"'.$current.'>' . $current_day . '</td>';
            $current_day++;
            $this->day_of_week++;
        }

        if($this->day_of_week != 7){
            $remaining_days = 7 - $this->day_of_week;
            $output .= '<td colspan="' . $remaining_days . '"></td>';
        }

        $output .= '</tr>';
        $output .= '</table>';
        echo $output;
    }
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Steakic
  • 1
  • 2
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Mar 27 '17 at 08:38
  • thank you I will have a look at this :) When I get behind the issue – Steakic Mar 27 '17 at 08:43

1 Answers1

0

Change your foreach loop as below:

foreach($tnr as $tnrs) {
    $sql= "SELECT * FROM termin WHERE nr = '$tnrs' AND monat ='$now_mon' ";
    $query = mysqli_query($conn, $sql);
    $row = mysqli_fetch_array($query);
    $tagdata = $row['tag'];
    if(!empty($tagdata)) {
        $tage[] = $tagdata;
    }
}

Can you please check at line $output .='<td class="day"'.$current.'>' . $current_day . '</td>';

It should be $output .='<td class="day"'.$current.'">' . $current_day . '</td>';, You are forgot to add ".

May be it is help you.

Maulik Savaliya
  • 1,262
  • 7
  • 17