0

以下の表から、次のようにデータを取得するにはどうすればよいでしょうか。

Heading 1 Eg: Kitchenware
 Heading 2 Eg: Knives
  Heading 3 Eg: Butter Knives
   Item: Cut em all
   Item: Cull em all
   Item: Smear em all 
  Heading 3 Eg: Meat Knives
   Item: Cut em meat
   Item: Cull em meat
   Item: Smear em meat

レベル 1 と 2 は見出しであり、アイテムを保持できません。レベル3はアイテムを保持できます。レベル4はアイテムです。上記の対応は可能でしょうか。場合によっては、レベル 1 の後にレベル 3 が来ることがあります。

"id"    "name"         "description"               "level"  "parent"    "country"   "maxLevel"
"1"     "Kitchenware"   "Kitchenware description"   "1"       "0"         "US"        "0"
"2"     "Knives"        "All our knives"            "2"       "1"         "US"        "0"
"3"     "Butter Knives" "All Butter Knives"         "3"       "2"         "US"        "0"
"4"     "Cut em all"    "Cut em all"                "4"       "3"         "US"        "0"
"5"     "Cull em all"   "Cull em all"               "4"       "3"         "US"        "0"
"6"     "Smear em all"  "Smear em all"              "4"       "3"         "US"        "0"
"7"     "Meat Knives"   "All Meat Knives"           "3"       "2"         "US"        "0"
"8"     "Cut em meat"   "Cut em meat"               "4"       "7"         "US"        "0"
"9"     "Cull em meat"  "Cull em meat"              "4"       "7"         "US"        "0"
"10"    "Smear em meat" "Smear em meat"             "4"       "7"         "US"        "0"

テーブルの作成

CREATE TABLE `products` (
    `id` INT(10) NULL AUTO_INCREMENT,
    `name` VARCHAR(50) NULL DEFAULT NULL,
    `description` VARCHAR(240) NULL DEFAULT NULL,
    `level` TINYINT(1) NULL DEFAULT '0',
    `parent` INT(10) NULL DEFAULT '0',
    `country` VARCHAR(2) NULL DEFAULT NULL,
    `maxLevel` INT(1) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

表データ

INSERT IGNORE INTO `products` (`id`, `name`, `description`, `type`, `parent`, `country`, `maxLevel`) VALUES
    (1, 'Kitchenware', 'Kitchenware description', 1, 0, 'US', 0),
    (2, 'Knives', 'All our knives', 2, 1, 'US', 0),
    (3, 'Butter Knives', 'All Butter Knives', 3, 2, 'US', 0),
    (4, 'Cut em all', 'Cut em all', 4, 3, 'US', 0),
    (5, 'Cull em all', 'Cull em all', 4, 3, 'US', 0),
    (6, 'Smear em all', 'Smear em all', 4, 3, 'US', 0),
    (7, 'Meat Knives', 'All Meat Knives', 3, 2, 'US', 0),
    (8, 'Cut em meat', 'Cut em meat', 4, 7, 'US', 0),
    (9, 'Cull em meat', 'Cull em meat', 4, 7, 'US', 0),
    (10, 'Smear em meat', 'Smear em meat', 4, 7, 'US', 0);
4

3 に答える 3

0

このようなテーブルを作成できます

H1                     H2                 H3            Item:
Kitchenware          Knives            Butter Knives   Cut em all
Kitchenware          Knives            Meat Knives     Cut em all
Kitchenware          Knives            Butter Knives   Smear em meat

次に、に基づいて簡単に選択できますh1 or h2 or h3

そして、このようなテーブル構造。

CREATE TABLE `products` (
`id` INT(10) NULL AUTO_INCREMENT,
`h1` VARCHAR(50) NULL DEFAULT NULL,
`h2` VARCHAR(240) NULL DEFAULT NULL,
`h3` VARCHAR(240) NULL DEFAULT NULL,
`Item:` VARCHAR(240) NULL DEFAULT NULL,
`level` TINYINT(1) NULL DEFAULT '0',
`parent` INT(10) NULL DEFAULT '0',
`country` VARCHAR(2) NULL DEFAULT NULL,
`maxLevel` INT(1) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

SQlのようなものSELECT from table WHERE h1=somthing AND h3=somthing

于 2013-05-05T14:55:19.197 に答える
0

こんな感じに仕上げました。最高ではありませんが、機能します。彼らは、関数内から関数を呼び出すことは良い考えではないと言います。

<?php

$conn = connect();

$what = 1;

$stmt = $conn->prepare("select id as id, name as name, description as description from products where level = :what and country = 'US'");
$stmt->bindParam(':what', $what);
$stmt->execute();

$rows = $stmt->rowCount();

//echo 'Rows found '.$rows.' hey<br>';

while($row = $stmt->fetch())
{  
    echo $row['name']." ".$row['id'];
    echo "<br><br>";

    $next = $conn->prepare("select id as id, level as level, name as name, description as description from products where level > :what and country = 'US' and parent = :parent");
    $next->bindParam(':what', $row['id']);
    $next->bindParam(':parent', $row['id']);
    $next->execute();

    while($row = $next->fetch())
    {  
        if($row['level'] == '2')
        {
            echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>";
            fetchElements($row['level'],$row['id']);
        }else if($row['level'] == '3')
        {
            echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>";
            fetchElements($row['level'],$row['id']);
        }
    }
}

function fetchElements($one,$two)
{   
    global $conn; //I feel this is incorrect. $conn is defined up there once.
    $elements = $conn->prepare("select id as id, level as level, name as name, description as description from products where level > :what and parent = :parent and country = 'US'");
    $elements->bindParam(':what', $one);
    $elements->bindParam(':parent', $two);
    $elements->execute();

    while($row = $elements->fetch())
    {  
        echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>";
        if($one == 2)
        {
            fetchElements($row['level'],$row['id']);
        }
    }
    echo "<br>";
}
?>
于 2013-05-07T07:40:03.017 に答える
0

階層ツリーを構築するには、PHP などのサーバー コーディングを使用する必要があります。表示の代替手段は、レコード レベルに基づいてインデント文字を作成することです。

于 2013-05-05T14:46:24.443 に答える