2

I'm trying to create a table that shows the outputs of a mysql query. However, if the result on a row of the query is equal to the default null 00:00:00 then I'd like to instead display ''. Unfortunately, my code for whatever reason changes all of the entries to '' if there exist any 00:00:00 in the query. I expect the issue is with the variable not being redefined during the loop, but I am not completely sure. I appreciate any help. PHP script is as follows:

$table = "<ul data-role='listview' data-theme='b' id='myTabDiv'>";
 while($row = mysqli_fetch_assoc($res)){ 
  $actdep = $row['actdep'];

  if($row['actdep'] = '00:00:00'){
       $actdep = '';
  }

 $table .=  "<li><table style='table-layout: fixed; width: 100%'><tr><td>" . $actdep . "</td></tr></table></li>";

 };
$table .= "</ul>";
echo $table;

If you comment out the 6th line ($actdep = '';) then all the values show, otherwise none of them do. Only one value in the table has a time of 00:00:00.

5 Answers 5

3

It's because you're using the = symbol in the if test. You should be using == for comparison (note the two equals signs.) Instead of comparing the values, you're assigning the value to the variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Easiest question of the day! =) Thank you.
1

You should use == or === for comparison (where the latter compares type, too, other than just interpretations of the value):

if($row['actdep'] == '00:00:00'){
  $actdep = '';
}

Comments

1

change it to

if($row['actdep'] == '00:00:00'){ 
     $actdep = '';
}

1 equals is assigning 2 equals is comparing

Comments

1

You made a mistake in your if statement; rather than using the equality comparison operator, you set the value, which will also not return a boolean.

Try this, instead:

if($row['actdep'] == '00:00:00'){

Comments

0

i would rather place html code outside php

<ul data-role='listview' data-theme='b' id='myTabDiv'>
<?php
 while($row = mysqli_fetch_assoc($res)){ 
  $actdep = $row['actdep'];

  if($row['actdep'] == '00:00:00'){
?>
  <li><table style='table-layout: fixed; width: 100%'><tr><td>&nbsp;</td></tr></table></li>
<?php
 } else {
 ?> 
 <li><table style='table-layout: fixed; width: 100%'><tr><td><?php echo $actdep; ?></td></tr></table></li>
<?php }
 }
 ?>
</ul>

I've also corrected an error with equal symbol
one is used to declare variables,
two is used for equal to,
three is used for different to, (you can also != for different compare)
so you have use double to compare ==.

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.