0

次のようなデータベースにテーブルがあります。

ID, features, Values : ID は 1、2、3.... 特徴は色、サイズ、素材.... 値は赤、13 インチ、シリコン... になります。

私は次のように保存しました。

ID

1
1
1

特徴
カラー
サイズ
素材

値赤13
インチシリコン


そして、製品 2 に続きます... . 取得していない表形式で表示しようとしています。

実際には、id -2 が来ると次の行に移動する必要があります...しかし、同じ行に表示され続けます....

誰かがそれを行う方法を教えてもらえますか?

私はこのようにしてみました

echo "<table width='100%'>";
echo "<tr><th>color</th>
<th>size</th>
<th>material</th></tr>";
echo "<tr>";
while ($row = mysql_fetch_assoc($result)) {

    echo "<td>".$row['values']."</td>";


} 
echo "</tr>";
echo "</table>";

編集済み* *

これは私の完全なコードです

if(isset($_POST['submit']))

{
$id = $_POST['id'];

$test = "SELECT id, features, values FROM mytable WHERE features IN ('color', 'size', 'material') AND id IN (" . implode(',',$id) .")";
$result = mysql_query($test) or die (mysql_error());
echo "<table width='100%'>";
echo "<tr><th>color</th>
<th>size</th>
<th>material</th></tr>";
echo "<tr>";
while ($row = mysql_fetch_assoc($result)) {

    echo "<td>".$row['values']."</td>";


} 
echo "</tr>";
echo "</table>";
}

との出力print_r(mysql_fetch_assoc($result));

Array ([id] => 1 [features] => color [values] => red )

4

1 に答える 1

0

これで問題は解決すると思います:

$table_markup = '<tr>';
$current_row = null;

while ( $row = mysql_fetch_assoc($result) ) {

    if ( $current_row === null ) {
        $current_row = $row['id'];
    }

    if ( $current_row != $row['id'] ) {
        $current_row = $row['id'];
        $table_markup .= '</tr><tr>';
    }

    $table_markup .=  '<td>' . $row['values'] . '</td>';
}

/* cutting off the last opened-TR tage */
$table_markup .= substr($table_markup, 0, -4);

echo $table_markup;
于 2013-09-27T11:29:31.057 に答える