Duplicate problems, pls help!!!
Posted: Tue Jul 14, 2009 4:27 pm
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
.
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.
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>";