Displaying Unique First Item From Comma Separated Strings Stored In MYSQL Database










0















<ul class="p-b-54">
<li class="p-t-4">
<a href="#" class="s-text13 active1">
All
</a>
</li>
<?php
$category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
$run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
if(mysqli_num_rows($run_query_category) > 0)
while($row = mysqli_fetch_array($run_query_category))
$product_Category = $row["Category"];
?>

<li class="p-b-9">
<a href="#" class="s-text7">
<?php
$prod_Cat = explode(", ", $product_Category);
echo $prod_Cat[0];
?>
</a>
</li>
<?php


?>
</ul>


This is how they are saved in the database



Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops
Electronics, Computers & Accessories, Computers & Tablets
Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops
Electronics, Computers & Accessories, Computers & Tablets, Tablets
Electronics, Computers & Accessories, Monitors
Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts
Electronics, Accessories & Supplies, Audio & Video Accessories
Electronics, Camera & Photo, Digital Cameras, DSLR Cameras
Electronics, Television & Video, Televisions
Electronics, PC Gaming, Gaming Computers
Gaming Monitors
Gaming Laptops


This is what am expecting



Electronics
Gaming Monitors
Gaming Laptops


I want to get rid of all duplicate electronics



So far my output is



Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Gaming Monitors
Gaming Laptops


I have a list of categories stored in mysql database with comma separated words.
I have managed to display the first word form each string. Though my intentions are getting unique categories after extracting the first words. But as you can see am getting all duplicate content. How do i get rid of them?



IF YOU WANT TO CREATE THE TABLE THIS IS THE SQL QUERY



-- --------------------------------------------------------

--
-- Table structure for table `gaminglaptops`
--

CREATE TABLE `gaminglaptops` (
`Id` int(11) NOT NULL,
`Category` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `gaminglaptops`
--

INSERT INTO `gaminglaptops` (`Category`) VALUES
('Gaming Laptops'),
('Electronics, PC Gaming, Gaming Computers'),
('Gaming Monitors'),
('Electronics, Television & Video, Televisions'),
('Electronics, Camera & Photo, Digital Cameras, DSLR Cameras'),
('Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts'),
('Electronics, Accessories & Supplies, Audio & Video Accessories'),
('Electronics, Computers & Accessories, Monitors'),
('Electronics, Computers & Accessories, Computers & Tablets, Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops'),
('Electronics, Computers & Accessories, Computers & Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `gaminglaptops`
--
ALTER TABLE `gaminglaptops`
ADD PRIMARY KEY (`Id`),









share|improve this question
























  • @Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

    – webscrapingtech
    Nov 14 '18 at 11:33











  • If you can provide how sub categories are shown, may be I can help with that too.

    – Samir
    Nov 14 '18 at 12:42











  • just nested ul li that drops down after you click the main category

    – webscrapingtech
    Nov 14 '18 at 13:21















0















<ul class="p-b-54">
<li class="p-t-4">
<a href="#" class="s-text13 active1">
All
</a>
</li>
<?php
$category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
$run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
if(mysqli_num_rows($run_query_category) > 0)
while($row = mysqli_fetch_array($run_query_category))
$product_Category = $row["Category"];
?>

<li class="p-b-9">
<a href="#" class="s-text7">
<?php
$prod_Cat = explode(", ", $product_Category);
echo $prod_Cat[0];
?>
</a>
</li>
<?php


?>
</ul>


This is how they are saved in the database



Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops
Electronics, Computers & Accessories, Computers & Tablets
Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops
Electronics, Computers & Accessories, Computers & Tablets, Tablets
Electronics, Computers & Accessories, Monitors
Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts
Electronics, Accessories & Supplies, Audio & Video Accessories
Electronics, Camera & Photo, Digital Cameras, DSLR Cameras
Electronics, Television & Video, Televisions
Electronics, PC Gaming, Gaming Computers
Gaming Monitors
Gaming Laptops


This is what am expecting



Electronics
Gaming Monitors
Gaming Laptops


I want to get rid of all duplicate electronics



So far my output is



Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Gaming Monitors
Gaming Laptops


I have a list of categories stored in mysql database with comma separated words.
I have managed to display the first word form each string. Though my intentions are getting unique categories after extracting the first words. But as you can see am getting all duplicate content. How do i get rid of them?



IF YOU WANT TO CREATE THE TABLE THIS IS THE SQL QUERY



-- --------------------------------------------------------

--
-- Table structure for table `gaminglaptops`
--

CREATE TABLE `gaminglaptops` (
`Id` int(11) NOT NULL,
`Category` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `gaminglaptops`
--

INSERT INTO `gaminglaptops` (`Category`) VALUES
('Gaming Laptops'),
('Electronics, PC Gaming, Gaming Computers'),
('Gaming Monitors'),
('Electronics, Television & Video, Televisions'),
('Electronics, Camera & Photo, Digital Cameras, DSLR Cameras'),
('Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts'),
('Electronics, Accessories & Supplies, Audio & Video Accessories'),
('Electronics, Computers & Accessories, Monitors'),
('Electronics, Computers & Accessories, Computers & Tablets, Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops'),
('Electronics, Computers & Accessories, Computers & Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `gaminglaptops`
--
ALTER TABLE `gaminglaptops`
ADD PRIMARY KEY (`Id`),









share|improve this question
























  • @Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

    – webscrapingtech
    Nov 14 '18 at 11:33











  • If you can provide how sub categories are shown, may be I can help with that too.

    – Samir
    Nov 14 '18 at 12:42











  • just nested ul li that drops down after you click the main category

    – webscrapingtech
    Nov 14 '18 at 13:21













0












0








0








<ul class="p-b-54">
<li class="p-t-4">
<a href="#" class="s-text13 active1">
All
</a>
</li>
<?php
$category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
$run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
if(mysqli_num_rows($run_query_category) > 0)
while($row = mysqli_fetch_array($run_query_category))
$product_Category = $row["Category"];
?>

<li class="p-b-9">
<a href="#" class="s-text7">
<?php
$prod_Cat = explode(", ", $product_Category);
echo $prod_Cat[0];
?>
</a>
</li>
<?php


?>
</ul>


This is how they are saved in the database



Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops
Electronics, Computers & Accessories, Computers & Tablets
Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops
Electronics, Computers & Accessories, Computers & Tablets, Tablets
Electronics, Computers & Accessories, Monitors
Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts
Electronics, Accessories & Supplies, Audio & Video Accessories
Electronics, Camera & Photo, Digital Cameras, DSLR Cameras
Electronics, Television & Video, Televisions
Electronics, PC Gaming, Gaming Computers
Gaming Monitors
Gaming Laptops


This is what am expecting



Electronics
Gaming Monitors
Gaming Laptops


I want to get rid of all duplicate electronics



So far my output is



Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Gaming Monitors
Gaming Laptops


I have a list of categories stored in mysql database with comma separated words.
I have managed to display the first word form each string. Though my intentions are getting unique categories after extracting the first words. But as you can see am getting all duplicate content. How do i get rid of them?



IF YOU WANT TO CREATE THE TABLE THIS IS THE SQL QUERY



-- --------------------------------------------------------

--
-- Table structure for table `gaminglaptops`
--

CREATE TABLE `gaminglaptops` (
`Id` int(11) NOT NULL,
`Category` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `gaminglaptops`
--

INSERT INTO `gaminglaptops` (`Category`) VALUES
('Gaming Laptops'),
('Electronics, PC Gaming, Gaming Computers'),
('Gaming Monitors'),
('Electronics, Television & Video, Televisions'),
('Electronics, Camera & Photo, Digital Cameras, DSLR Cameras'),
('Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts'),
('Electronics, Accessories & Supplies, Audio & Video Accessories'),
('Electronics, Computers & Accessories, Monitors'),
('Electronics, Computers & Accessories, Computers & Tablets, Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops'),
('Electronics, Computers & Accessories, Computers & Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `gaminglaptops`
--
ALTER TABLE `gaminglaptops`
ADD PRIMARY KEY (`Id`),









share|improve this question
















<ul class="p-b-54">
<li class="p-t-4">
<a href="#" class="s-text13 active1">
All
</a>
</li>
<?php
$category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
$run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
if(mysqli_num_rows($run_query_category) > 0)
while($row = mysqli_fetch_array($run_query_category))
$product_Category = $row["Category"];
?>

<li class="p-b-9">
<a href="#" class="s-text7">
<?php
$prod_Cat = explode(", ", $product_Category);
echo $prod_Cat[0];
?>
</a>
</li>
<?php


?>
</ul>


This is how they are saved in the database



Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops
Electronics, Computers & Accessories, Computers & Tablets
Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops
Electronics, Computers & Accessories, Computers & Tablets, Tablets
Electronics, Computers & Accessories, Monitors
Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts
Electronics, Accessories & Supplies, Audio & Video Accessories
Electronics, Camera & Photo, Digital Cameras, DSLR Cameras
Electronics, Television & Video, Televisions
Electronics, PC Gaming, Gaming Computers
Gaming Monitors
Gaming Laptops


This is what am expecting



Electronics
Gaming Monitors
Gaming Laptops


I want to get rid of all duplicate electronics



So far my output is



Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Gaming Monitors
Gaming Laptops


I have a list of categories stored in mysql database with comma separated words.
I have managed to display the first word form each string. Though my intentions are getting unique categories after extracting the first words. But as you can see am getting all duplicate content. How do i get rid of them?



IF YOU WANT TO CREATE THE TABLE THIS IS THE SQL QUERY



-- --------------------------------------------------------

--
-- Table structure for table `gaminglaptops`
--

CREATE TABLE `gaminglaptops` (
`Id` int(11) NOT NULL,
`Category` text NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `gaminglaptops`
--

INSERT INTO `gaminglaptops` (`Category`) VALUES
('Gaming Laptops'),
('Electronics, PC Gaming, Gaming Computers'),
('Gaming Monitors'),
('Electronics, Television & Video, Televisions'),
('Electronics, Camera & Photo, Digital Cameras, DSLR Cameras'),
('Electronics, Accessories & Supplies, Audio & Video Accessories, TV Accessories & Parts, TV Ceiling & Wall Mounts'),
('Electronics, Accessories & Supplies, Audio & Video Accessories'),
('Electronics, Computers & Accessories, Monitors'),
('Electronics, Computers & Accessories, Computers & Tablets, Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, 2 in 1 Laptops'),
('Electronics, Computers & Accessories, Computers & Tablets'),
('Electronics, Computers & Accessories, Computers & Tablets, Laptops, Traditional Laptops');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `gaminglaptops`
--
ALTER TABLE `gaminglaptops`
ADD PRIMARY KEY (`Id`),






php unique categories






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 11:28







webscrapingtech

















asked Nov 14 '18 at 9:12









webscrapingtechwebscrapingtech

188




188












  • @Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

    – webscrapingtech
    Nov 14 '18 at 11:33











  • If you can provide how sub categories are shown, may be I can help with that too.

    – Samir
    Nov 14 '18 at 12:42











  • just nested ul li that drops down after you click the main category

    – webscrapingtech
    Nov 14 '18 at 13:21

















  • @Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

    – webscrapingtech
    Nov 14 '18 at 11:33











  • If you can provide how sub categories are shown, may be I can help with that too.

    – Samir
    Nov 14 '18 at 12:42











  • just nested ul li that drops down after you click the main category

    – webscrapingtech
    Nov 14 '18 at 13:21
















@Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

– webscrapingtech
Nov 14 '18 at 11:33





@Samir I know but i don't want to mess with the query because i will also use it for sub categories. I want to pull everything then manipulate it with php.

– webscrapingtech
Nov 14 '18 at 11:33













If you can provide how sub categories are shown, may be I can help with that too.

– Samir
Nov 14 '18 at 12:42





If you can provide how sub categories are shown, may be I can help with that too.

– Samir
Nov 14 '18 at 12:42













just nested ul li that drops down after you click the main category

– webscrapingtech
Nov 14 '18 at 13:21





just nested ul li that drops down after you click the main category

– webscrapingtech
Nov 14 '18 at 13:21












6 Answers
6






active

oldest

votes


















1














You can change your query a bit to return unique results.



Use SUBSTRING_INDEX to extract 1st value,



SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)


will return Electronics.



Then do a distinct on your query,



SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
FROM `laptops`
ORDER BY `Id`
DESC LIMIT 100


Now the query will return unique results with one value only. So, you can eliminate the need to explode the results in your loop. Optimized your snippet below,



<?php
while($row = mysqli_fetch_array($run_query_category))
?>
<li class="p-b-9">
<a href="#" class="s-text7"><?php echo $row["Cat"]; ?></a>
</li>
<?php






share|improve this answer

























  • I had to use this it's working as expected

    – webscrapingtech
    Nov 14 '18 at 14:33











  • What if my first word looks like this - Arts, Crafts & Sewing

    – webscrapingtech
    Nov 18 '18 at 21:16












  • If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

    – Samir
    Nov 19 '18 at 6:50


















0














try with continue statement



<ul class="p-b-54">
<li class="p-t-4">
<a href="#" class="s-text13 active1">
All
</a>
</li>
<?php
$cat_name = "";
$category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
$run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
if(mysqli_num_rows($run_query_category) > 0)
while($row = mysqli_fetch_array($run_query_category))
$product_Category = $row["Category"];
?>

<li class="p-b-9">
<a href="#" class="s-text7">
<?php
$prod_Cat = explode(", ", $product_Category);
if($cat_name == $prod_Cat[0])
continue;
else
$catname = $prod_Cat[0];

echo $catname;
?>
</a>
</li>
<?php


?>
</ul>





share|improve this answer























  • Its Working but its outputing a lot of empty spaces between <li>.

    – webscrapingtech
    Nov 14 '18 at 9:57











  • so use echo $catname inside else part its working.

    – Bhargav Chudasama
    Nov 14 '18 at 9:59











  • if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

    – webscrapingtech
    Nov 14 '18 at 10:10












  • and remove echo $catname in last?

    – Bhargav Chudasama
    Nov 14 '18 at 10:12











  • cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

    – webscrapingtech
    Nov 14 '18 at 10:42


















0














The simplest approached would probably be to create an empty array (such as $uniqueElements), iterate each of your elements in a for-loop and push each unique item into your empty array by checking the content with in_array($element, $uniqueElements).






share|improve this answer






























    0














    You should save all your first-words in an array instantiated at the start like



    $first_categories = ;


    And then, for the sake of simplicity, in each iteration you could just append the value to the array like



    $first_categories = $prod_Cat[0];


    And get a new array with only the unique values



    $unique_first_categories = array_unique($first_categories);





    share|improve this answer























    • Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

      – webscrapingtech
      Nov 14 '18 at 10:54



















    0














    You can try saving it an array and check if exist



    <ul class="p-b-54">
    <li class="p-t-4">
    <a href="#" class="s-text13 active1">
    All
    </a>
    </li>
    <?php
    $cat_name = "";
    $allCat = array();
    $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
    $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
    if(mysqli_num_rows($run_query_category) > 0)
    while($row = mysqli_fetch_array($run_query_category))
    $product_Category = $row["Category"];
    $prod_Cat = explode(", ", $product_Category);

    if(!in_array($prod_Cat[0], $allCats) // if not yet exist in array
    // push to allCats (unique)
    array_push($allCats, $prodCat[0]);
    // display
    ?>

    <li class="p-b-9">
    <a href="#" class="s-text7">
    <?php
    echo end($allCats); // last item pushed
    ?>
    </a>
    </li>
    <?php



    ?>
    </ul>





    share|improve this answer

























    • Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

      – webscrapingtech
      Nov 14 '18 at 11:09











    • This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

      – webscrapingtech
      Nov 14 '18 at 11:47


















    0














    my answer updated



    <li class="p-b-9">
    <a href="#" class="s-text7">
    <?
    echo end($allCats); // last item
    ?>
    </a>
    </li>





    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296559%2fdisplaying-unique-first-item-from-comma-separated-strings-stored-in-mysql-databa%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can change your query a bit to return unique results.



      Use SUBSTRING_INDEX to extract 1st value,



      SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)


      will return Electronics.



      Then do a distinct on your query,



      SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
      FROM `laptops`
      ORDER BY `Id`
      DESC LIMIT 100


      Now the query will return unique results with one value only. So, you can eliminate the need to explode the results in your loop. Optimized your snippet below,



      <?php
      while($row = mysqli_fetch_array($run_query_category))
      ?>
      <li class="p-b-9">
      <a href="#" class="s-text7"><?php echo $row["Cat"]; ?></a>
      </li>
      <?php






      share|improve this answer

























      • I had to use this it's working as expected

        – webscrapingtech
        Nov 14 '18 at 14:33











      • What if my first word looks like this - Arts, Crafts & Sewing

        – webscrapingtech
        Nov 18 '18 at 21:16












      • If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

        – Samir
        Nov 19 '18 at 6:50















      1














      You can change your query a bit to return unique results.



      Use SUBSTRING_INDEX to extract 1st value,



      SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)


      will return Electronics.



      Then do a distinct on your query,



      SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
      FROM `laptops`
      ORDER BY `Id`
      DESC LIMIT 100


      Now the query will return unique results with one value only. So, you can eliminate the need to explode the results in your loop. Optimized your snippet below,



      <?php
      while($row = mysqli_fetch_array($run_query_category))
      ?>
      <li class="p-b-9">
      <a href="#" class="s-text7"><?php echo $row["Cat"]; ?></a>
      </li>
      <?php






      share|improve this answer

























      • I had to use this it's working as expected

        – webscrapingtech
        Nov 14 '18 at 14:33











      • What if my first word looks like this - Arts, Crafts & Sewing

        – webscrapingtech
        Nov 18 '18 at 21:16












      • If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

        – Samir
        Nov 19 '18 at 6:50













      1












      1








      1







      You can change your query a bit to return unique results.



      Use SUBSTRING_INDEX to extract 1st value,



      SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)


      will return Electronics.



      Then do a distinct on your query,



      SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
      FROM `laptops`
      ORDER BY `Id`
      DESC LIMIT 100


      Now the query will return unique results with one value only. So, you can eliminate the need to explode the results in your loop. Optimized your snippet below,



      <?php
      while($row = mysqli_fetch_array($run_query_category))
      ?>
      <li class="p-b-9">
      <a href="#" class="s-text7"><?php echo $row["Cat"]; ?></a>
      </li>
      <?php






      share|improve this answer















      You can change your query a bit to return unique results.



      Use SUBSTRING_INDEX to extract 1st value,



      SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)


      will return Electronics.



      Then do a distinct on your query,



      SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
      FROM `laptops`
      ORDER BY `Id`
      DESC LIMIT 100


      Now the query will return unique results with one value only. So, you can eliminate the need to explode the results in your loop. Optimized your snippet below,



      <?php
      while($row = mysqli_fetch_array($run_query_category))
      ?>
      <li class="p-b-9">
      <a href="#" class="s-text7"><?php echo $row["Cat"]; ?></a>
      </li>
      <?php







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 14 '18 at 9:29

























      answered Nov 14 '18 at 9:23









      SamirSamir

      5,3492628




      5,3492628












      • I had to use this it's working as expected

        – webscrapingtech
        Nov 14 '18 at 14:33











      • What if my first word looks like this - Arts, Crafts & Sewing

        – webscrapingtech
        Nov 18 '18 at 21:16












      • If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

        – Samir
        Nov 19 '18 at 6:50

















      • I had to use this it's working as expected

        – webscrapingtech
        Nov 14 '18 at 14:33











      • What if my first word looks like this - Arts, Crafts & Sewing

        – webscrapingtech
        Nov 18 '18 at 21:16












      • If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

        – Samir
        Nov 19 '18 at 6:50
















      I had to use this it's working as expected

      – webscrapingtech
      Nov 14 '18 at 14:33





      I had to use this it's working as expected

      – webscrapingtech
      Nov 14 '18 at 14:33













      What if my first word looks like this - Arts, Crafts & Sewing

      – webscrapingtech
      Nov 18 '18 at 21:16






      What if my first word looks like this - Arts, Crafts & Sewing

      – webscrapingtech
      Nov 18 '18 at 21:16














      If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

      – Samir
      Nov 19 '18 at 6:50





      If there is a comma in your term as well as comma is used as as separator, then there is no way to identify string delimiter. Consider changing the separator to any uncommon character like | or something unique that doesn't appear in your actual text.

      – Samir
      Nov 19 '18 at 6:50













      0














      try with continue statement



      <ul class="p-b-54">
      <li class="p-t-4">
      <a href="#" class="s-text13 active1">
      All
      </a>
      </li>
      <?php
      $cat_name = "";
      $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
      $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
      if(mysqli_num_rows($run_query_category) > 0)
      while($row = mysqli_fetch_array($run_query_category))
      $product_Category = $row["Category"];
      ?>

      <li class="p-b-9">
      <a href="#" class="s-text7">
      <?php
      $prod_Cat = explode(", ", $product_Category);
      if($cat_name == $prod_Cat[0])
      continue;
      else
      $catname = $prod_Cat[0];

      echo $catname;
      ?>
      </a>
      </li>
      <?php


      ?>
      </ul>





      share|improve this answer























      • Its Working but its outputing a lot of empty spaces between <li>.

        – webscrapingtech
        Nov 14 '18 at 9:57











      • so use echo $catname inside else part its working.

        – Bhargav Chudasama
        Nov 14 '18 at 9:59











      • if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

        – webscrapingtech
        Nov 14 '18 at 10:10












      • and remove echo $catname in last?

        – Bhargav Chudasama
        Nov 14 '18 at 10:12











      • cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

        – webscrapingtech
        Nov 14 '18 at 10:42















      0














      try with continue statement



      <ul class="p-b-54">
      <li class="p-t-4">
      <a href="#" class="s-text13 active1">
      All
      </a>
      </li>
      <?php
      $cat_name = "";
      $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
      $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
      if(mysqli_num_rows($run_query_category) > 0)
      while($row = mysqli_fetch_array($run_query_category))
      $product_Category = $row["Category"];
      ?>

      <li class="p-b-9">
      <a href="#" class="s-text7">
      <?php
      $prod_Cat = explode(", ", $product_Category);
      if($cat_name == $prod_Cat[0])
      continue;
      else
      $catname = $prod_Cat[0];

      echo $catname;
      ?>
      </a>
      </li>
      <?php


      ?>
      </ul>





      share|improve this answer























      • Its Working but its outputing a lot of empty spaces between <li>.

        – webscrapingtech
        Nov 14 '18 at 9:57











      • so use echo $catname inside else part its working.

        – Bhargav Chudasama
        Nov 14 '18 at 9:59











      • if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

        – webscrapingtech
        Nov 14 '18 at 10:10












      • and remove echo $catname in last?

        – Bhargav Chudasama
        Nov 14 '18 at 10:12











      • cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

        – webscrapingtech
        Nov 14 '18 at 10:42













      0












      0








      0







      try with continue statement



      <ul class="p-b-54">
      <li class="p-t-4">
      <a href="#" class="s-text13 active1">
      All
      </a>
      </li>
      <?php
      $cat_name = "";
      $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
      $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
      if(mysqli_num_rows($run_query_category) > 0)
      while($row = mysqli_fetch_array($run_query_category))
      $product_Category = $row["Category"];
      ?>

      <li class="p-b-9">
      <a href="#" class="s-text7">
      <?php
      $prod_Cat = explode(", ", $product_Category);
      if($cat_name == $prod_Cat[0])
      continue;
      else
      $catname = $prod_Cat[0];

      echo $catname;
      ?>
      </a>
      </li>
      <?php


      ?>
      </ul>





      share|improve this answer













      try with continue statement



      <ul class="p-b-54">
      <li class="p-t-4">
      <a href="#" class="s-text13 active1">
      All
      </a>
      </li>
      <?php
      $cat_name = "";
      $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
      $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
      if(mysqli_num_rows($run_query_category) > 0)
      while($row = mysqli_fetch_array($run_query_category))
      $product_Category = $row["Category"];
      ?>

      <li class="p-b-9">
      <a href="#" class="s-text7">
      <?php
      $prod_Cat = explode(", ", $product_Category);
      if($cat_name == $prod_Cat[0])
      continue;
      else
      $catname = $prod_Cat[0];

      echo $catname;
      ?>
      </a>
      </li>
      <?php


      ?>
      </ul>






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 14 '18 at 9:22









      Bhargav ChudasamaBhargav Chudasama

      4,3582925




      4,3582925












      • Its Working but its outputing a lot of empty spaces between <li>.

        – webscrapingtech
        Nov 14 '18 at 9:57











      • so use echo $catname inside else part its working.

        – Bhargav Chudasama
        Nov 14 '18 at 9:59











      • if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

        – webscrapingtech
        Nov 14 '18 at 10:10












      • and remove echo $catname in last?

        – Bhargav Chudasama
        Nov 14 '18 at 10:12











      • cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

        – webscrapingtech
        Nov 14 '18 at 10:42

















      • Its Working but its outputing a lot of empty spaces between <li>.

        – webscrapingtech
        Nov 14 '18 at 9:57











      • so use echo $catname inside else part its working.

        – Bhargav Chudasama
        Nov 14 '18 at 9:59











      • if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

        – webscrapingtech
        Nov 14 '18 at 10:10












      • and remove echo $catname in last?

        – Bhargav Chudasama
        Nov 14 '18 at 10:12











      • cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

        – webscrapingtech
        Nov 14 '18 at 10:42
















      Its Working but its outputing a lot of empty spaces between <li>.

      – webscrapingtech
      Nov 14 '18 at 9:57





      Its Working but its outputing a lot of empty spaces between <li>.

      – webscrapingtech
      Nov 14 '18 at 9:57













      so use echo $catname inside else part its working.

      – Bhargav Chudasama
      Nov 14 '18 at 9:59





      so use echo $catname inside else part its working.

      – Bhargav Chudasama
      Nov 14 '18 at 9:59













      if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

      – webscrapingtech
      Nov 14 '18 at 10:10






      if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; Produces the same result

      – webscrapingtech
      Nov 14 '18 at 10:10














      and remove echo $catname in last?

      – Bhargav Chudasama
      Nov 14 '18 at 10:12





      and remove echo $catname in last?

      – Bhargav Chudasama
      Nov 14 '18 at 10:12













      cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

      – webscrapingtech
      Nov 14 '18 at 10:42





      cutting and pasting doesnt work ?> <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if($cat_name == $prod_Cat[0]) continue; else $cat_name = $prod_Cat[0]; echo $cat_name; ?> </a> </li> <?php } } ?>

      – webscrapingtech
      Nov 14 '18 at 10:42











      0














      The simplest approached would probably be to create an empty array (such as $uniqueElements), iterate each of your elements in a for-loop and push each unique item into your empty array by checking the content with in_array($element, $uniqueElements).






      share|improve this answer



























        0














        The simplest approached would probably be to create an empty array (such as $uniqueElements), iterate each of your elements in a for-loop and push each unique item into your empty array by checking the content with in_array($element, $uniqueElements).






        share|improve this answer

























          0












          0








          0







          The simplest approached would probably be to create an empty array (such as $uniqueElements), iterate each of your elements in a for-loop and push each unique item into your empty array by checking the content with in_array($element, $uniqueElements).






          share|improve this answer













          The simplest approached would probably be to create an empty array (such as $uniqueElements), iterate each of your elements in a for-loop and push each unique item into your empty array by checking the content with in_array($element, $uniqueElements).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 9:23









          ohfierceohfierce

          12




          12





















              0














              You should save all your first-words in an array instantiated at the start like



              $first_categories = ;


              And then, for the sake of simplicity, in each iteration you could just append the value to the array like



              $first_categories = $prod_Cat[0];


              And get a new array with only the unique values



              $unique_first_categories = array_unique($first_categories);





              share|improve this answer























              • Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

                – webscrapingtech
                Nov 14 '18 at 10:54
















              0














              You should save all your first-words in an array instantiated at the start like



              $first_categories = ;


              And then, for the sake of simplicity, in each iteration you could just append the value to the array like



              $first_categories = $prod_Cat[0];


              And get a new array with only the unique values



              $unique_first_categories = array_unique($first_categories);





              share|improve this answer























              • Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

                – webscrapingtech
                Nov 14 '18 at 10:54














              0












              0








              0







              You should save all your first-words in an array instantiated at the start like



              $first_categories = ;


              And then, for the sake of simplicity, in each iteration you could just append the value to the array like



              $first_categories = $prod_Cat[0];


              And get a new array with only the unique values



              $unique_first_categories = array_unique($first_categories);





              share|improve this answer













              You should save all your first-words in an array instantiated at the start like



              $first_categories = ;


              And then, for the sake of simplicity, in each iteration you could just append the value to the array like



              $first_categories = $prod_Cat[0];


              And get a new array with only the unique values



              $unique_first_categories = array_unique($first_categories);






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 14 '18 at 9:26









              Carlos FdevCarlos Fdev

              590422




              590422












              • Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

                – webscrapingtech
                Nov 14 '18 at 10:54


















              • Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

                – webscrapingtech
                Nov 14 '18 at 10:54

















              Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

              – webscrapingtech
              Nov 14 '18 at 10:54






              Your answer is not working also i had tried array unique before <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); $first_categories = ; $first_categories = $prod_Cat[0]; $unique_first_categories = array_unique($first_categories); echo $unique_first_categories[0]; ?> </a> </li> <?php

              – webscrapingtech
              Nov 14 '18 at 10:54












              0














              You can try saving it an array and check if exist



              <ul class="p-b-54">
              <li class="p-t-4">
              <a href="#" class="s-text13 active1">
              All
              </a>
              </li>
              <?php
              $cat_name = "";
              $allCat = array();
              $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
              $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
              if(mysqli_num_rows($run_query_category) > 0)
              while($row = mysqli_fetch_array($run_query_category))
              $product_Category = $row["Category"];
              $prod_Cat = explode(", ", $product_Category);

              if(!in_array($prod_Cat[0], $allCats) // if not yet exist in array
              // push to allCats (unique)
              array_push($allCats, $prodCat[0]);
              // display
              ?>

              <li class="p-b-9">
              <a href="#" class="s-text7">
              <?php
              echo end($allCats); // last item pushed
              ?>
              </a>
              </li>
              <?php



              ?>
              </ul>





              share|improve this answer

























              • Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

                – webscrapingtech
                Nov 14 '18 at 11:09











              • This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

                – webscrapingtech
                Nov 14 '18 at 11:47















              0














              You can try saving it an array and check if exist



              <ul class="p-b-54">
              <li class="p-t-4">
              <a href="#" class="s-text13 active1">
              All
              </a>
              </li>
              <?php
              $cat_name = "";
              $allCat = array();
              $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
              $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
              if(mysqli_num_rows($run_query_category) > 0)
              while($row = mysqli_fetch_array($run_query_category))
              $product_Category = $row["Category"];
              $prod_Cat = explode(", ", $product_Category);

              if(!in_array($prod_Cat[0], $allCats) // if not yet exist in array
              // push to allCats (unique)
              array_push($allCats, $prodCat[0]);
              // display
              ?>

              <li class="p-b-9">
              <a href="#" class="s-text7">
              <?php
              echo end($allCats); // last item pushed
              ?>
              </a>
              </li>
              <?php



              ?>
              </ul>





              share|improve this answer

























              • Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

                – webscrapingtech
                Nov 14 '18 at 11:09











              • This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

                – webscrapingtech
                Nov 14 '18 at 11:47













              0












              0








              0







              You can try saving it an array and check if exist



              <ul class="p-b-54">
              <li class="p-t-4">
              <a href="#" class="s-text13 active1">
              All
              </a>
              </li>
              <?php
              $cat_name = "";
              $allCat = array();
              $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
              $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
              if(mysqli_num_rows($run_query_category) > 0)
              while($row = mysqli_fetch_array($run_query_category))
              $product_Category = $row["Category"];
              $prod_Cat = explode(", ", $product_Category);

              if(!in_array($prod_Cat[0], $allCats) // if not yet exist in array
              // push to allCats (unique)
              array_push($allCats, $prodCat[0]);
              // display
              ?>

              <li class="p-b-9">
              <a href="#" class="s-text7">
              <?php
              echo end($allCats); // last item pushed
              ?>
              </a>
              </li>
              <?php



              ?>
              </ul>





              share|improve this answer















              You can try saving it an array and check if exist



              <ul class="p-b-54">
              <li class="p-t-4">
              <a href="#" class="s-text13 active1">
              All
              </a>
              </li>
              <?php
              $cat_name = "";
              $allCat = array();
              $category_query_sql = "SELECT DISTINCT `Category` FROM `laptops` ORDER BY `Id` DESC LIMIT 100 ";
              $run_query_category = mysqli_query($con,$category_query_sql) or die(mysqli_error($con));
              if(mysqli_num_rows($run_query_category) > 0)
              while($row = mysqli_fetch_array($run_query_category))
              $product_Category = $row["Category"];
              $prod_Cat = explode(", ", $product_Category);

              if(!in_array($prod_Cat[0], $allCats) // if not yet exist in array
              // push to allCats (unique)
              array_push($allCats, $prodCat[0]);
              // display
              ?>

              <li class="p-b-9">
              <a href="#" class="s-text7">
              <?php
              echo end($allCats); // last item pushed
              ?>
              </a>
              </li>
              <?php



              ?>
              </ul>






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 3:35

























              answered Nov 14 '18 at 9:35









              Warren ClarinWarren Clarin

              18110




              18110












              • Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

                – webscrapingtech
                Nov 14 '18 at 11:09











              • This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

                – webscrapingtech
                Nov 14 '18 at 11:47

















              • Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

                – webscrapingtech
                Nov 14 '18 at 11:09











              • This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

                – webscrapingtech
                Nov 14 '18 at 11:47
















              Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

              – webscrapingtech
              Nov 14 '18 at 11:09





              Working But Producing empty li <li class="p-b-9"> <a href="#" class="s-text7"> </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Monitors </a> </li> <li class="p-b-9"> <a href="#" class="s-text7"> Gaming Laptops </a>

              – webscrapingtech
              Nov 14 '18 at 11:09













              This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

              – webscrapingtech
              Nov 14 '18 at 11:47





              This is what i exactly did <li class="p-b-9"> <a href="#" class="s-text7"> <?php $prod_Cat = explode(", ", $product_Category); if(!in_array($prod_Cat[0], $allCats)) array_push($allCats, $prod_Cat[0]); if ($prod_Cat[0] != "") # code... echo $prod_Cat[0]; ?> </a> </li> <?php } } ?>

              – webscrapingtech
              Nov 14 '18 at 11:47











              0














              my answer updated



              <li class="p-b-9">
              <a href="#" class="s-text7">
              <?
              echo end($allCats); // last item
              ?>
              </a>
              </li>





              share|improve this answer



























                0














                my answer updated



                <li class="p-b-9">
                <a href="#" class="s-text7">
                <?
                echo end($allCats); // last item
                ?>
                </a>
                </li>





                share|improve this answer

























                  0












                  0








                  0







                  my answer updated



                  <li class="p-b-9">
                  <a href="#" class="s-text7">
                  <?
                  echo end($allCats); // last item
                  ?>
                  </a>
                  </li>





                  share|improve this answer













                  my answer updated



                  <li class="p-b-9">
                  <a href="#" class="s-text7">
                  <?
                  echo end($allCats); // last item
                  ?>
                  </a>
                  </li>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 3:39









                  Warren ClarinWarren Clarin

                  18110




                  18110



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296559%2fdisplaying-unique-first-item-from-comma-separated-strings-stored-in-mysql-databa%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo