stmosaic Posted December 10, 2009 Share Posted December 10, 2009 I've written some code that works about 90% of the way, but have run into an issue that has me stumped. I'm displaying Categories & Subcategories from a database intertwined with a JS expand & contract script. Everything works except that when there is more than one entry in a Subcategory, I get that Subcategory listed twice, which then breaks my JS. I know the solution to the JS end is to get the Subcategory only once, but implementing in has me pulling my hair out. You can view my semi-working sample here http://www.citylifesites.com/services.php?CLSfolder=pinehurst-nc&City=Pinehurst&State=NC, notice the Category Automotive where the Subcategory Tires is shown twice. I've tried a couple of different approaches, GROUP BY, Selecting disctinct Subcategory, array unique function (I may have messed up the syntax when calling the array later in the code as that tends to confuse me), but none have worked. I would appreciate any suggestions! $last_category = ''; // initialize variable to detect a change in the category. Set to a value that will never exist as data $columns = 3; // number of columns in a table row $count = 0; // counter to track current column in the table row $sub = mysql_query("SELECT * FROM $DBTABLE WHERE (CLSfolder='$selectedCLSfolder') && (Active='Y') ORDER BY Category, Subcategory"); echo "<table width=\"98%\" align=\"center\">\n"; // overall table (whatever you current have...) while($row = mysql_fetch_array($sub)){ // check for a new Category and output the heading when the Category changes - if($last_category != $row['Category']){ // if $count is not 0, finish any previous partial row/section of Subcategory data if($count != 0){ // finish the current row while($count % $columns != 0){ echo "<td> </td>"; $count++; } // close the row/section echo "</tr>\n</table>\n"; } // output the Category heading echo "<tr><td colspan=3> </td></tr><tr><td valign=\"top\" bgcolor=\"#FDD9BA\">"; echo "<font face=\"Verdana\"><b>{$row['Category']}</b></font></td></tr>\n"; $last_category = $row['Category']; // remember the new Category $count = 0; // reset the count (new set of rows) } // at this point, you have Subcategory data to display // if $count == 0, need to start a new section for the Subcategory data if($count == 0){ echo "<tr><td><TABLE width=\"100%\" border=0 align=\"center\">\n<tr>"; } elseif($count % $columns == 0) { // if count is not 0 and $count % $columns == 0, need to finish previous row and start a new one echo "</tr>\n<tr>"; } // display the actual Subcategory data - echo "<td width=\"25%\" align=\"left\" valign=\"top\">"; if ($row['Subcategory'] == 'None'){ if ($row['udURL'] == ""){ echo "<font face=\"Verdana\">{$row['Bname']}</font>"; }else{ echo "<a href=\"http://{$row['udURL']}\"><font face=\"Verdana\">{$row['Bname']}</font></a>"; } echo" <br>"; if ($row['Slogan'] != '') { echo "<font face=\"Verdana\" size=2><i>{$row['Slogan']}</i></font><br>"; } if (($row['TFPhone'] != "") && ($row['LPhone'] != "")){echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} / {$row['LPhone']}<br></font>"; }else{ echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} {$row['LPhone']}<br></font>"; } if ($row['CLSmap'] == 'Y'){ echo "<font face=\"Verdana\" size=2><a href=\"maps/maps.php?IDENT={$row['ID']}\">Map/Directions</a></font>"; } }else{ //THIS SEEMS TO BE THE PROBLEM AREA - STARTS THE SUBCATEGORY DATA OUTPUT echo "<ul id=\"{$row['Subcategory']}\" class=\"treeview\"> <font face=\"Verdana\" size=2><li><b>{$row['Subcategory']}</b></font><br><br> <ul>"; if ($row['udURL'] == ""){ echo "<font face=\"Verdana\">{$row['Bname']}</font>"; }else{ echo "<a href=\"http://{$row['udURL']}\"><font face=\"Verdana\">{$row['Bname']}</font></a>"; } echo "<br>"; if ($row['Slogan'] != '') { echo "<font face=\"Verdana\" size=2><i>{$row['Slogan']}</i></font><br>"; } if (($row['TFPhone'] != "") && ($row['LPhone'] != "")){echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} / {$row['LPhone']}<br></font>"; }else{ echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} {$row['LPhone']}<br></font>"; } if ($row['CLSmap'] == 'Y'){ echo "<font face=\"Verdana\" size=2><a href=\"maps/maps.php?IDENT={$row['ID']}\">Map/Directions</a></font>"; } echo"</li></ul> </li></ul> <script type=\"text/javascript\"> ddtreemenu.createTree(\"{$row['Subcategory']}\", false) </script>"; } echo " </td>"; $count++; // count one Subcategory } // end of while loop // if $count is not 0, finish any previous partial row/section of Subcategory data if($count != 0){ while($count % $columns != 0){ echo "<td> </td>"; $count++; } // close the row/section echo "</tr>\n</table>"; } echo "</table>\n"; // close overall table Link to comment https://forums.phpfreaks.com/topic/184668-removing-duplicates-in-an-array-output/ Share on other sites More sharing options...
stmosaic Posted December 11, 2009 Author Share Posted December 11, 2009 Still working on this one, I just tried yet another option, and now all I get is Array rather than the Subcategory. Really getting frustrated with this one. Please, can anyone help? Here is the modified code: $last_category = ''; // initialize variable to detect a change in the category. Set to a value that will never exist as data $columns = 3; // number of columns in a table row $count = 0; // counter to track current column in the table row $sub = mysql_query("SELECT * FROM $DBTABLE WHERE (CLSfolder='$selectedCLSfolder') && (Active='Y') ORDER BY Category, Subcategory"); echo "<table width=\"98%\" align=\"center\">\n"; // overall table (whatever you current have...) while($row = mysql_fetch_array($sub)){ // ADDED $subcategories[] = $row['subcategory']; $newData[] = array_unique($subcategories); //END ADDED // check for a new Category and output the heading when the Category changes - if($last_category != $row['Category']){ // if $count is not 0, finish any previous partial row/section of Subcategory data if($count != 0){ // finish the current row while($count % $columns != 0){ echo "<td> </td>"; $count++; } // close the row/section echo "</tr>\n</table>\n"; } // output the Category heading echo "<tr><td colspan=3> </td></tr><tr><td valign=\"top\" bgcolor=\"#FDD9BA\">"; echo "<font face=\"Verdana\"><b>{$row['Category']}</b></font></td></tr>\n"; $last_category = $row['Category']; // remember the new Category $count = 0; // reset the count (new set of rows) } // at this point, you have Subcategory data to display // if $count == 0, need to start a new section for the Subcategory data if($count == 0){ echo "<tr><td><TABLE width=\"100%\" border=0 align=\"center\">\n<tr>"; } elseif($count % $columns == 0) { // if count is not 0 and $count % $columns == 0, need to finish previous row and start a new one echo "</tr>\n<tr>"; } // display the actual Subcategory data - echo "<td width=\"25%\" align=\"left\" valign=\"top\">"; if ($row['Subcategory'] == 'None'){ if ($row['udURL'] == ""){ echo "<font face=\"Verdana\">{$row['Bname']}</font>"; }else{ echo "<a href=\"http://{$row['udURL']}\"><font face=\"Verdana\">{$row['Bname']}</font></a>"; } echo" <br>"; if ($row['Slogan'] != '') { echo "<font face=\"Verdana\" size=2><i>{$row['Slogan']}</i></font><br>"; } if (($row['TFPhone'] != "") && ($row['LPhone'] != "")){echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} / {$row['LPhone']}<br></font>"; }else{ echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} {$row['LPhone']}<br></font>"; } if ($row['CLSmap'] == 'Y'){ echo "<font face=\"Verdana\" size=2><a href=\"maps/maps.php?IDENT={$row['ID']}\">Map/Directions</a></font>"; } }else{ //MODIFIED 1 echo "<ul id=\"$newData\" class=\"treeview\"> <font face=\"Verdana\" size=2><li><b>$newData</b></font><br><br> //END MODIFIED 1 <ul>"; if ($row['udURL'] == ""){ echo "<font face=\"Verdana\">{$row['Bname']}</font>"; }else{ echo "<a href=\"http://{$row['udURL']}\"><font face=\"Verdana\">{$row['Bname']}</font></a>"; } echo "<br>"; if ($row['Slogan'] != '') { echo "<font face=\"Verdana\" size=2><i>{$row['Slogan']}</i></font><br>"; } if (($row['TFPhone'] != "") && ($row['LPhone'] != "")){echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} / {$row['LPhone']}<br></font>"; }else{ echo "<font face=\"Verdana\" size=2>{$row['TFPhone']} {$row['LPhone']}<br></font>"; } if ($row['CLSmap'] == 'Y'){ echo "<font face=\"Verdana\" size=2><a href=\"maps/maps.php?IDENT={$row['ID']}\">Map/Directions</a></font>"; } echo"</li></ul> </li></ul> <script type=\"text/javascript\"> // MODIFIED 2 ddtreemenu.createTree(\"$newData\", false) //END MODIFIED 2 </script>"; } echo " </td>"; $count++; // count one Subcategory } // end of while loop // if $count is not 0, finish any previous partial row/section of Subcategory data if($count != 0){ while($count % $columns != 0){ echo "<td> </td>"; $count++; } // close the row/section echo "</tr>\n</table>"; } echo "</table>\n"; // close overall table Link to comment https://forums.phpfreaks.com/topic/184668-removing-duplicates-in-an-array-output/#findComment-975443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.