0

Hello I think I am really close but I just cant seem to get my statement working correctly

I have a overtime field from Mysql and I can display it but I am trying to make it either checked or not checked depending on if it is a 1 for checked or 0 for not checked. I can show the 1 or the 0 onto a Text field but the checkbox just won't work for me.

echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"  <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';

My complete code:

<?
// This next section is to load the text fields from the mysql
// this is to collect all of the logged hours on the job sheet and then display
// the hours already entered. 
$hours_query = "SELECT * FROM hours where job_number = '$job_number'";
$hours_result= ($mysqli-> query($hours_query));

// this next section collects the data from the parts table and then adds it to an array
// this then goes into a while statement until it has displayed all of the results it has
// depending on how many parts were used

while ($list_hours = mysqli_fetch_array($hours_result))
{
    echo " <table border ='0' table width = 70%>";
    echo '<td> <input type="hidden" name="chg_hrs_id[]" value="'.$list_hours['unique_id'].'">';
    echo '<td> Start Time  <input type="time" name="chg_hrs_str[]" value="'.$list_hours['start_time'].'" >';
    echo '<td> Finish Time <input type="time" name="chg_hrs_fin[]" value="'.$list_hours['finish_time'].'">';
    echo '<td> Date Of Work <input type="date" name="chg_hrs_date[]" value="'.$list_hours['date'].'">';
    echo '<td> Ovt Boolean <input type="ovt_Booloean" name="chg_hrs_ovtb[]" value="'.$list_hours['overtime'].'">';
    echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"  <? if($list_hours["overtime"]==1){ echo "checked=checked"} ?> >';               
    echo "</tr>";
}
echo "</table>";    
?>
6
  • Why all the extra braces in $hours_result= ($mysqli-> query($hours_query)); Commented Jan 18, 2017 at 20:07
  • You are using the PHP open characters "<?" in an "echo". Try to use dots like you did it in all echo's before. stackoverflow.com/a/3507055/5524279 Commented Jan 18, 2017 at 20:07
  • Hello RiggsFolly I am a little bit of a noob so it tends to be what works and i'm not always understanding why. Commented Jan 18, 2017 at 20:10
  • Hello FabioWdmer thank you I will have a look at the link. I did initially try with the dots but just couldn't get it so went on the extra echo approach Commented Jan 18, 2017 at 20:12
  • One more thing, are you sure that open_tags i.e. <?...?> work on your PHP. To be safe its always best to do <?php if($list_hours["overtime"]==1){ echo "checked=checked"} ?> Commented Jan 18, 2017 at 20:17

2 Answers 2

1

I think you have some Syntax issues. Try the following:

echo '<td> Overtime <input type="checkbox" name="chg_hrs_ovt[]"' . ($list_hours["overtime"]==1 ? 'checked="checked"' : '') . '>';  
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to Marc I had got the syntax incorrect I have used Marc's syntax and it has worked straight away. I now have to see how the statement is put together. It seems like the dots is a link to connect statements together and it doesn't seem to actually need a if statement to be setup.

Comments

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.