我(目前)有 4 个颜色栏:
color1, color2, color3, color4
我希望下面的内容能够为填充的每个“color%”列回显一个值。
即,如果 color1 = Red,color2 = Green,color3 = ,color4 = ,那么我希望它输出“颜色:红色,绿色”
目前不输出
$query = mysqli_query($conn,"SELECT * FROM Single Where weave LIKE '$weave' AND pattern LIKE '$pattern' AND series LIKE '$series' AND (color1 LIKE '$color1' or color2 LIKE '$color1' OR color3 LIKE '$color1' OR color4 LIKE '$color1') AND (color1 LIKE '$color2' or color2 LIKE '$color2' OR color3 LIKE '$color2' OR color4 LIKE '$color2') AND type LIKE '$type' ORDER BY name");
$i=0;
while($row = $query->fetch_assoc())
{
echo "<figure class='item'>";
echo "<figcaption class='caption'><H1>$row[name]</H1></figcaption>";
echo "<a href='$row[image]'><img src='$row[image]'/><a>";
echo "<figcaption class='caption'>Weave: $row[weave]</figcaption>";
echo "<figcaption class='caption'>SWG: $row[gauge]   ID: $row[innerdiameter]</figcaption>";
echo "<figcaption class='caption'>Colors:";
while (strpos($key, "color") !== FALSE)
{
echo "$row[color%]";
}
echo "</figcaption>";
echo "<figcaption class='caption'>Color Pattern: $row[pattern]</figcaption>";
echo "<figcaption class='caption'><br></figcaption>";
echo "<figcaption class='caption'>$row[description]</figcaption>";
echo "</figure>";
}
我尝试过 while、foreach 和 if 语句。所有的语句都没有输出。
这将查找数据库结果中以“颜色”开头并以数字结尾的任何列,忽略它找到的任何空白。它假设颜色从 1 开始,并且最大值不超过表中的列数。如果您从 100 开始,或者跳过 20(例如在此数据集中),则它将不起作用。
<?php
$rowFromDatabase = array('name'=>'test', 'color1'=>'Red', 'color2'=>'Green', 'color3'=>'Blue', 'color4'=>'Orange');
$foundColors = array();
for($i=1; $i<=count($rowFromDatabase); $i++){
$foundColors[] = $rowFromDatabase['color'.$i] ?? '';
}
echo implode(', ',array_filter($foundColors));
输出
Red, Green, Blue, Orange