EzAnalyst
EzAnalyst

Reputation: 253

MySql While Loop to Assign Variables

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.

Upvotes: 2

Views: 722

Answers (5)

zavg
zavg

Reputation: 11061

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'){

Upvotes: 1

Fabio
Fabio

Reputation: 23490

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 ==.

Upvotes: 0

Kenneth Garza
Kenneth Garza

Reputation: 1916

change it to

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

1 equals is assigning 2 equals is comparing

Upvotes: 1

Grant Thomas
Grant Thomas

Reputation: 45083

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 = '';
}

Upvotes: 1

pilsetnieks
pilsetnieks

Reputation: 10420

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.

Upvotes: 3

Related Questions