TNK
TNK

Reputation: 4323

How I bring data to outside from a while loop?

I have an array of data in a while loop. When I echoing them in while loop data is something similar to this..

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>';

    }
}

Data :

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

In while loop I want to separate data something like this..

$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..

Why I need to do so, to use those values from outside of the while loop.

From outside the while loop I have some dropdowns and checkboxes like this -

My select box -

<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>

My Check box -

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

So I want to display fetching values in select boxes and checkboxes with attribute selected=selected and checked..

Any ideas would be greatly appreciated and an alternative way also welcome. Thank you.

Upvotes: 1

Views: 192

Answers (3)

Prasannjit
Prasannjit

Reputation: 795

try this

while (mysqli_stmt_fetch ($stmt)) 
{    
  $result[$days] = $opentime.' - '.$closetime.' - '.$closestate
}
print_r($result);

Upvotes: 2

ɴᴀᴛᴇ ᴅᴇᴠ
ɴᴀᴛᴇ ᴅᴇᴠ

Reputation: 4611

As Ben suggested, add them into an array so they can be referenced later in your code.

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

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

To make this work with a select you can compare the selection with your current value. You will need to format $i to make it comparable to your database entry.

<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>

Upvotes: 4

Ashish
Ashish

Reputation: 271

    $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/>';
      }
    }

Upvotes: 0

Related Questions