Wilian Brain Wilian Brain - 6 months ago 17
PHP Question

print id one time PHP PDO

I have this code php/pdo and it works fine

$id = (int)$_GET['id'];
$place = $DB_con->prepare("SELECT * FROM `post` WHERE `category`=".$id."");
$place->execute();
foreach ($place->fetchAll() as $rawp){

$mains = $DB_con->prepare("SELECT * FROM `place` WHERE `id`=".$rawp['placeid']."");
$mains->execute();

foreach ($mains->fetchAll() as $rawps){
echo $rawps['id'].'<br/>';
}

}


and the result of that code like this
113
114
125
122
122
127
127

My question is, how can I print the place id just one time like id 122 duplicate i dont want duplicate I want to print id just one time like this 113 114 125 122 127

Answer

This should work and print only once each unique number

$id = (int)$_GET['id'];
$place = $DB_con->prepare("SELECT * FROM `post` WHERE `category`=".$id."");
$place->execute(); 
$array = array();
foreach ($place->fetchAll() as $rawp){
$mains = $DB_con->prepare("SELECT * FROM `place` WHERE `id`=".$rawp['placeid']."");
$mains->execute(); 
foreach ($mains->fetchAll() as $rawps){ 
    if(!in_array($rawps['id'], $array)){
        echo $rawps['id'].'<br/>';
    }
    $array[] = $rawps['id'];
  }
}