'while'循环中的数组

时间:2017-06-14 15:24:08

标签: php html arrays

我被困了

我会解释这个概念;我检索了一段时间后修改的查询的数据(日期),在我正确修改日期后,我启动了一个开关。 在这个开关中,我想将日期添加到一个表格中,该表格显示“在2017年,”+日期,但在此集合中我想插入我的日期,但它不起作用。 如果我初始化一个变量示例:$ x =“15:06”;有用。救命啊!

以下是相应的代码:$ annee_selectionne框不起作用

 $req_public_holiday = $bdd - > query('SELECT * FROM public_holiday');


 while ($donnees_public_holiday = $req_public_holiday - > fetch()) {

   $format_date = date('Y.m.d', strtotime($donnees_public_holiday['date']));
   $var1 = explode('.', $format_date);

   $year_selectionne = $var1[0];
   $month_selectionne = $var1[1];
   $day_selectionne = $var1[2];
   $date_x = $day_selectionne.
   ":".$month_selectionne;


   switch ($year_selectionne) {
     case 2013:
       $free_day = array('01:01', '01:04', '01:05', '08:05', '09:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2014:
       $free_day = array('01:01', '21:04', '01:05', '08:05', '29:05', '09:06', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2015:
       $free_day = array('01:01', '06:04', '01:05', '08:05', '14:05', '25:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2016:
       $free_day = array('01:01', '28:03', '01:05', '05:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
       problem-- >
     case $year_selectionne:
       $free_day = array($day_selectionne.':'.$month_selectionne);
       break;
     default:
       $free_day = array('01:01', '01:05', '08:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
   }

 }

2 个答案:

答案 0 :(得分:0)

我认为更好的方法是为月份创建一个变量:要在数组中添加的日期值,而不是推送到它(PHP array_push())

只需创建一个类型为array()的$ month_date变量,然后使用该方法将其推入。 您可能希望进行if..else检查以查看该变量是否已存在。

答案 1 :(得分:0)

我更改了您的代码并使用了函数array_push()

 $req_public_holiday = $bdd - > query('SELECT * FROM public_holiday');


 while ($donnees_public_holiday = $req_public_holiday - > fetch()) {

   $format_date = date('Y.m.d', strtotime($donnees_public_holiday['date']));
   $var1 = explode('.', $format_date);

   $year_selectionne = $var1[0];
   $month_selectionne = $var1[1];
   $day_selectionne = $var1[2];
   $date_x = $day_selectionne.
   ":".$month_selectionne;


   switch ($year_selectionne) {
     case 2013:
       $free_day = array('01:01', '01:04', '01:05', '08:05', '09:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2014:
       $free_day = array('01:01', '21:04', '01:05', '08:05', '29:05', '09:06', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2015:
       $free_day = array('01:01', '06:04', '01:05', '08:05', '14:05', '25:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
     case 2016:
       $free_day = array('01:01', '28:03', '01:05', '05:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;

     case date('Y'):
       $free_day = array_push($free_day, $day_selectionne.':'.$month_selectionne);
       break;
     default:

       $free_day = array('01:01', '01:05', '08:05', '14:07', '15:08', '01:11', '11:11', '25:12');
       break;
   }

 }

我希望能帮助你。

相关问题