Jump to content


Photo

Loop

php mysql loop

  • Please log in to reply
1 reply to this topic

#1 StuHannah

StuHannah
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 07 October 2017 - 08:32 AM

Hi,

 

I want to create a loop within PHP and MySQL. At the moment my table has a column named active that is either 0 or 1 to represent active or not.

 

I want to update my current loop. I know that there are 5 days we operate (at the moment) so I want to pull through the table using a variable so that I can loop through and select days that are active and for the days that are not active simply print a greyed out row.

 

Can anyone point me in the right direction, I was thinking a whileloop so for example:

// $i is the days we are active)

while ($i = 1, $i <= 5, $i++) {
echo "Day name"; // If the day is active then print the name
}
‚Äčelse if the day isn't active it will still be printed, but greyyed out.

This is my current query:

$query = "SELECT * FROM tbl_days WHERE Active = 1";
$result = mysqli_query($dblink, $query);if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
       echo "<tr>";
       echo "<td>" . $row["ID"] ."</td>";
       echo "<td>" . $row["DayName"] . "</td>";
       echo "<td>" . $row["Active"]. "</td>";
       echo "</tr>";
    }
} else {
    echo "Nothing active on the system at the moment.";
}

Thanks in advance, and hope i've given enough information.

 

S

 

 

 



#2 requinix

requinix
  • Administrators
  • Maddening Administrator
  • 9,558 posts
  • LocationWA

Posted 07 October 2017 - 09:10 AM

If you want to show active and inactive days then your query needs to return all of them, not just the active ones. Then loop. A while is fine for it. If the day is active then do one thing, otherwise the day is inactive so do a different thing.

Beyond that it depends on what output you need to create, so if you don't know that then figure it out first. Like with some test data that doesn't come from the database.
The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users