【问题标题】:Echo array based on distinct key基于不同键的回显数组
【发布时间】:2015-02-11 21:50:39
【问题描述】:

我已将数据库“排序”为按国家升序排列,然后是年份降序排列。每个数据库记录包含:国家名称、年份、详细信息。有很多重复的国家,但年份不同。例如:

Albania,  2000, details  
Albania,  1965, details  
Croatia, 2014, details  
Croatia, 2003, details

无法弄清楚如何回显数组以获得如下结果,其中国家在一行中,年份和详细信息在下面列出,而不重复国家名称:

Albania  
    2000,  details  
    1965,  details  

Croatia  
    2014,  details  
    2003,  details  

似乎我需要针对每个不同的国家、回声年份和详细信息?

到目前为止,这是我的 php:

$result = mysql_query("SELECT country, year, details FROM studies ORDER BY country, year DESC ");  
    //output data from each row in db  
    while($row = mysql_fetch_array($result))  {  
echo "   Country:  " .$row['country']. "<br />      Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";  
}  

不胜感激,我很难过!

【问题讨论】:

  • 以后高亮代码块,按ctrl+k,就会格式化成代码。
  • GROUP BY country 应该这样做。
  • @Fred-ii-,GROUP BY 还不够; OP 需要按照描述格式化其输出。
  • 也许使用嵌套的while循环?首先获取不同的国家/地区名称,显示该值,然后在遍历国家/地区名称时,提取每个国家/地区的年份和其他详细信息?

标签: php mysql arrays database key-value


【解决方案1】:

尝试添加国家/地区检查:

$newcountry = '';
 while($row = mysql_fetch_array($result))  {
   if ($newcountry != $row['country']) {
     echo "Country:". $row['country']."<br />";
     $newcountry = $row['country'];
   }
  echo " Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";
}

这应该可行,因为您已按国家/地区对查询进行了排序。这很关键,否则您绝对应该在 SQL 中添加 GROUP BY 子句。

编辑:要在组周围添加&lt;div&gt;,您只需更改回显顺序,首先检查国家是否已设置一次。它看起来像:

 $newcountry = 'undefined';
     while($row = mysql_fetch_array($result))  {
       if ($newcountry !='undefined' && $newcountry != $row['country']){
         echo '</div>'; // only add a closing div if a new country (and not 1st)
       }
       if ($newcountry != $row['country']) {
         echo "Country:". $row['country']."<br /><div class='countryDetail'>";
         $newcountry = $row['country'];
       }// added the start of the <div>
      echo " Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";
    }
    if ($newcountry != 'undefined') { //make sure at least one <div> is set
      echo "</div>"; // close the last <div>
    }

我在 div 中添加了 countryDetail 类,因此您可以在 jQuery 中将其与 toggle 一起使用。

【讨论】:

  • 小改进:$newcountry = $row['country'];可以放在if 块中以避免无效分配。更改 $newcountry 只有在不同时才有意义。
  • 这很完美!但是,为简单起见,我没有包括 html 格式要求。无法使用
    来围绕国家/地区的每组年份和详细信息。我需要它在国家旁边用箭头显示/隐藏。我知道 jquery 适用于纯 html。无论我将
    放在 echo 语句中的哪个位置,它都围绕着每一年和细节。谢谢,我快到了。
  • 已根据您的
    请求进行了修改。您也可以使用 jQuery 进行包装,但我认为这更易于管理。
  • 非常感谢您,这很完美!!详细信息现在包含在
    中,并且 jquery 切换效果很好。
【解决方案2】:

您可以使用嵌套的 while 循环。您可能还想使用 PDO/mysqli_ 函数/准备语句代替 mysql_ 函数:

// get unique country list first
$sql1 = "SELECT DISTINCT(country) FROM studies ORDER BY country";
$result1 = mysql_query($sql1);

// iterate through result set of sql1
while($row1 = mysql_fetch_array($result1))   
{
     $country = $row1['country'];
     echo "<br>";      // new line
     echo $country;      

     // get year, details for each country
     $sql2 = "SELECT year, details FROM studies WHERE country = '$country' ORDER BY year DESC";
     $result2 = mysql_query($sql2);
     // iterate through result set of $sql2 
     while ($row2 = mysql_fetch_array($result2))
     {
          echo "<br>";      // new line
          echo $row2['year']. ", " . $row2['details']; 
     }
}

【讨论】:

    【解决方案3】:

    循环派对!

    $rows = [];
    while($row = mysql_fetch_array($result))  {
        $rows[ $row['country'] ] = [ $row['year'], $row['details'] ];
    }
    
    foreach($rows as $country => $row) {
      $details = array_map(function($items) { return sprintf("\t - %s: %s\n", $items[0], $items[1]) }, $row);
      echo sprintf("\n%s:\n %s", $country, $details);
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签