PHP Echo Incorrect

Associate
Joined
1 Mar 2006
Posts
425
hi all

im having a little trouble with a PHP Echo

PHP:
<?php
$strSQL = "SELECT * FROM  game";
$result = mysql_query($strSQL);
$row = mysql_fetch_assoc($result);
$chkbox1        = $row['chkbx1'];
$chkbox2        = $row['chkbx2'];
       
echo '
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<meta charset=utf-8 />
<title></title>
</head>
<body>
<table>
        <tr>
                <td>1</td>


                <td><input type="checkbox" name="chkbox1" value="1"
 ';
                                if ($chkbox1 == 1)
                                {
                                        echo ' checked="checked"> ';
                                       
                               }
                                
                               
                                
echo '                 
               
        </tr>
        <tr>
                <td>2</td>
                <td><input type="checkbox" name="chkbox2" value="1"
                ';
                                if ($chkbox2 == 1)
                                {
                                        echo ' checked="checked"> ';
                                }
 
                        echo '
                </td>
        </tr>
</table>
  <div style="display:none">0</div>
</body>
</html>

';
?>


now if you look at the output

jpbxn7.png



i need them both to read

Code:
 checked="checked">

however this does not work

any ideas?
 
You should probably echo the last ">" outside the if statement. Currently if chkbox1/2 = 0 then it's never going to output >.

That should also fix the problem you're having.
 
As Craig321 mentioned, you should move the end chevron outside the IF statement otherwise if the condition fails then no end chevron is added to the input tag.

Try (short-tags replace echo, ternary IF's used) -
Code:
<?php
$strSQL = "SELECT `chkbx1`, `chkbx2` FROM `game`";
$result = mysql_query($strSQL);
$row = mysql_fetch_assoc($result);
$chkbox1 = $row['chkbx1'];
$chkbox2 = $row['chkbx2'];
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<meta charset=utf-8 />
<title></title>
</head>
<body>
<table>
    <tr>
        <td>1</td>
        <input type="checkbox" se
        <td><input type="checkbox" name="chkbox1" value="1"<?=($chkbox1 == 1)?' checked="checked"':''?>></td>
    </tr>
    <tr>
        <td>2</td>
        <td><input type="checkbox" name="chkbox2" value="1"<?=($chkbox2 == 1)?' checked="checked"':''?>></td>
    </tr>
</table>
<div style="display:none">0</div>
</body>
</html>
 
As Craig321 mentioned, you should move the end chevron outside the IF statement otherwise if the condition fails then no end chevron is added to the input tag.

Try (short-tags replace echo, ternary IF's used) -
Code:
<?php
$strSQL = "SELECT `chkbx1`, `chkbx2` FROM `game`";
$result = mysql_query($strSQL);
$row = mysql_fetch_assoc($result);
$chkbox1 = $row['chkbx1'];
$chkbox2 = $row['chkbx2'];
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<meta charset=utf-8 />
<title></title>
</head>
<body>
<table>
    <tr>
        <td>1</td>
        <input type="checkbox" se
        <td><input type="checkbox" name="chkbox1" value="1"<?=($chkbox1 == 1)?' checked="checked"':''?>></td>
    </tr>
    <tr>
        <td>2</td>
        <td><input type="checkbox" name="chkbox2" value="1"<?=($chkbox2 == 1)?' checked="checked"':''?>></td>
    </tr>
</table>
<div style="display:none">0</div>
</body>
</html>


thanks that worked great
 
Back
Top Bottom