[英]PHP MYSQL Array and sort
So, I have a database table that has up to 8 separate category options for each customer. 所以,我有一个数据库表,每个客户最多有8个单独的类别选项。
Example: 例:
company_name | category_1 | category_2 | category_3
****************************************************
My Company | computers | parts | electronics
ect... up on up to eight category options. 最多八个类别选项。 What I need to do is get the categories in a list and list all companies with that category under each category item.
我需要做的是获取列表中的类别,并列出每个类别项下具有该类别的所有公司。 I have the categories into an array, but I get all of them in a foreach loop which will give me duplicates.
我将类别放入一个数组中,但是我将它们全部放在foreach循环中,这将给我重复。 I don't want to list the duplicates, I just want to list them once and place all companies under that category.
我不想列出重复项,我只想列出一次并将所有公司列入该类别。
Like: 喜欢:
Computers 电脑
Company Name 公司名
Parts 部分
Company Name 公司名
Electronics 电子产品
Company Name 公司名
ect.... ECT ....
My code currently: 我的代码目前:
$sql = $wpdb->get_results( "SELECT * FROM $table_name");
echo '<ul>';
foreach ($sql as $cat){
$cats[0] = $cat->category_1.' '.$cat->category_2.' '.$cat->category_3.' '.$cat->category_4.' '.$cat->category_5.' '.$cat->category_6.' '.$cat->category_7.' '.$cat->category_8;
$totalCats = $cats[0];
echo '<li>'.$totalCats.'</li>';
}
echo '</ul>';
}// End of foreach loop
This will then give me the following: 这将给我以下内容:
ect... for each database entry depending on how many categories that company chose. ect ...对于每个数据库条目,取决于公司选择的类别数量。
Any help would be appreciated! 任何帮助,将不胜感激!
I don't have a dataset to test it but this should work. 我没有测试它的数据集,但这应该工作。 We create a multidimensional array from the dataset like this:
我们从数据集创建一个多维数组,如下所示:
[comp][0] = company A
[1] = company B
...
[elec][0] = company A
[1] = company C
...
[part][0] = company Y
[1] = company Z
...
Then we iterate over it to print it out. 然后我们迭代它以打印出来。
<?php
$cats = array();
// loop through rows
foreach($sql as $cat) {
// loop through row categories
for($i=1; $i<=8; ++$i) {
// column name
$column = 'category_'.$i;
// column has data
// $cats[category][irrelevant index] = company name
if($cat->$column !== null && $cat->$column !== '') {
$cats[$cat->$column][] = $cat->company_name;
}
}
}
// sort categories
ksort($cats);
echo '<ul>';
// loop though categories
foreach($cats as $catname=>$cat) {
// sort companies in category
sort($cat);
// category name
echo '<li>'.$catname.'</li><ul>';
// loop through companies
foreach($cat as $company) {
echo '<li>'.$company.'</li>';
}
echo '</ul>';
}
echo '</ul>';
?>
It would probably be best to store categories in a separate table and start from there, using a JOIN
to retrieve associated company names. 最好将类别存储在单独的表中,然后从那里开始,使用
JOIN
检索关联的公司名称。 It would also require less code to print it out the way you want it. 它还需要更少的代码来按照您希望的方式打印它。
Changed column data testing to $cat->$column !== ''
将列数据测试更改为
$cat->$column !== ''
Changed column data testing to $cat->$column !== null && $cat->$column !== ''
based on wpdb results structure. 根据wpdb结果结构将列数据测试更改为
$cat->$column !== null && $cat->$column !== ''
。
I think you need to create separate table for categories 我认为你需要为类别创建单独的表
For eg:-
table companies
company_id | company_name
table categories
category_id | company_id | category_name
So your query will comes like
$query = "SELECT * FROM companies comp RIGHT JOIN categories cate ON comp.company_id=cate.company_id";
With this you will get company details and array of categories, hope this is the better way to do this 有了这个,您将获得公司详细信息和一系列类别,希望这是更好的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.