2017-03-29 18 views
0

私はuniのための小さなWebアプリケーションを開発しています。要件の1つは、データベースからのデータを持つリストを持っていなければならず、そのカテゴリの項目のみが表示されます。 データベースからWebサイトのテーブルにデータを取得することができましたが、表示するカテゴリを選択するドロップダウンメニューは機能しません。それは、何に関係なくデータベースのすべてのアイテムを表示することを意味します。私は何の誤りもなく、この問題をどのように解決できるか分かりません。 うまくいけば、誰かが私にこれを手伝ってくれるだろう。そのカテゴリからのみの表示項目になっている機能のための コード:選択したデータをMySQLから取得する

//Get ItemEntity objects from the database and return them in an array. 
function GetItemByCategory($category) { 
    // require 'dbconnect.php'; 

    //Open connection and Select database.  
    $con = mysqli_connect("localhost", "root", "", "login") or die(mysqli_error); 

    $query = "SELECT * FROM items WHERE category LIKE '$category'"; 
    $result = mysqli_query($con, $query) or die(mysqli_error($con)); 
    $itemArray = array(); 

    //Get data from database. 
    while ($row = mysqli_fetch_array($result)) { 

     $name = $row[1]; 
     $category = $row[2]; 
     $place = $row[3]; 
     $date = $row[4]; 
     $description = $row[5]; 
     $image = $row[6]; 

     //Create item objects and store them in an array. 
     $item = new ItemEntity(-1, $name, $category, $place, $date, $description, $image); 
     array_push($itemArray, $item); 
    } 
    //Close connection and return result 
    mysqli_close($con); 
    return $itemArray; 
} 

} 

は、私はそれが簡単な修正であるかもしれないが、私が言ったように、私はそれが理由ですので迷ってしまいました、まだこれにはかなり新しいです私は手を伸ばした。 あらかじめありがとうございます。私からのコードが必要な場合はお知らせください。

//編集 データを表示するHTML形式。他に必要なものがあれば教えてください。ご協力いただきありがとうございます。

function CreateItemTables($categories) 
    { 
     $itemModel = new ItemModel(); 
     $itemArray = $itemModel->GetItemByCategory($categories); 
     $result = ""; 

     // Generate an item table for each item entity in the array 
     foreach ($itemArray as $key => $item) { 
      $result = $result . "<table class = 'itemTable'> 
           <tr> 
            <th rowspan='6' width = '150px' ><img runat = 'server' src = '$item->image'/></th> 
            <th width = '75px' >Name: </th> 
            <td>$item->name</td> 
           </tr> 

           <tr> 
            <th>Category: </th> 
            <td>$item->category</td> 
           </tr> 

           <tr> 
            <th>Place: </th> 
            <td>$item->place</td> 
           </tr> 

           <tr> 
            <th>Date: </th> 
            <td>$item->date</td> 
           </tr> 

           <tr> 
            <td colspan='2' >$item->description</td> 
           </tr> 


           </table>"; 
     } 
     return $result; 
    } 

//エディット2ホールItemControllerクラス

に "カテゴリLIKE '$カテゴリ' を" あなたのDBはどのように見えるかが、あなたは何を変更した場合、私は本当に知らない
<?php 

require ("Model/ItemModel.php"); 

/** 
* Contains non-db related functions for the Item page 
*/ 
class ItemController 
{ 

    function CreateItemDropdownList() 
    { 
     $itemModel = new ItemModel(); 
     $result = "<form action = '' method = 'post' width = '200px'> 
        Please select a category: <select name = 'types' > 
        <option value = '%' >ALL</option>".$this->CreateOptionValues($itemModel->GetCategories())." 
        </select> 
        <input type = 'submit' value = 'Search' /></form>"; 

       return $result; 
    } 
    function CreateOptionValues(array $valueArray) 
    { 
     $result = ""; 

     foreach ($valueArray as $value) 
     { 
      $result = $result . "<option value ='$value'>$value</option>"; 
     } 
     return $result; 
    } 

    function CreateItemTables($categories) 
    { 
     $itemModel = new ItemModel(); 
     $itemArray = $itemModel->GetItemByCategory($categories); 
     $result = ""; 

     // Generate an item table for each item entity in the array 
     foreach ($itemArray as $key => $item) { 
      $result = $result . "<table class = 'itemTable'> 
           <tr> 
            <th rowspan='6' width = '150px' ><img runat = 'server' src = '$item->image'/></th> 
            <th width = '75px' >Name: </th> 
            <td>$item->name</td> 
           </tr> 

           <tr> 
            <th>Category: </th> 
            <td>$item->category</td> 
           </tr> 

           <tr> 
            <th>Place: </th> 
            <td>$item->place</td> 
           </tr> 

           <tr> 
            <th>Date: </th> 
            <td>$item->date</td> 
           </tr> 

           <tr> 
            <td colspan='2' >$item->description</td> 
           </tr> 


           </table>"; 
     } 
     return $result; 
    } 
} 
?> 
+0

「カテゴリ= '$ category' "? –

+0

いくつかの自己デバッグを試みてください。関数が正常に見えるので、関数が指定されたカテゴリを受け取っていることを確認してください。 – Hienz

+0

コードをデバッグします。変数を出力して$ query変数にどのクエリを取得しているのかを調べます。 –

答えて

0
//changed like to = more accurate 
$query = "SELECT * FROM items WHERE category = '$category'"; 
    $result = mysqli_query($con, $query) or die(mysqli_error($con)); 
    $itemArray = array(); 

    //Get data from database. 
//note this 
$i=0; 

while ($row = mysqli_fetch_array($result)) { 
     //gave then a record number so you get the right data 
     $name = $row[$i]; 
     $category = $row[$i]; 
     $place = $row[$i]; 
     $date = $row[$i]; 
     $description = $row[$i]; 
     $image = $row[$i]; 
$item = new ItemEntity($i, $name, $category, $place, $date, $description, $image); 
$i++; 
+0

は、$ row [1] $ row [2] ..... whileループで正しいデータを得ることはできません – Gert

+0

あなたのループを使用することを提案していただき、ありがとうございます。私は、これは、このデータを取得するHTMLフォームメソッドに問題があることを意味すると思いますか? (上に掲載) – Frenzyy

+0

行['1']行['2']を使用すると、1組のデータしか得られません。異なる行からでもそれを使用することはできません。 htmlになる前にまずPHPをチェックしてください。ダムまたはエコー結果が配列と同じになる場合 – Gert

関連する問題