删除重复项并显示最大计数
Remove duplication and show max count
我有下面的代码,但我的问题是我收到了 PHP 通知:未定义的偏移量:(从 1 到 7)
error message
对我来说非常重要的是只获得最大数量的国家 ID 一次。
它计算项目的数量并每次增加一个正确的和我需要的项目,但只需要显示一次
how does it display
正确的结果显示类似于:
the correct result
这是我的代码:-
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$count_country =0;
$country_count_each=0;
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) || !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
$country_count_each = $project['Project']['country_id'];
if($project['Project']['country_id']==$country_count_each){
$count_country+=$country_count_each;
$ids[]=$country_id;
//echo $country_counts[$country_id];
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
}
}else {
$country_counts[$country_id] =$country_id;
}
}
$proects_num = count($projects);
?>
</table>
<?php
echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>';
echo $html->link('Home ', $this->webroot.'countries/index/');
?>
数据库中的table
Table of projects
我需要帮助
你的问题在于你试图在同一个循环中循环、计数和回显 table...你不能:)
您需要在输出前准备好数据
您需要事先知道每个国家/地区代码出现的次数(在开始 table 输出循环之前....)。
添加另一个循环来准备数据是一个简单的选择。
我创建了一个代码来解释你的代码(在开始时创建了一些数据):
在这个例子中我又添加了一个循环...现在我们有两个。
<?php
$projects['Project'][0]['id'] = 1;
$projects['Project'][0]['project_name'] = "musem something";
$projects['Project'][0]['project_price'] = 10;
$projects['Project'][0]['project_deduction'] = 30;
$projects['Project'][0]['country_id'] = 1;
$projects['Project'][1]['id'] = 2;
$projects['Project'][1]['project_name'] = "musem nid";
$projects['Project'][1]['project_price'] = 5;
$projects['Project'][1]['project_deduction'] = 25;
$projects['Project'][1]['country_id'] = 1;
$projects['Project'][2]['id'] = 3;
$projects['Project'][2]['project_name'] = "khalefa tower";
$projects['Project'][2]['project_price'] = 20;
$projects['Project'][2]['project_deduction'] = 50;
$projects['Project'][2]['country_id'] = 2;
$projects['Project'][3]['id'] = 4;
$projects['Project'][3]['project_name'] = "gold market";
$projects['Project'][3]['project_price'] = 15;
$projects['Project'][3]['project_deduction'] = 40;
$projects['Project'][3]['country_id'] = 2;
/*Country names*/
$cNames[1] = "Holland";
$cNames[2] = "Dubai";
echo '<pre>';
print_r($projects); /* print the array */
$country_counts=[];
$count_country =0;
$country_count_each=0;
$ids=array();// Store unique country_id values here
?><table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr><?php
/* Loop to create table data */
foreach( $projects['Project'] as $p ) {
/* create new table data array with the country code as its key */
if(!isset($tableArray[$p['country_id']])){
$tableArray[$p['country_id']]['count'] = 1;
} else {
++$tableArray[$p['country_id']]['count'];
}
}
print_r($tableArray); /* print table data */
/* Loop to create table */
foreach($tableArray as $k => $line){
?>
<tr>
<td style="width: 30%"><?php echo $k; ?></td>
<td style="width: 30%"><?php echo $cNames[$k] ?></td>
<td style="width: 30%"><?php echo $line['count']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
$proects_num = count($projects['Project']);
echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>';
echo '<br>' .'Total countries are: ' . count($tableArray) .'<br>';
echo $html->link('Home ', $this->webroot.'countries/index/');
?>
将输出:
Array
(
[Project] => Array
(
[0] => Array
(
[id] => 1
[project_name] => musem something
[project_price] => 10
[project_deduction] => 30
[country_id] => 1
)
[1] => Array
(
[id] => 2
[project_name] => musem nid
[project_price] => 5
[project_deduction] => 25
[country_id] => 1
)
[2] => Array
(
[id] => 3
[project_name] => khalefa tower
[project_price] => 20
[project_deduction] => 50
[country_id] => 2
)
[3] => Array
(
[id] => 4
[project_name] => gold market
[project_price] => 15
[project_deduction] => 40
[country_id] => 2
)
)
)
Array
(
[1] => Array
(
[count] => 2
)
[2] => Array
(
[count] => 2
)
)
Country ID Country Name Number of Place
1 Holland 2
2 Dubai 2
Total projects numbers are: 4
Total countries are: 2
我有下面的代码,但我的问题是我收到了 PHP 通知:未定义的偏移量:(从 1 到 7) error message 对我来说非常重要的是只获得最大数量的国家 ID 一次。 它计算项目的数量并每次增加一个正确的和我需要的项目,但只需要显示一次 how does it display 正确的结果显示类似于: the correct result
这是我的代码:-
<table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr>
<?php
$country_counts=[];
$count_country =0;
$country_count_each=0;
$ids=array();// Store unique country_id values here
foreach( $projects as $project ) {
$country_id = $project['Project']['country_id'];
# Check if the country_id is NOT in the array and display if OK.
if( isset( $country_counts[ $country_id ] ) || !in_array( $country_id, $ids ) ) {
$country_counts[$country_id]++;
$country_count_each = $project['Project']['country_id'];
if($project['Project']['country_id']==$country_count_each){
$count_country+=$country_count_each;
$ids[]=$country_id;
//echo $country_counts[$country_id];
?>
<tr>
<td style="width: 30%"><?php echo $project['Project']['country_id']; ?></td>
<td style="width: 30%"><?php echo 'Country Name'; ?></td>
<td style="width: 30%"><?php echo $country_counts[$project['Project']['country_id']]; ?></td>
</tr>
<?php
}
}else {
$country_counts[$country_id] =$country_id;
}
}
$proects_num = count($projects);
?>
</table>
<?php
echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>';
echo $html->link('Home ', $this->webroot.'countries/index/');
?>
数据库中的table Table of projects
我需要帮助
你的问题在于你试图在同一个循环中循环、计数和回显 table...你不能:)
您需要在输出前准备好数据
您需要事先知道每个国家/地区代码出现的次数(在开始 table 输出循环之前....)。
添加另一个循环来准备数据是一个简单的选择。
我创建了一个代码来解释你的代码(在开始时创建了一些数据): 在这个例子中我又添加了一个循环...现在我们有两个。
<?php
$projects['Project'][0]['id'] = 1;
$projects['Project'][0]['project_name'] = "musem something";
$projects['Project'][0]['project_price'] = 10;
$projects['Project'][0]['project_deduction'] = 30;
$projects['Project'][0]['country_id'] = 1;
$projects['Project'][1]['id'] = 2;
$projects['Project'][1]['project_name'] = "musem nid";
$projects['Project'][1]['project_price'] = 5;
$projects['Project'][1]['project_deduction'] = 25;
$projects['Project'][1]['country_id'] = 1;
$projects['Project'][2]['id'] = 3;
$projects['Project'][2]['project_name'] = "khalefa tower";
$projects['Project'][2]['project_price'] = 20;
$projects['Project'][2]['project_deduction'] = 50;
$projects['Project'][2]['country_id'] = 2;
$projects['Project'][3]['id'] = 4;
$projects['Project'][3]['project_name'] = "gold market";
$projects['Project'][3]['project_price'] = 15;
$projects['Project'][3]['project_deduction'] = 40;
$projects['Project'][3]['country_id'] = 2;
/*Country names*/
$cNames[1] = "Holland";
$cNames[2] = "Dubai";
echo '<pre>';
print_r($projects); /* print the array */
$country_counts=[];
$count_country =0;
$country_count_each=0;
$ids=array();// Store unique country_id values here
?><table>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Number of Place</th>
</tr><?php
/* Loop to create table data */
foreach( $projects['Project'] as $p ) {
/* create new table data array with the country code as its key */
if(!isset($tableArray[$p['country_id']])){
$tableArray[$p['country_id']]['count'] = 1;
} else {
++$tableArray[$p['country_id']]['count'];
}
}
print_r($tableArray); /* print table data */
/* Loop to create table */
foreach($tableArray as $k => $line){
?>
<tr>
<td style="width: 30%"><?php echo $k; ?></td>
<td style="width: 30%"><?php echo $cNames[$k] ?></td>
<td style="width: 30%"><?php echo $line['count']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
$proects_num = count($projects['Project']);
echo '<br>' .'Total projects numbers are: ' . $proects_num .'<br>';
echo '<br>' .'Total countries are: ' . count($tableArray) .'<br>';
echo $html->link('Home ', $this->webroot.'countries/index/');
?>
将输出:
Array
(
[Project] => Array
(
[0] => Array
(
[id] => 1
[project_name] => musem something
[project_price] => 10
[project_deduction] => 30
[country_id] => 1
)
[1] => Array
(
[id] => 2
[project_name] => musem nid
[project_price] => 5
[project_deduction] => 25
[country_id] => 1
)
[2] => Array
(
[id] => 3
[project_name] => khalefa tower
[project_price] => 20
[project_deduction] => 50
[country_id] => 2
)
[3] => Array
(
[id] => 4
[project_name] => gold market
[project_price] => 15
[project_deduction] => 40
[country_id] => 2
)
)
)
Array
(
[1] => Array
(
[count] => 2
)
[2] => Array
(
[count] => 2
)
)
Country ID Country Name Number of Place
1 Holland 2
2 Dubai 2
Total projects numbers are: 4
Total countries are: 2