如何在连续的数据库整体上交替浮动?

发布于 2024-10-31 11:17:39 字数 1138 浏览 7 评论 0原文

我的网站

正如您在上面的链接中看到的,有几个“孩子” ',每一项的信息都是从数据库中获取的。

我想让每个交替图像向左浮动,以便图像向左,然后向右,然后向左等等。目前它们都向左浮动。这就是我到目前为止所拥有的:

<?php
$query="SELECT id, title, text, media, media2, thumb, deleted FROM kids ORDER BY id DESC";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
        if($row['deleted'] == 0) {

echo '<div id="'.$row['title'].'">';
echo '<img src="images/'.$row['media'].'" class="floatLeftClear" id="border" />';
if($row['media2'] != "") {
echo '<img src="images/'.$row['media2'].'" class="floatLeftClear" id="border" />';
}

echo '<p id="text">';
echo '<span class="kisstitle">'.$row['title'].'</span><br>';
echo $row['text'];
echo '</p>';
echo ' <p align="center" id="spererater"><img src="images/seperater.jpg" width="900" height="5" /><a href="index2.php?op=The Kids#top">Top</a><br /></p>';
}}
?>

我尝试检查ID,如果是偶数,则向左浮动,如果是奇数,则向右浮动,但我不知道该怎么做,而且ID可能并不总是偶数/奇数交替所以如果可能的话我宁愿找到不同的解决方案。

My Website

As you can see on the above link there are several 'kids', the information for each one is grabbed from a database.

I want to make each alternating image float to the left, so that the images go left, then right, then left etc. At the moment they all float left. This is what I have so far:

<?php
$query="SELECT id, title, text, media, media2, thumb, deleted FROM kids ORDER BY id DESC";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
        if($row['deleted'] == 0) {

echo '<div id="'.$row['title'].'">';
echo '<img src="images/'.$row['media'].'" class="floatLeftClear" id="border" />';
if($row['media2'] != "") {
echo '<img src="images/'.$row['media2'].'" class="floatLeftClear" id="border" />';
}

echo '<p id="text">';
echo '<span class="kisstitle">'.$row['title'].'</span><br>';
echo $row['text'];
echo '</p>';
echo ' <p align="center" id="spererater"><img src="images/seperater.jpg" width="900" height="5" /><a href="index2.php?op=The Kids#top">Top</a><br /></p>';
}}
?>

I tried to check the ID, if even, float left, if odd, float right, but I couldn't work out how to do it, plus the ID may not always be even / odd alternating so I'd rather find a different solution if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-11-07 11:17:40

使用计数器变量,每次循环一个项目时加一 - 并将该计数器与模一起使用,以确定您是在偶数行还是奇数行:

$counter = 0;
while($row = mysql_fetch_array($result)){
    if (($counter % 2) == 0) {
        // even
    } else {
        // odd
    }

    // use the data of the current $row, display it

    $counter++; // increment the counter, for next iterator
}

Use a counter variable, increment by one each time you're looping over an item -- and use that counter with a modulo, to determiner whether you are on an even or odd line :

$counter = 0;
while($row = mysql_fetch_array($result)){
    if (($counter % 2) == 0) {
        // even
    } else {
        // odd
    }

    // use the data of the current $row, display it

    $counter++; // increment the counter, for next iterator
}
如果没结果 2024-11-07 11:17:40

添加一个计数器。循环之前 $i = 0; 和循环内部 $i++, if ($i % 2 == 0) { // odd } else {// Even }

Add a counter. Before loop $i = 0; and inside loop $i++, if ($i % 2 == 0) { // odd} else {// even}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文