Populating checkboxes with data from database
How do I populate each checkbox with users role? Stupid thing to get stuck on but anything would help.
$userRoles
is printing out JSON with all users and its roles
$total_row = $data->count();
$output = "";
if ($total_row > 0) {
foreach ($data as $row) {
$roleNames = '';
$userRoles = $row->roles;
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
}
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td>'.$userRoles.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
php arrays laravel if-statement
add a comment |
How do I populate each checkbox with users role? Stupid thing to get stuck on but anything would help.
$userRoles
is printing out JSON with all users and its roles
$total_row = $data->count();
$output = "";
if ($total_row > 0) {
foreach ($data as $row) {
$roleNames = '';
$userRoles = $row->roles;
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
}
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td>'.$userRoles.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
php arrays laravel if-statement
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
All your checkbox inputs have the samename
(not even an array) and ID. how do you recover the data after showing the HTML ? and<td>'.$userRoles.'</td>
will print a json structure no ?
– N69S
Nov 15 '18 at 11:08
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37
add a comment |
How do I populate each checkbox with users role? Stupid thing to get stuck on but anything would help.
$userRoles
is printing out JSON with all users and its roles
$total_row = $data->count();
$output = "";
if ($total_row > 0) {
foreach ($data as $row) {
$roleNames = '';
$userRoles = $row->roles;
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
}
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td>'.$userRoles.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
php arrays laravel if-statement
How do I populate each checkbox with users role? Stupid thing to get stuck on but anything would help.
$userRoles
is printing out JSON with all users and its roles
$total_row = $data->count();
$output = "";
if ($total_row > 0) {
foreach ($data as $row) {
$roleNames = '';
$userRoles = $row->roles;
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
}
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td>'.$roleNames.'</td>
<td>'.$userRoles.'</td>
<td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
php arrays laravel if-statement
php arrays laravel if-statement
asked Nov 15 '18 at 10:59
SucomsSucoms
153
153
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
All your checkbox inputs have the samename
(not even an array) and ID. how do you recover the data after showing the HTML ? and<td>'.$userRoles.'</td>
will print a json structure no ?
– N69S
Nov 15 '18 at 11:08
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37
add a comment |
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
All your checkbox inputs have the samename
(not even an array) and ID. how do you recover the data after showing the HTML ? and<td>'.$userRoles.'</td>
will print a json structure no ?
– N69S
Nov 15 '18 at 11:08
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
All your checkbox inputs have the same
name
(not even an array) and ID. how do you recover the data after showing the HTML ? and <td>'.$userRoles.'</td>
will print a json structure no ?– N69S
Nov 15 '18 at 11:08
All your checkbox inputs have the same
name
(not even an array) and ID. how do you recover the data after showing the HTML ? and <td>'.$userRoles.'</td>
will print a json structure no ?– N69S
Nov 15 '18 at 11:08
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37
add a comment |
1 Answer
1
active
oldest
votes
As mentioned by other people, you have errors in your name
and id
attributes for the checkboxes, as currently they will always be the same. However there is a bigger more sinister problem here.
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role . ' ' . '<input type="checkbox" ' . $checked . ' name="role" value="' . $roles1->id . '" class="checkbox" id="checkboxId">' . ' ' : '';
}
This block of code loops over all the roles and updates the $checked
variable with a string if the user has the role. Problem is, the $checked
variable is outside the loop. So if on the first iteration it passes the truth test, it updates the variable to the string 'checked="checked"', but if all the other roles do not pass, they do not change the variable back to empty, so all check boxes would be checked.
Without seeing what the json structure of the roles are, this is about as best as I can comment.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317938%2fpopulating-checkboxes-with-data-from-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As mentioned by other people, you have errors in your name
and id
attributes for the checkboxes, as currently they will always be the same. However there is a bigger more sinister problem here.
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role . ' ' . '<input type="checkbox" ' . $checked . ' name="role" value="' . $roles1->id . '" class="checkbox" id="checkboxId">' . ' ' : '';
}
This block of code loops over all the roles and updates the $checked
variable with a string if the user has the role. Problem is, the $checked
variable is outside the loop. So if on the first iteration it passes the truth test, it updates the variable to the string 'checked="checked"', but if all the other roles do not pass, they do not change the variable back to empty, so all check boxes would be checked.
Without seeing what the json structure of the roles are, this is about as best as I can comment.
add a comment |
As mentioned by other people, you have errors in your name
and id
attributes for the checkboxes, as currently they will always be the same. However there is a bigger more sinister problem here.
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role . ' ' . '<input type="checkbox" ' . $checked . ' name="role" value="' . $roles1->id . '" class="checkbox" id="checkboxId">' . ' ' : '';
}
This block of code loops over all the roles and updates the $checked
variable with a string if the user has the role. Problem is, the $checked
variable is outside the loop. So if on the first iteration it passes the truth test, it updates the variable to the string 'checked="checked"', but if all the other roles do not pass, they do not change the variable back to empty, so all check boxes would be checked.
Without seeing what the json structure of the roles are, this is about as best as I can comment.
add a comment |
As mentioned by other people, you have errors in your name
and id
attributes for the checkboxes, as currently they will always be the same. However there is a bigger more sinister problem here.
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role . ' ' . '<input type="checkbox" ' . $checked . ' name="role" value="' . $roles1->id . '" class="checkbox" id="checkboxId">' . ' ' : '';
}
This block of code loops over all the roles and updates the $checked
variable with a string if the user has the role. Problem is, the $checked
variable is outside the loop. So if on the first iteration it passes the truth test, it updates the variable to the string 'checked="checked"', but if all the other roles do not pass, they do not change the variable back to empty, so all check boxes would be checked.
Without seeing what the json structure of the roles are, this is about as best as I can comment.
As mentioned by other people, you have errors in your name
and id
attributes for the checkboxes, as currently they will always be the same. However there is a bigger more sinister problem here.
$checked = '';
foreach (Role::all() as $roles1) {
if (in_array($roles1, (array)$userRoles)) {
$checked = 'checked="checked"';
}
$roleNames .= $roles1->role != null ? $roles1->role . ' ' . '<input type="checkbox" ' . $checked . ' name="role" value="' . $roles1->id . '" class="checkbox" id="checkboxId">' . ' ' : '';
}
This block of code loops over all the roles and updates the $checked
variable with a string if the user has the role. Problem is, the $checked
variable is outside the loop. So if on the first iteration it passes the truth test, it updates the variable to the string 'checked="checked"', but if all the other roles do not pass, they do not change the variable back to empty, so all check boxes would be checked.
Without seeing what the json structure of the roles are, this is about as best as I can comment.
answered Nov 15 '18 at 14:03
John HalseyJohn Halsey
900825
900825
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317938%2fpopulating-checkboxes-with-data-from-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems correct, where exactly is your problem? You can't output the restult html string?
– Alesandro Giordano
Nov 15 '18 at 11:02
All your checkbox inputs have the same
name
(not even an array) and ID. how do you recover the data after showing the HTML ? and<td>'.$userRoles.'</td>
will print a json structure no ?– N69S
Nov 15 '18 at 11:08
it does not populate the checkbox, yes, $userRoles prints json
– Sucoms
Nov 15 '18 at 11:37