3

I have a while loop that returns X amount of data with the same value X amount of times before it changes to a different value.

// example 121, 121, 121, 121, 113, 113, 113, 113 

each value is on its own line so a while loops puts them on the next is it possible to have an if that uses when the value changes ?

some thing like

while($row = $result->fetch_assoc()) {

    if ($row["name"] == $row["name"]){
         echo "<tr><td>".$row["name"]."</td><td>".$row["combi"]."</td>"
    }else{
         echo "<td>".$row["combi"]."</td></tr>"
    }
}
2
  • 2
    $row["name"] == $row["name"] - this will always be true, I suppose. You can use a temporary variable to store previous name, but I wonder can't you do the same thing with GROUP BY-ing your query instead. Commented Sep 25, 2015 at 15:42
  • Simply make a group by name clause within your query Commented Sep 25, 2015 at 16:20

1 Answer 1

1

You just use another variable to stock the last value

<?php
$lastval = 0;
while($row = $result->fetch_assoc()) {

    if ($row["name"] != $lastval){
        $lastval = $row["name"];
        // value has changed
    }else{
        // Value is the same as the last one
    }
}
?>

Don't forget to "ORDER BY name"

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

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.