0

I have following SQL query:

    $readNews_SQLselect = "SELECT ";
    $readNews_SQLselect .= "live, content, user, created, created_updated, user_updated ";  // rows names
    $readNews_SQLselect .= "FROM ";
    $readNews_SQLselect .= "news ";         // table name


    $readNews_SQLselect_Query = mysql_query($readNews_SQLselect); 

And while loop to display the data from DB:

    while ($row = mysql_fetch_array($readNews_SQLselect_Query, MYSQL_ASSOC)) {
                    $LIVE = $row['live'];
                    $CONTENT = $row['content'];
                    $USER = $row['user'];
                    $CREATED = $row['created'];
                    $USER_UPDATED = $row['user_updated'];
                    $CREATED_UPDATED = $row['created_updated'];
                    echo '<input type="checkbox" value=" '.$LIVE.'" />';
                    echo '<input value=" '.$CONTENT.'" />';
                    echo '<p>'.$USER.'<p/>';
                    echo '<p>'.$CREATED.'<p/>';
                    echo '<p>'.$USER_UPDATED.'<p/>';
                    echo '<p>'.$CREATED_UPDATED.'<p/>';
                }
                mysql_free_result($readNews_SQLselect_Query);

As my echo '<input type="checkbox" value=" '.$LIVE.'" />'; will be either '0' or '1' - how can I convert this string into checkbox checking / unchecking with PHP?

Any suggestion much appreciated.

4 Answers 4

1

You'll want to use an In-Line If Statement.

echo '<input id="chkLive" type="checkbox"'.(($LIVE=='1') ? ' checked ' : '').'/>';
Sign up to request clarification or add additional context in comments.

5 Comments

Really? See Live Demo: codepad.org/8yszk8xv. It yields the result <input type="checkbox" checked /><input type="checkbox" /> and you can clearly see the end result on this JSFiddle: jsfiddle.net/6svrG
@Dutche432 - your PHP code looks very complicated to me, can you just briefly explain how does it work?
Click the link in my answer - it brings you to a page that explains the in-line statements, essentially, it's an IF-ELSE statement but written in-line. $var = ((condition) ? trueValue : falseValue)
did some reading / exercises and now I understand your code! Thanks for your time!
1

I don't know if I understand correctly, but you only want to display which ones are checked, right?

echo '<input type="checkbox" ',($LIVE ? 'checked="checked"':''),'/>';

4 Comments

no, I want to display all of them, and check those with value of '1'.
Sorry, it's my bad english ;) But I meant exactly the same thing. This code will of course do and I believe it's the shortest version. You don't need value="" if you don't want to send anything to server while submitting your form.
I will be sending the value of checkbox (checked = 1, unchecked = 0) to the server - as this will be 'update page'.
Then you still don't need value ;) What you need is name="" to distinguish those checkboxes. Use name="live[SOME_KIND_OF_UNIQUE_ID]" so you can use foreach($live as $id => $val) and you will get 'unchecked' in val if any of them are unchecked
1

if $live is your variable which is either 0 or 1,

then you can use:

$boxCheck = '';
if($LIVE == '1')
{
    $boxCheck = 'checked="checked"';
}
else
{
    $boxCheck = '';

}

echo '<input type="checkbox" value=" '.$LIVE.'" '.$boxCheck.'/>';

6 Comments

checked needs value (so checked="checked") if using XHTML (and you do, since you're putting / for self-closing tag). {} and ==1 are useless, and if they weren't it's good practice to put constant first when using == to avoid = and == mistake. Also, you missed ; at the end of the first line ;)
Ok, but thats just to explain the problem in simplest rather than giving short-hand solutions to a beginner and confusing them :)
@linuxeasy - you have a point with beginner straggling to understand shorthands - but learning them from the start can be useful in the future.
Yes, but I frequently encounter $something==true or worse in production code of many companies. There are many thing which most people get wrong just because they are present in every example. Like using . instead of , in echo statement (thus creating temporary string). These are simple things that show that you understand what kind of functions/language constructs you're using and how do they work. It's mostly about good looking code, because those things aren't changing much, the code will still work.
@Anonymous87 well then it becomes a matter of trade-offs, you need to choose, either get used to language as fastest as possible and then learn to optimize or get frustrated at the learning stage itself, and chances that you may lose the wish to learn the language itself (in its extreme sense ;) )
|
1
$checked = ($LIVE) ? 'checked="checked"' : '';
echo '<input type="checkbox" '.$checked.' value=" '.$LIVE.'" />';

3 Comments

your code works perfectly, can you just briefly explain to me how exactly does it work. Thanks.
Yup, that's a solution, although use checked="checked" in xhtml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.