PHP在不同的地方显示前4行

时间:2014-02-22 12:43:00

标签: php html mysql

我有以下HTML代码:

<div class="content-block1">
    <div class="image-block1"></div>
    <div class="bear1"></div>
    <div class="where-block1"></div>
    <div class="intro-block1"></div>
    <div class="map-block1"></div>
    <div class="extra-block1"></div>
    <div class="coordinates-block1"></div>
    <a href=""><div class="button-block1">More...</div></a>
</div>

<div class="content-block2">
    <div class="image-block2"></div>
    <div class="bear2">/div>
    <div class="where-block2"></div>
    <div class="intro-block2"></div>
    <div class="map-block2"></div>
    <div class="extra-block2"></div>
    <div class="coordinates-block2"></div>
    <a href=""><div class="button-block2">More...</div></a>
</div>

..以及以下PHP代码:

$host = "localhost";
$user = "username";
$pass = "password";
$database = "db";

mysql_connect($host,$user,$pass,$database) or die(mysql_error());
mysql_query("SET NAMES 'utf8'");

$query = mysql_query("select image,where,intro,map,extra,coordinates from content limit 2");

mysql_close();

现在,我已经非常努力地做了以下事情:我需要在表格内容中选择前两行,其中包括列图像,where,intro,map,extra和coordinates(不包括在内)。好的,我已经用查询选择了它们,但我有下一个问题 - &gt;我需要将第一行中的内容放在名为content-block1的第一个DIV元素中,因此,columns image,where,intro,map,extra和coordinates需要适合content-block1中的div。

另外,我需要将第二行(图像,where,intro,map,extra和coordinates)中的数据放入名为content-block2的DIV元素中,放入它们所属的DIV中。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

我认为这可能就是你要找的......

$host = "localhost";
$user = "username";
$pass = "password";
$database = "db";

mysql_connect($host,$user,$pass,$database) or die(mysql_error());
mysql_query("SET NAMES 'utf8'");

$query = mysql_query("select image,where,intro,map,extra,coordinates from content limit 2");

$i = 0;
while($row = mysql_fetch_array($query , MYSQL_ASSOC))
{ 
    $i++;
    echo '
        <div class="content-block{$i}">
            <div class="image-block{$i}">{$row['yourvaluehere']}</div>
            <div class="bear{$i}">{$row['yourvaluehere']}</div>
            <div class="where-block{$i}">{$row['yourvaluehere']}</div>
            <div class="intro-block{$i}">{$row['yourvaluehere']}</div>
            <div class="map-block{$i}">{$row['yourvaluehere']}</div>
            <div class="extra-block{$i}"></div>
            <div class="coordinates-block{$i}">{$row['yourvaluehere']}</div>
            <a href=""><div class="button-block{$i}">More...</div></a>
        </div>
     ';
}

答案 1 :(得分:0)

尝试将您的查询更改为此

$query = mysql_query("select * from content limit 0, 2");

来源= https://dev.mysql.com/doc/refman/5.0/en/select.html

然后执行以下操作以获取查询值

while($row = mysqli_fetch_array($query))
  {
$image = $row['image'];
$where = $row['where'];
$map = $row['intro'];
$extra = $row['extra'];
$coordinates = $row['coordinates'];

}

如果您只选择前两名

,这将是您的查询
$query = mysql_query("select TOP 2 * from content");