使用php按类别显示来自Mysql数据库的所有数据

时间:2014-09-05 15:49:58

标签: php html

目前在我的php代码中,我从mysql表中获取所有keywords并在Html td列上显示它。它看起来像下图中的那个。

enter image description here

每个关键字都有唯一的关键字类型。例如,它可能包含 Web开发关键字类型下的php,html css,js。

所以现在,我只打印所有关键字,而不是关键字类型

我需要在每个关键字类型下显示关键字。所以看起来应该是这样的:

Keyword Type1     Keyword Type2    Keyword Type3   Keyword Type4  and so on...           
html              wp               js              php 
mysql             html5            jquery          c
css               css3             xml             c++

我怎样才能在php中显示这个?

我现有的PHP代码如下:

<?php
$query =  mysql_query("SELECT keywords . *, keyword_type . * FROM keywords LEFT JOIN keyword_type ON keywords.keyword_typeID = keyword_type.keyword_typeID  ORDER BY kid DESC");
echo "<div id='keywordBox'>";

echo '<table border="0" cellpadding="0" cellspacing="0" width="1055">';
$count = 0;
while($result =  mysql_fetch_array($query)){

  if($count%6==0 && $count!=0){    
    echo "</tr><tr>";
  }elseif($count==0)
    echo "<tr>";

    $kid = $result['kid'];
    $keywordName = ucfirst($result['keywordName']);
    $keyword_short_name = ucfirst($result['keyword_short_name']); // keyword type
    $keyword_full_name = ucfirst($result['keyword_full_name']);  // keyword type full name 

    $query2 =  mysql_query("SELECT kid FROM userkeywords WHERE cdid = '$cdid' AND kid='$kid'");
    $num=mysql_num_rows($query2);

    if($num > 0){
        $class = "keywordHigligh";  
    }else{
        $class = "";
    }

    echo "<input type='hidden' id='kid' value='$kid'/>";    
    echo "<input type='hidden' id='cdid' value='$cdid'/>";  
    echo "<td width='400' onclick='keywordclick($kid,$cdid)' class='$class'><strong>$keywordName</td>";

    $count++;  
}

echo '</table>';
echo "</div>";
echo "<a href='../index.php?redcdid=$cdid'><input type='submit' value='Save' class='submit' /></a>"
?>

注意:

$keyword_short_name = ucfirst($result['keyword_short_name']); // keyword type
$keyword_full_name = ucfirst($result['keyword_full_name']);  // keyword type full name 

感谢您的建议和帮助。 --Shibbir

1 个答案:

答案 0 :(得分:0)

尝试将转换执行到另一个数组上,如下所示:

$output = array();
foreach($keywords as $keyword) {
    $output[$keyword["type"]][] = $keyword["name"];
}

var_dump($output);

[编辑] 我很无聊,所以我写了这个。 如果您想要我澄清一些内容,请注释,但请告诉我。

<?php

$keywords = array(
    array("name" => "html", "type" => "front-end"),
    array("name" => "css", "type" => "front-end"),
    array("name" => "css", "type" => "front-end"),
    array("name" => "php", "type" => "back-end"),
    array("name" => "java", "type" => "back-end"),
    array("name" => "analytics", "type" => "marketing"),
    array("name" => "algebra", "type" => "maths"),
);

// Here we're looking to go from the above format to something like this:
// array("front-end" => array("html", "css"), "back-end" => array("php", "java") ...)
$keywordTypes = array();
foreach($keywords as $keyword) {
    $keywordTypes[$keyword["type"]][] = $keyword["name"];
}

// Not every keyword type will have the same number of keywords so we need a height that we will all fill them to
// array_map will create an array with a count for every keyword type and max will take the longest.
$nbRows = max(array_map("count", $keywordTypes));
?>
<table>
<thead>
    <tr>
        <!-- We iterate over all the keyword types in order to write our table headings -->
        <?php foreach($keywordTypes as $keywordType => $relatedKeywords): ?>
        <th><?php echo $keywordType ?></th>
        <?php endforeach; ?>
    </tr>
</thead>
<tbody>
    <!-- We know how many rows we are working with so let's use a for loop -->
    <?php for($i = 0; $i < $nbRows; ++$i): ?>
    <tr>
        <!-- This foreach will display one of each keyword type as we are writing row by row -->
        <?php foreach($keywordTypes as $keywords): ?>
            <!-- If we do not have a keyword, let's still add a placeholder so the table looks right -->
            <td><?php if(isset($keywords[$i])) { echo $keywords[$i]; } ?></td>
        <?php endforeach; ?>
    </tr>
    <?php endfor; ?>
</tbody>
</table>