php if first instance of variable int

if i have a mysql query that returns some values, these values are categorized by 'parent_id' there are roughly 3-5 values pr 'parent_id'. In my while loop, I only want to echo the first value from each 'parent_id'. How do i go about this?

something like "if this is the first time this variable has this value do stuff"

Answers


Just add the conditional to your while loop ...

$old_id = '';
while($row = $db->fetch_object()){
    if($row->parent_id != $old_id){
        //new parent_id
        $old_id = $row->parent_id;
        //... do stuff ...
    }
}

You can keep the values in an array, and check if a new value is in the array.

$SeenValues=array();

foreach($AllValues as $Value){ //Or some other loop
    if (!in_array($Value, $SeenValues)){
        //Do something

        //Add new value to array
        array_push($SeenValues, $Value);
    }
}

Need Your Help

Can you create your own EC2 autoscaling triggers?

amazon-ec2 amazon-web-services scalability infrastructure

When using an auto-scaling group on EC2 the documentation says you can trigger new servers to come up based on cloudwatch metrics. Is it possible to launch new instances on your own?

C# equivalent of Android WebView for desktop application

javascript c# html5 webview desktop-application

i need to create a desktop application in C# that integrates a "web browser" more or less like an Android WebView, the purpose is to host a specific web site and provide it some javascripts functio...