0

我在让我的图像从我的 mySQL 数据库中正确排列时遇到问题。我遇到的问题是,我基本上想从 CMS 加载此页面上的 24 张图像。我将图像加载到一个设计为每行有 4 个图像的表格中。问题:第一行有 2 个图像,第二行有 1 个图像,第三行有 3 个图像,第四行有 1 个图像,第五行有 3 个图像,第六和第七行每行有 1 个图像。我每行无法获得 4 张图像。任何人都可以帮忙吗?

<!-- IMAGE GALLERY -->

    <h2 class="text1">Image Gallery</h2>
    <p>Click any image to open up a larger version</p>
    <div class="gallery">
        <?php
            $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
            $query = "SELECT * FROM cmhimage ORDER BY id DESC";
            $data = mysqli_query($dbc, $query);
            ?><table><?php 
            $level = 1;
            while ($row = mysqli_fetch_array($data)) { 
                $description = $row['capt'];
                $photo = $row['file'];
                $count = 1;
                $level = $level + $count;

                if (($level == 4) || ($level == 8) || ($level == 12) || ($level == 16) || ($level == 20) || ($level == 24)){
                    echo '<tr>';
                }

                 // Display the data
                echo '<td><table><tr><td><a href="manage/image/' . $photo . '" target="_blank"><img class="imgcontain" src="manage/image/' . $photo .'" border="0"/></a></td></tr></td><br/>';  
                echo '<td><tr><td><p class="caption">' . $description .'</p></tr></td></table></td>';  
                if (($level == 4) || ($level == 8) || ($level == 12) || ($level == 16) || ($level == 20) || ($level == 24)){
                    echo '</tr>';   
                    }

            }
            mysqli_close($dbc);

        ?>
       </table>

如果这是问题,这里是 CSS。

.gallery {
width: 525px;
}
.imgcontain {
padding-right:15px; 
float: left;
width: 110px;
height: 110px
}
.caption {
font:bold 78% arial,helvetica,sans-serif; 
text-align:center; 
position: relative;
right: 10px;
color:#056063;
} 
4

2 回答 2

0

尝试这个

<h2 class="text1">Image Gallery</h2>
<p>Click any image to open up a larger version</p>
<div class="gallery">
    <?php
        $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
        $query = "SELECT * FROM cmhimage ORDER BY id DESC";
        $data = mysqli_query($dbc, $query);
        ?>
    <table><tr>
        <?php 
        $level = 1;
        while ($row = mysqli_fetch_array($data)) { 
            $description = $row['capt'];
            $photo = $row['file'];
            $count = 4; //items per row

            if (($level > $count) && $level%$count==1){
                echo '
        </tr>
        <tr>';
            }

             // Display the data
//part of code from mercsen
echo '<td>';
echo ' <table>';
echo '  <tr>';
echo '    <td>';
echo '       <a href="manage/image/' . $photo . '" target="_blank"><img class="imgcontain" src="manage/image/' . $photo .'" border="0"/></a>';
echo '   </td>';
echo '   <td>';
echo '     <p class="caption">' . $description .'</p>';
echo '   </td>';
echo '  </tr>';
echo ' </table>';
echo '</td>';
            $level++;
        }
        mysqli_close($dbc);

    ?>
       </tr>
   </table>
于 2013-01-13T16:42:15.800 回答
0

有很多问题。首先代码几乎不可读

使用这种结构,第一个 tr 不显示而不是

 if (($level == 4) || ($level == 8) || ($level == 12) || ($level == 16) || ($level == 20) || ($level == 24))

尝试

if($level == 1 || $level % 4 == 0)

而不是

echo '<td><table><tr><td><a href="manage/image/' . $photo . '" target="_blank"><img class="imgcontain" src="manage/image/' . $photo .'" border="0"/></a></td></tr></td><br/>';
echo '<td><tr><td><p class="caption">' . $description .'</p></tr></td></table></td>'; 

尝试这个

echo '<td>';
echo ' <table>';
echo '  <tr>';
echo '    <td>';
echo '       <a href="manage/image/' . $photo . '" target="_blank"><img class="imgcontain" src="manage/image/' . $photo .'" border="0"/></a>';
echo '   </td>';
echo '   <td>';
echo '     <p class="caption">' . $description .'</p>';
echo '   </td>';
echo '  </tr>';
echo ' </table>';
echo '</td>';

if (($level == 4) || ($level == 8) || ($level == 12) || ($level == 16) || ($level == 20) || ($level == 24)){
                    echo '</tr>';   
                    }

像第一个。

尝试格式化您的代码。有了这个空间,您会看到许多单元格和行没有关闭。

编辑:在 while 开始之前放置 $count = 1 !在while的最后一行:

$count++;
于 2013-01-13T13:29:43.297 回答