CodeIgniter Forums
Removing dulicates during echo - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Removing dulicates during echo (/showthread.php?tid=58059)



Removing dulicates during echo - El Forum - 05-10-2013

[eluser]davehedgehog[/eluser]
hi basically what im trying todo is remover the venue where it repeats its self during and echo. at the moment it work but for only the next entry because or the for each. heres the code~
Code:
<?php $prevenue=false;?><?php $pretitle=false;?>
<?php if ($education): ?>
<?php foreach($education as $ed): ?>

<table class="table" >
&lt;?php if($prevenue == $ed['place']){
$ed['place'] = '';
}else{?&gt;
<tr><td>Venue of  study:<td>&lt;?php echo $ed['place']; ?&gt;</tr>
&lt;?php } ?&gt;
<tr><td>from:<td>&lt;?php echo $ed['datefrom']; ?&gt;<td>To:<td>&lt;?php echo $ed['dateto']; ?&gt;</tr>
<tr><td>Title: <td>&lt;?php echo $ed['title']?&gt;</tr>
<tr><td>Module: <td>&lt;?php echo $ed['module']?&gt;</tr>
<tr><td>Description: <td>&lt;?php echo $ed['description']?&gt;</tr>
&lt;?php $prevenue = $ed['place'];?&gt;
</table>&lt;?php endforeach;?&gt;&lt;?php else: ?&gt;

<br></br>
<p>
    There are no education for this portfolio.
</p>&lt;?php endif;?&gt;

I want to change this to a while so that for every echo if the venue is there then dont repeat, but when I try while it falls over, any help?


Removing dulicates during echo - El Forum - 05-13-2013

[eluser]MonkeyZeus[/eluser]
I assume you are retrieving this data from a DB so I would personally try and modify the query to only pull the results that you need.

However, for your situation I would implement something like this:
Code:
$unique_venues = array();

echo '<table>';
foreach($education as $ed){
   if(!array_key_exists($ed['place'], $unique_venues)){
      $unique_venues[$ed['place']] = '';
      //echo your <tr> stuff
   }
}
echo '</table>';

Good Luck!


Removing dulicates during echo - El Forum - 05-14-2013

[eluser]davehedgehog[/eluser]
awesome cheers pal =)