/    Sign up×
Community /Pin to ProfileBookmark

show if checkbox is checked in edit page

how to show multiple checkboxes checked in Core PHP Edit Page:

ps: array data

`<div class=”controls controls-row”>
<?php
$tenses = mysqli_query($conn, “SELECT * from tenses”);
if(sizeof($tenses) > 0)
{
foreach($tenses as $tens)
{
?>
<div class=”span3″ style=”margin-left:30px;”>
<label class=”checkbox-inline”><input type=”checkbox” name=”tenses[]” value=”<?php echo $tens[‘pktenseid’]; ?>”> <?php echo $tens[‘tense_name’]; ?></label></div>
<?php
}
}
?>
</div>`

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 17.2020 — Hard to tell what your question is here. Are you asking how one "checks" a box on the display of a page? Or are you asking how you determine if the box is checked when you receive your post-ed data from the client?
Copy linkTweet thisAlerts:
@jasmine381authorJan 17.2020 — @ginerjm#1613270 I just want to show checkedboxes those already selected in Edit page
Copy linkTweet thisAlerts:
@jasmine381authorJan 17.2020 — <label class="checkbox-inline"><input type="checkbox" name="tenses[]" value="<?php echo $tens['pktenseid']; ?>"

<?php if(in_array($tens['tense_name'],$tensesarr)) { echo "checked"; } ?> > <?php echo $tens['tense_name']; ?>

</label>

When i use this code it shows all checkboxes checked in the Edit page?? I want to show only select checkboxes???
Copy linkTweet thisAlerts:
@NachfolgerJan 18.2020 — @jasmine381#1613275

Please use the proper formatting blocks (E.g. [ code ]) when pasting code, it makes it incredibly hard to decipher your code without it.

Moving onto your problem, I don't see why you're using in_array for this, and you have provided **zero** context to allow us to even provide a valid solution. How are we supposed to know when it IS or IS NOT checked?

``<i>
</i>&lt;!-- Checkbox Container --&gt;
&lt;label class="checkbox-inline"&gt;
&lt;!-- Checkbox Element --&gt;
&lt;input type="checkbox" name="tenses[]" value="&lt;?php echo $tens['pktenseid']; ?&gt;" checked="&lt;?php echo $tens['checked'] == 1 ? "true" : "false" ?&gt;" /&gt;
&lt;!-- Checkbox Label --&gt;
&lt;?php echo $tens['tense_name']; ?&gt;
&lt;/label&gt;<i>
</i>
``

As you can see, I've just provided a simple evaluation to allow you to expand on. Does the $tens array contain a property which specifies if it's checked or not? Again, we don't know.

PS, I don't see the value in closing and opening the PHP tags constantly. Just use the foreach loop and echo out the result input element as a string. It's not a serious performance issue, but it creates ugly code and becomes harder to maintain.
Copy linkTweet thisAlerts:
@jasmine381authorJan 18.2020 — this shows all values checked
Copy linkTweet thisAlerts:
@NachfolgerJan 18.2020 — @jasmine381#1613295

Yes I know that, but you aren’t understanding what I’m telling you and I have no idea how to articulate this any further. Read my post again.

You have given us NO means to know WHEN IT SHOULD or SHOULD NOT be checked.
Copy linkTweet thisAlerts:
@ginerjmJan 18.2020 — It seems to me that the problem is the if check on the contents of your $tensearr variable. That is what is setting the checkbox. Debug that and you wouldn't need our help.

Below I provide my version of your spaghetti code:
<i>
</i>echo '&lt;label class="checkbox-inline"&gt;';
echo '&lt;input type="checkbox" name="tenses[]" value="' . $tens['pktenseid'] . "'";
if(in_array($tens['tense_name'],$tensesarr))
echo "checked"; <br/>
echo '&gt;&amp;nbsp;&amp;nbsp;' . $tens['tense_name'];
echo '&lt;/label&gt;';
×

Success!

Help @jasmine381 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...