Skip to content
Advertisement

PHP getting days of a month for a database

I’m making a calender planner with php/css/javascript/html.

I am trying to set each day of the month a class which is “day”. I tried using the following:

**PHP CODE**
error_reporting(E_ALL);
ini_set('display_errors', '1');

include "scripts/connect_to_mysql.php";
$jobNameValue ='';
$monthDays = '';
$monthname = '';
$days = '';
//getting values for job names

$sql_job_name = mysql_query("SELECT * FROM jobs");
$num_job_name = mysql_num_rows($sql_job_name);
if($num_job_name >0) {
    while ($row = mysql_fetch_array($sql_job_name)) {
        $job_name = $row["job_name"];
        $job_short_name = $row["job_short_name"];
        $jobNameValue .= '<option value="' . $job_short_name . '">' . $job_name . '</option>';
    }
} else {
    $jobNameValue .= '<option value="NULL">No job listed</option>';
}
//getting values for months days
$sql_month_days = mysql_query("SELECT * FROM months");
$num_month_days = mysql_num_rows($sql_month_days);
if($sql_month_days > 0) {
    while($row = mysql_fetch_array($sql_month_days)) {
        $month_id = $row["id"];
        $name = $row["name"]; 
        $num_days = $row["num_days"];if($num_days === "31") {
   for($i=1; $i <=31; $i++) { 
      $days .= '<span class="day">' . $i . '</span>';
   }
} else { 
    if($num_days === "30") {
       for($i=1; $i <=30; $i++) { 
          $days .= '<span class="day">' . $i . '</span>';
       }
     } else { 
         if($num_days === "29") {
           for($i=1; $i <=29; $i++) { 
             $days .= '<span class="day">' . $i . '</span>';
           }
         }
       }
  }
    $monthDays .= '<div id="monthContainer">
                        <span class="monthName">'. $name .'</span>
                        <div class="monthDaysContainter">
                            '. $days.'
                        </div>
                   </div>';
}

}


**HTML CODE**
<div id="container">
    <div ="newJob">
        <form action="<?php $_SERVER['PHP_SELF'] ?>" id="newJobForm">
            <label for="jobName">Job Name:</label>
                <select id="jobName">
                    <?php echo $jobNameValue; ?>
                </select>
            <input type="date">
            <input type="submit" id="submit">
        </form>
    </div>
    <div id="calander">
        <?php echo $monthDays; ?>
    </div>
</div>

It does not work correctly, it kept duplicating every month and I am not sure how to correctly right the php to achieve what was intended.

I am only a beginner at php so I am not good at the moment

Could anyone help me with this?

If there any more information you need, please don’t hesitate to ask.

Thank you in advance! Chris

Advertisement

Answer

Your problem is that you are creating the whole thing in the while loop, so the days that are appended from the last call are also appended to the next output.

$tmpArray = array();
if($sql_month_days > 0) {
    while($row = mysql_fetch_array($sql_month_days)) {
        array_push($tmpArray, $row);
    }
}
foreach($tmpArray as $k){
    $monthDays .= '<div id="monthContainer"><span class="monthName">'. $k['name'] .'</span><div class="monthDaysContainter">'. calNumDays($k['num_days']).'</div></div>';
} 

Update
Should you wish to keep what you already have

if($num_days === "31") {
    $days = '';
    for($i=1; $i <=31; $i++) {
        $days .= '<span class="day">' . $i . '</span>';
    }
} else { 
if($num_days === "30") {
    $days = '';
   for($i=1; $i <=30; $i++) { 
      $days .= '<span class="day">' . $i . '</span>';
   }
 } else { 
     if($num_days === "29") {
         $days = '';
       for($i=1; $i <=29; $i++) { 
         $days .= '<span class="day">' . $i . '</span>';
       }
     }
   }
}

You only really to need to reinitialize the $days before each for after the if

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement