<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>
这是将它们保存在数据库中的方式
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
这就是期望
Electronics
Gaming Monitors
Gaming Laptops
我想摆脱所有重复的电子设备
到目前为止,我的输出是
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Electronics
Gaming Monitors
Gaming Laptops
我有一个用逗号分隔的单词存储在mysql数据库中的类别列表。 我设法显示每个字符串的第一个单词。虽然我的意图是在提取第一个单词后获得独特的类别。但是正如您所看到的,我正在获取所有重复的内容。我如何摆脱它们?
如果要创建表,这是SQL查询
-- --------------------------------------------------------
--
-- 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`),
答案 0 :(得分:1)
您可以稍微更改查询以返回唯一结果。
使用SUBSTRING_INDEX提取第一个值,
SUBSTRING_INDEX('Electronics, PC Gaming, Gaming Computers', ',', 1)
将返回Electronics
。
然后对您的查询执行不同的操作
SELECT DISTINCT(SUBSTRING_INDEX(`Category`, ',', 1)) AS Cat
FROM `laptops`
ORDER BY `Id`
DESC LIMIT 100
现在查询将仅返回一个值的唯一结果。因此,您无需在循环中爆炸结果。优化了下面的代码段,
<?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
}
答案 1 :(得分:0)
尝试使用continue
语句
<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>
答案 2 :(得分:0)
最简单的方法可能是创建一个空数组(例如$ uniqueElements),在for循环中迭代每个元素,并通过使用in_array($ element, $ uniqueElements)。
答案 3 :(得分:0)
您应该将所有第一个单词保存在开头实例化的数组中,例如
$first_categories = [];
然后,为简单起见,在每次迭代中,您都可以将值附加到数组中,例如
$first_categories[] = $prod_Cat[0];
并获得一个只有唯一值的新数组
$unique_first_categories = array_unique($first_categories);
答案 4 :(得分:0)
您可以尝试将其保存为数组并检查是否存在
<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>
答案 5 :(得分:0)
我的答案已更新
<li class="p-b-9">
<a href="#" class="s-text7">
<?
echo end($allCats); // last item
?>
</a>
</li>