jasmine825 jasmine825 - 4 months ago 7
PHP Question

How do I make the output of my array distinct?

I was able to fix the issue however, when i echo out my client_id there is a lot of repetition, and I tried to put array_unique and distinct but nothing worked, what else is there I could do? I also want it to show all the addresses because some clients have more than one address.

$query1= "Select distinct client_id from client_profile where client_status= 'f'";

$result1= pg_query($conn2, $query1);

$clientid = array();
while($row1 = pg_fetch_array($result1)){
$id=$row1['client_id'];
$clientid[]=$id;
$clientid = array_unique($clientid);

}

//$clientid=array_unique($clientid);
$clientid=implode(',',$clientid);


$query= "select * from vouchers where client_id IN ($clientid)";

$result = pg_query($conn,$query);



?>
<!DOCTYPE html>
<html>
<head>
<title>Inactive Clients</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
</head>
<body>
<table class="table table-sm">
<thead>
<tr>
<th>Client id</th>
<th>File Name</th>
</tr>
</thead>
<?php

while($row = pg_fetch_array($result))
{


?>

<tbody>

<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo $row['file_name']; ?></td>


</tr>
<?php }?> </tbody>
</table>
</body>
</html>

Answer

Try running the below query. Also check the column names are correct or not.

select client_id,array_agg(address_Field_column) from vouchers where client_id IN (select distinct client_id from client_profile where client_status= 'f') group by client_id;

I don't know which postgre sql version you are using. In mysql, there is a group_concat function. For your reference See this