Displaying Unique First Item From Comma Separated Strings Stored In MYSQL Database
<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
add a comment |
<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
@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
add a comment |
<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
<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
php unique categories
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
add a comment |
@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
add a comment |
6 Answers
6
active
oldest
votes
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
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
add a comment |
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>
Its Working but its outputing a lot of empty spaces between <li>.
– webscrapingtech
Nov 14 '18 at 9:57
so useecho $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 removeecho $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
|
show 3 more comments
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).
add a comment |
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);
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
add a comment |
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>
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
add a comment |
my answer updated
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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>
Its Working but its outputing a lot of empty spaces between <li>.
– webscrapingtech
Nov 14 '18 at 9:57
so useecho $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 removeecho $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
|
show 3 more comments
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>
Its Working but its outputing a lot of empty spaces between <li>.
– webscrapingtech
Nov 14 '18 at 9:57
so useecho $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 removeecho $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
|
show 3 more comments
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>
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>
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 useecho $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 removeecho $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
|
show 3 more comments
Its Working but its outputing a lot of empty spaces between <li>.
– webscrapingtech
Nov 14 '18 at 9:57
so useecho $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 removeecho $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
|
show 3 more comments
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).
add a comment |
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).
add a comment |
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).
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).
answered Nov 14 '18 at 9:23
ohfierceohfierce
12
12
add a comment |
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
add a comment |
my answer updated
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>
add a comment |
my answer updated
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>
add a comment |
my answer updated
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>
my answer updated
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>
answered Nov 15 '18 at 3:39
Warren ClarinWarren Clarin
18110
18110
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
@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