MySQL:我将如何为此显示简单的“类别”?

时间:2023-01-27 11:21:28

I'm wishing for a simple index more or less with my PHP note taking software, each note has a "language" field for the programming language its written in. When I iterate through each entry in a while loop, I'm not sure how i'd list categories of them such as:

我希望用我的PHP笔记软件或多或少的简单索引,每个笔记都有一个“语言”字段,用于编写其编写的语言。当我在while循环中遍历每个条目时,我不确定我如何列出它们的类别,例如:

PHP:
  Note 1
  Note 5
SQL: 
  Note 2
  Note 3

Do I need to place all entries in a temporary array with ifs, like this?

我是否需要将所有条目放在ifs的临时数组中,如下所示?

if($field['language'] == "PHP")
    $PHPsection[] = $field;

Although that doesn't look the best as I'd need to hardcode each section.. I'm stuck.

虽然这看起来并不是最好的,因为我需要对每个部分进行硬编码。我被卡住了。

2 个解决方案

#1


2  

you could group already in the database. or sort by section and then iterate through and output the proper seperation code when the section changes. a temporary array is also a solution. given your example code maby this can point you in one possible directino (altough the temporary array is of course not an elegant solution but if the data is not that much it doesnt really matter)

你可以在数据库中进行分组。或按部分排序,然后迭代并在部分更改时输出正确的分隔代码。临时数组也是一种解决方案。鉴于您的示例代码maby,这可以指向一个可能的directino(尽管临时数组当然不是一个优雅的解决方案,但如果数据不是那么多它并不重要)

<?
$result = array();
$result[] = array('section' => 'php','note' => 'bla');
$result[] = array('section' => 'php','note' => 'bla');
$result[] = array('section' => 'perl','note' => 'bla');
$result[] = array('section' => 'java','note' => 'bla');

$grouped = array();
for($i=0;$i<count($result);$i++) {
  $grouped[$result[$i]['section']][] = $result[$i]['note'];
}
print_r($grouped);

?>

#2


1  

Sounds like you need to do a lookup table here. Such as the following.

听起来你需要在这里做一个查找表。如下。

Note_id | Language_id 
--------+------------
 1      |   1
 1      |   4
 2      |   3 
 3      |   1

This, then, links these two tables

然后,这将链接这两个表

Note_id | Note_Name| ...
--------+----------+----
    1   | Testing  | ...
    2   | Groceries| ...

Language_ID | Language
------------+---------
    1       | English
    2       | German
    3       | Klingon

And from there, you can write queries like:

从那里,您可以编写如下查询:

SELECT
   Note_name, Language
FROM
   languages l, notes n, language_note_lookup lnl
WHERE
   l.Language_ID = lnl.Language_ID AND
   n.Note_ID = n.Note_ID AND
   l.Language = "German"

You can also get a list of all languages easily by simply doing a

您还可以通过简单地获取所有语言的列表

SELECT Language FROM languages

#1


2  

you could group already in the database. or sort by section and then iterate through and output the proper seperation code when the section changes. a temporary array is also a solution. given your example code maby this can point you in one possible directino (altough the temporary array is of course not an elegant solution but if the data is not that much it doesnt really matter)

你可以在数据库中进行分组。或按部分排序,然后迭代并在部分更改时输出正确的分隔代码。临时数组也是一种解决方案。鉴于您的示例代码maby,这可以指向一个可能的directino(尽管临时数组当然不是一个优雅的解决方案,但如果数据不是那么多它并不重要)

<?
$result = array();
$result[] = array('section' => 'php','note' => 'bla');
$result[] = array('section' => 'php','note' => 'bla');
$result[] = array('section' => 'perl','note' => 'bla');
$result[] = array('section' => 'java','note' => 'bla');

$grouped = array();
for($i=0;$i<count($result);$i++) {
  $grouped[$result[$i]['section']][] = $result[$i]['note'];
}
print_r($grouped);

?>

#2


1  

Sounds like you need to do a lookup table here. Such as the following.

听起来你需要在这里做一个查找表。如下。

Note_id | Language_id 
--------+------------
 1      |   1
 1      |   4
 2      |   3 
 3      |   1

This, then, links these two tables

然后,这将链接这两个表

Note_id | Note_Name| ...
--------+----------+----
    1   | Testing  | ...
    2   | Groceries| ...

Language_ID | Language
------------+---------
    1       | English
    2       | German
    3       | Klingon

And from there, you can write queries like:

从那里,您可以编写如下查询:

SELECT
   Note_name, Language
FROM
   languages l, notes n, language_note_lookup lnl
WHERE
   l.Language_ID = lnl.Language_ID AND
   n.Note_ID = n.Note_ID AND
   l.Language = "German"

You can also get a list of all languages easily by simply doing a

您还可以通过简单地获取所有语言的列表

SELECT Language FROM languages