我想我可能已经掌握了。我所拥有的是mysql中的两个带有公共字段的表。我希望在一个表格中显示一条记录,其中包含公共字段对其共有的所有记录的行或列表。
详细地说,我有一张带有图像的纱线表和带有图像的图案表。我喜欢在一行中显示图案及其信息,然后在其下面显示与各种子行中的公共重量字段匹配的所有纱线图像。
我还通过复选框组选择图案的颜色和类别。
这是我的复选框组
<input type="checkbox" name="color[]" value="Black">Black
<input type="checkbox" name="color[]" value="Blue">Blue
<input type="checkbox" name="color[]" value="Brown">Brown
<input type="checkbox" name="color[]" value="Green">Green
<input type="checkbox" name="color[]" value="Grey">Grey
<input type="checkbox" name="color[]" value="Orange">Orange
<input type="checkbox" name="color[]" value="Pink">Pink
<input type="checkbox" name="color[]" value="Purple">Purple
<input type="checkbox" name="color[]" value="Red">Red
<input type="checkbox" name="color[]" value="Teal">Teal
<input type="checkbox" name="color[]" value="White">White
<input type="checkbox" name="color[]" value="Yellow">Yellow
<input type="checkbox" name="pattcat[]" value="Home Decor">Home Decor
<input type="checkbox" name="pattcat[]" value="Children">Children
<input type="checkbox" name="pattcat[]" value="Tops Women">Tops Women
<input type="checkbox" name="pattcat[]" value="Accessories">Accessories
<input type="checkbox" name="pattcat[]" value="Lace">Lace
<input type="checkbox" name="pattcat[]" value="Tops Men">Tops Men
这是我的查询和显示表到目前为止的结果。我将所有图像名称保存在mysql表中并链接到路径。
extract($_GET);
$query = "(SELECT tblPatterns.PatternName, tblPatterns.Needlesize, tblPatterns.FinishedSize, tblPatterns.YardageNeeded, tblPatterns.PatternImage, tblYarn.YarnImage FROM tblYarn, tblPatterns WHERE tblYarn.WeightCategory=tblPatterns.WeightCategory && Color IN ('" . implode("','",$color) . "') && Category IN ('" . implode("','",$pattcat) . "') ORDER BY `tblPatterns`.`PatternName` ASC)" ;
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table border='1'>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "<td><img src=./images/$col_value width='150' height='150' border='3' ></td>";
echo "\t</tr>\n";
}
echo "</table>\n";
我想我可能不得不改变foreach($ line as $ col_value)部分。有没有办法建立一个表,并准确选择哪些数据在哪里以及是否重复?如在来自在行下重复的纱线的图像及其相应的图案数据?
答案 0 :(得分:0)
您需要使用tblYarn加入tblPatterns:)
http://www.tizag.com/mysqlTutorial/mysqljoins.php
此外,图像TD在foreach之外,因此它不会在其前面带有$ col_value循环。
答案 1 :(得分:0)
我认为这就是你想要的:
$last_pattern = null;
while ($line = mysql_fetch_assoc($result, MYSQL_ASSOC)) {
$col_count = count($line) - 1; // Subtract 1 because image isn't in a column
if ($line['PatternName'] !== $last_pattern) {
if ($last_pattern !== null) {
echo "</td></tr>\n";
}
echo "<tr>\n ";
foreach ($line as $col_name => $col_value) {
switch ($col_name) {
case 'YarnImage':
break;
case 'PatternImage':
echo "<td><image src='images/$col_value' /></td>";
break;
default:
echo "<td>$col_value</td>";
}
}
$last_pattern = $line['PatternName'];
echo "</tr>\n <tr><td colspan='$col_count'>\n";
}
$yarn_image = $line['YarnImage'];
echo " <img src='images/$yarn_image' width='150' height='150' border='3'/>\n";
}
if ($last_image !== null) {
echo "</td></tr>";
}