Duplicate problems, pls help!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Buung
Forum Newbie
Posts: 4
Joined: Thu Jul 09, 2009 8:14 am

Duplicate problems, pls help!!!

Post by Buung »

Hi all, I'm currently learning PHP and im a bit stuck, im trying to output an array with various number and word formatting which i have succeeded nicely in doing :D .

I now have started learning about the search function " preg_match " and thought I would try to be clever and see if i could build it into my array, which i did for the most part. It works but the way i have done it outputs duplicate records if particularly broad search terms are used like searching just for "e" or another single character because it detects a positive match in more than one column and outputs the whole row multiple times.

If anyone can help it would be most appreciated.

Alternatively theres probably a much more efficient way of doing it alltogether that i don't know about, if you know what it is I would really appreciate hearing about that too!

My code is below, many thanks in advance.

Code: Select all

 
        $array = $items;
        $search = "[a-z]";
        
        $heading = array_flip($array[0]);
        $hcount = 1;
        
        echo '<table class="uberTable">';
        echo '<tr bgcolor=#f4ffa0>';
        while ($hcount <= count($heading))      //the whole while loop is to render the headings of the table/array
            {
            echo '<td align="center">', ucwords(current($heading)), "</td>";
            $hcount++;
            next($heading); 
            }
            echo "</tr>";
            $hcount = 1;
            $vcount = 1;
        
        foreach ($array as $record)
        {
            foreach ($record as $key => $value)
            {
                if (preg_match("/$search/", $value))
                {   
                    if ($isOdd = $vcount%2)
                    {
                        echo '<tr class="trOdd"="tableMain">'; //table formatting for odd numbered rows
                    }
                    else
                    {
                        echo '<tr class="trEven"="tableMain">'; //table formatting for even numbered rows
                    }
                    foreach ($record as $key => $value)
                    {   
                        $noTest = is_numeric($value); //tests if array value is a number
                    
                        if ($key == "price")
                        {
                        echo '<td class="tablePrice">$', number_format($value,2,".",","), "</td>"; //individual formatting for price
                        }   
                        
                        elseif($key == "weight")
                        {
                            echo '<td>', number_format($value,","),  "kg</td>"; //individual formatting for weight
                        }
                        
                        elseif ($noTest == 1)
                        {       
                            echo '<td>', number_format($value,2,".",","), "</td>"; //formatting for numbers
                        }
                            
                        else    
                        {
                            echo '<td>', ucwords($value), "</td>"; //formatting for text
                        }   
            
            
                    }
                }
            }
            echo "</tr>";
            $vcount++;  
        }   
        $vcount=1;
        echo "</table>";
        unset($array,$detail,$value,$key,$noTest);
        echo "<br><br>";
 
 
 
 
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Duplicate problems, pls help!!!

Post by Skara »

Loop through the array searching for what you want first.
Then output the results to the screen.

e.g.

Code: Select all

$array = (some content);
foreach ($array as $a) {
    if (matches_search($a)) {
        $search_results[] = $a;
    }
}
 
// This is what you're looking for:
$search_results = array_unique($search_results);
 
foreach ($search_results as $result) {
    echo $result;
}
Buung
Forum Newbie
Posts: 4
Joined: Thu Jul 09, 2009 8:14 am

Re: Duplicate problems, pls help!!!

Post by Buung »

That was really helpful thanks, the array_unique command was new to me. I ended up working around the issue by confining the search to a single field in the array rather than searching all fields simultaneously so it couldn't duplicate the result.

Thanks very much for the input.
Post Reply