1

我在 while 循环中有一组数据。当我在 while 循环数据中回显它们时,类似于此..

if ( !empty($rows) ) {  
    // bind variables to prepared statement
    mysqli_stmt_bind_result($stmt, $userId, $days, $opentime, $closetime, $closestate);

    while (mysqli_stmt_fetch ($stmt)) {     

        echo '<pre>', print_r("$days - $opentime - $closetime - $closestate").'</pre>';

    }
}

数据 :

Monday - 06.00 PM - 06.00 PM - 0 
Tuesday - 05.00 PM - 06.00 PM - 0
Wednesday - 05.00 PM - 05.00 PM - 0  
Thursday - 05.00 PM - 05.00 PM - 0   
Friday - 03.00 PM - 04.00 PM - 0  
Saturday - 03.00 PM - 03.00 PM - 0   
Sunday - 03.00 PM - 02.00 PM - 0

在while循环中,我想分离这样的数据..

$monOpen = // monday open time
$monClose = // monday close time
$monNo = // closed day 

$tueOpen = // Tuesday open time
$tueClose = // Tuesday close time
$tueNo = // closed day 

.............
and so on for 7 days..

为什么我需要这样做,以便在 while 循环之外使用这些值。

在while循环之外,我有一些像这样的下拉菜单和复选框 -

我的选择框 -

<select name="tuesday-open" class="text2">
    <option>Open</option>
    <?php for($i = 1; $i <= 24; $i++): ?>
        <option value="<?php echo date("h.i A", strtotime("$i:00")); ?>" <?php if(isset($_POST['tuesday-open']) && $_POST['tuesday-open']==(date("h.i A", strtotime("$i:00")))) echo ' selected="selected"'; ?>><?php echo date("h.i A", strtotime("$i:00")); ?></option>
    <?php endfor; ?>
</select>

我的复选框 -

<input type="checkbox" name="tuesday-closed" <?php if(isset($_POST['tuesday-closed'])) echo "checked";?> /> closed

所以我想在带有属性selected=selectedchecked..的选择框和复选框中显示获取值

任何想法将不胜感激,也欢迎另一种方式。谢谢你。

4

3 回答 3

4

正如 Ben 建议的那样,将它们添加到一个数组中,以便稍后在您的代码中引用它们。

while (mysqli_stmt_fetch ($stmt)) {    
    $result[$days] = array(
        'open' => $opentime,
        'close' => $closetime,
        'state' => $closestate
    );
}

echo $result['Monday']['open']; // 06.00 PM

要使其与选择一起使用,您可以将选择与当前值进行比较。您将需要格式化$i以使其与您的数据库条目相当

<select name="monday-open">
    <?php for($i = 0; $i <= 23; $i++): ?>
        <option<?php echo ($i == $result['Monday']['open'] ? ' selected="selected"' : '');?>><?php echo $i; ?></option>
    <?php endfor; ?>
</select>
于 2013-09-06T09:26:49.303 回答
2

尝试这个

while (mysqli_stmt_fetch ($stmt)) 
{    
  $result[$days] = $opentime.' - '.$closetime.' - '.$closestate
}
print_r($result);
于 2013-09-06T09:21:54.723 回答
0
    $days = array();    
    if ( !empty($rows) ) {  

            mysqli_stmt_bind_result($stmt, $userId, $days, $opentime, $closetime, $closestate);

            while (mysqli_stmt_fetch ($stmt)) {     

                $days[] = array('day' => $days,
                                    'opentime' => $opentime,
                                    'closetime' => $closetime,
                                    'closedate' => $closestate
                                   );

            }
        }


     if(is_array($days) && count($days)>0){
      foreach($days as $day){
         echo $day['day'] . ' ' . $day['opentime'] . '  '  . $day['closetime'] . '  ' . $day['closeday'] . '<br/>';
      }
    }
于 2013-09-06T09:32:41.050 回答