douya7121 2012-03-05 19:21
浏览 39
已采纳

PHP连接零到期POST值

I need to add a zero to the date value that is posted as the MySQL date value will be used as a directory path to show added photos. Currently the directory is saved as 2012-2-5 and the MySQL entry is 2012-02-05.

I have tried the following, however it doesn't seem to be working:

$dates = array($_POST['photos_year'], $_POST['photos_month'], $_POST['photos_day']);
    foreach($dates as $date) {
    if (strlen($date) == 1) {
        $date = '0' . $date;
    }
    }
$photos->date = $dates[0] . "-" . $dates[1] . "-" . $dates[2];

I am using a for loop to create the date form, it might be easier for me to edit this however I haven't had any success:

<select name="photos_month"  id="photos_month" tabindex="3">
    <option value ="">- Month -</option>
    <?php 
    for($date=01;$date<=12;$date++) {
        if (isset($photos) && $date==$month) {
            echo "<option value='".$date."' selected='selected'>".$date."</option>";
        } else {
            echo "<option value='".$date."'>".$date."</option>";
        }
    }
    ?>
</select>
  • 写回答

5条回答 默认 最新

  • duandi6531 2012-03-05 19:27
    关注

    You need to update the foreach to be

    foreach ($dates as &$date)
    

    Currently, $date is a new symbol and will not overwrite the value. That should fix it, but it may be nice to have the correct strings in the options too. You can use str_pad to do that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?