1

I want to show/hide table row based on status values of the fetched columns for mysql using php.

Working Code: This section is working fine

while($row = mysql_fetch_array($sql))  
{
$name[] = $row['name'];
$title[] = $row['title'];
$prize[] = $row['prize'];
$status[] = $row['status'];  // this  will save values as enabled or disabled
$points[] = $row['points'];

}


<?php 

      while($row = mysql_fetch_array($sql))  

{

     echo "<tr> ";
     echo "<td>" .$row[name] . "</td>";
    echo "<td>" .$row[points] . "</td>";
        }
     echo "</tr> " ;
?>

Problem: I need solution for this

I cant display the table as given above due to some more values, that will be taken from other tables, to be shown into the table rows. I am displaying the tables like this.

<tr ><td><?php echo "$title[0]";?></td><td>complete <?php echo "$task[0]";?> </td><td></td></tr>
<tr ><td><?php echo "$title[1]";?></td><td>complete <?php echo "$task[1]";?> </td><td></td></tr>

Question:

Kindly guide me how I hide or show the rows , as defined in problem section , using the value of

$status[] = $row['status'];

, which will be enabled or disabled

3
  • 1
    like that: if($row['status'] == 1) { display } else { not display } Commented Mar 3, 2016 at 8:13
  • Check your condition in your while (fetch) loop, and if condition matches then display (echo) your stuff. If not, then just skip that part...! Commented Mar 3, 2016 at 8:15
  • I know this is easy, Please read problem section , I need to display values in Problem section of question Commented Mar 3, 2016 at 8:22

4 Answers 4

2

Like this

 while($row = mysql_fetch_array($sql))  
{
   if($row['status']=="enabled")
  {
   echo "<tr> ";
   echo "<td>" .$row[name] . "</td>";
   echo "<td>" .$row[points] . "</td>";
   echo "</tr> " ;
  }
}

For the other lines you can do like this

<?php if($status[0] == "enabled") { ?>
    <tr ><td><?php echo "$title[0]";?></td><td>complete <?php echo "$task[0]";?> </td><td></td></tr>
<?php } ?>

EDIT

For loop if all your arrays have the same size, you can try something like this

<?php for($i=0;$i<sizeof($title);$i++) {
if($status[$i]=="enabled"){
?>
<tr ><td><?php echo $title[$i];?></td><td>complete <?php echo $task[$i];?> </td><td></td></tr>
<?php  }
} ?>
Sign up to request clarification or add additional context in comments.

4 Comments

If you are displaying each row one by one, yes. If you find a solution for display them with a boucle it would be easier.
This is what I am looking for ,, simple one condition on the top to all rows, and it should do the trickj, can you suggest something using ( $[status[i] == "enabled" ) , something like using for loop, so that I need to put this condition only once and it should work ... I will accept current solution, as it will solve the problem partially.
I will test it now, Can you please explain this ,,($i=0;$i<sizeof($title);$i++) .. How will it work. with ($title)
If all your arrays have the same size then you can take the size of one and loop one by one. it works with all other arrays, we just need a referal size.
1

This will work for you, it just an example, i think you need to use <table> also.

<?php 
echo "<table border='1'>";
while($row = mysql_fetch_array($sql))  
{
  if($row['status'] == 1){
    echo "<tr>";
      echo "<td>" .$row['name'] . "</td>";
      echo "<td>" .$row['points'] . "</td>";
    echo "</tr>";
  }  
}
echo "</table>";
?>

Side Note:

Stop using mysql_* extension its deprecated and close in PHP 7, use mysqli_* or PDO.

Comments

0
<?php 
while($row = mysql_fetch_array($sql))  
{
    if($row['status']=='enabled') {
        echo "<tr> ";
        echo "<td>" .$row[name] . "</td>";
        echo "<td>" .$row[points] . "</td>";    
        echo "</tr> " ;
    }
}
?>

If $row['status']=='enabled' is false, it will just go to the next loop.

4 Comments

Please see the edit edit: Seems I'm still missing something in your question. Care to clarify what it is?
This is easy , Please see the Problem section, I need to add some more vallues ,
Three of us answered almost the same answers, so I guess it is safe to assume that the problem is in your question. Please consider updating your question, you might get what you're looking for the first time.
I have edited the question , I guess at first I did not clarify the question well.
0

You could use something like this:

<?php while($row = mysql_fetch_array($sql)): ?>
    <?php if($row['status'] == 'enabled'):?>
        <tr>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['points']; ?></td>
        </tr>
    <?php else: ?>
        <?php continue; ?>
    <?php endif; ?>
<?php endwhile; ?>

Or:

<?php
    while($row = mysql_fetch_array($sql)){
        if($row['status'] == 'enabled'){
            echo '<tr><td>' . $row[name] . '</td><td>' . $row[points] . '</td></tr>';
        }
    }
?>

2 Comments

I need to add some more values taken from other tables, That Why i am using it like this. else it would have been a straight forward .
So ur asking for a sql query containing a inner join between more tables to return all the needed values? If that's the case then we would need your DB structure as tables and rows.

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.