beginnerprogrammer
beginnerprogrammer

Reputation: 508

PHP repeat with Laravel 5

I already can get my data from database, and I need to show it using checkbox. I'm not sure what is wrong, I need it to repeat only once, with 2 checked box, but for now it repeat twice. Can anyone help?

Here is my code:

<?php
$edit_data->service_day = "목,토";
$service_day = explode(',', $edit_data->service_day);
foreach($service_day as $row){
?>
<input type="checkbox" name="service_day[]" id="checkbox2_1" value="월" 
<?php   if($row == "월"){echo 'checked="checked"';}?>
>
<label for="checkbox2_1">월</label>
<input type="checkbox" name="service_day[]" id="checkbox2_2" value="화"
<?php if($row == "화"){echo 'checked="checked"';}?>
>
<label for="checkbox2_2">화</label>
<input type="checkbox" name="service_day[]" id="checkbox2_3" value="수"
<?php if($row == "수"){echo 'checked="checked"';}?>
>
<label for="checkbox2_3">수</label>
<input type="checkbox" name="service_day[]" id="checkbox2_4" value="목"
<?php if($row == "목"){echo 'checked="checked"';}?>
>
<label for="checkbox2_4">목</label>
<input type="checkbox" name="service_day[]" id="checkbox2_5" value="금"
<?php if($row == "금"){echo 'checked="checked"';}?>
>
<label for="checkbox2_5">금</label>
<input type="checkbox" name="service_day[]" id="checkbox2_6" value="토"
<?php if($row == "토"){echo 'checked="checked"';}?>
>
<label for="checkbox2_6">토</label>
<input type="checkbox" name="service_day[]" id="checkbox2_7" value="일"
<?php if($row == "일"){echo 'checked="checked"';}?>
>
<label for="checkbox2_7">일</label>
</td>
<?php } ?>

Upvotes: 0

Views: 59

Answers (1)

Ivanka Todorova
Ivanka Todorova

Reputation: 10219

You are explode-ing the services, thus making it an array.

Foreach is iterating through the array (in your example it has two elements), therefore the code in the body of that foreach is going to repeat twice. If you have 3 elements, it is going to be repeated three times. And so on.

One way to achieve what you are after is to use in_array to check if the service is in the array you have. If it is in the array, mark the checkbox as checked.

<?php
$edit_data->service_day = "목,토";
$service_day = explode(',', $edit_data->service_day);
?>
<input type="checkbox" name="service_day[]" id="checkbox2_1" value="월" 
<?php   if(in_array("월", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_1">월</label>
<input type="checkbox" name="service_day[]" id="checkbox2_2" value="화"
<?php if(in_array("화", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_2">화</label>
<input type="checkbox" name="service_day[]" id="checkbox2_3" value="수"
<?php if(in_array("수", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_3">수</label>
<input type="checkbox" name="service_day[]" id="checkbox2_4" value="목"
<?php if(in_array("목", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_4">목</label>
<input type="checkbox" name="service_day[]" id="checkbox2_5" value="금"
<?php if(in_array("금", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_5">금</label>
<input type="checkbox" name="service_day[]" id="checkbox2_6" value="토"
<?php if(in_array("토", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_6">토</label>
<input type="checkbox" name="service_day[]" id="checkbox2_7" value="일"
<?php if(in_array("일", $service_day)){echo 'checked="checked"';}?>
>
<label for="checkbox2_7">일</label>
</td>

Another approach is:

<?php 

//All of the available service days
$available_service_days = ['월', '화', '수', '목', '금', '토', '일'];

//Checked service days
$edit_data->service_day = "목,토";
$service_day = explode(',', $edit_data->service_day);


//Loop through the available service days to generate checkbox
foreach($available_service_days as $key => $available_service_day) {
//Print the checkbox and check it if necessary
?>

    <input type="checkbox" name="service_day[]" id="checkbox2_<?php echo $key; ?>" value="<?php echo $available_service_day; ?>" 
    <?php if(in_array($available_service_day, $service_day)){echo 'checked="checked"';}?>
    >
    <label for="checkbox2_7"><?php echo $available_service_day; ?></label>
<?php } ?>

Upvotes: 1

Related Questions