Jump to content

Loop


StuHannah

Recommended Posts

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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.