简体   繁体   English

我如何将图像彼此相邻放置而不会破裂

[英]How do I put the images next to each other without them breaking

I have this PHP code that SELECTS up to 5 data from the mySQL database 我有这个PHP代码,可以从mySQL数据库中选择最多5个数据

<?php
$servername = "localhost";
$username = "kjikk";
$password = "kjkkk";
$dbname = "ujkhjkjl";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT content_url, id FROM posts LIMIT 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "' width='50%' height='25%'> - Name: " . $row["firstname"]. " " . $row["lastname"]. " ";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

When the 5 posts are echoed, the end up going vertically instead of next to each other and breaking (br tag)after two images are next to each other 回显5个帖子时,最终结果是垂直而不是彼此相邻,并且在两个图像彼此相邻之后断开(br标签)

Here's what it looks like: http://s9.postimg.org/vao63thr3/Screen_Shot_2015_05_25_at_11_16_03_AM.png 外观如下: http : //s9.postimg.org/vao63thr3/Screen_Shot_2015_05_25_at_11_16_03_AM.png

I want two images to be next to each other at the same time before they break to the next two images. 我希望两个图像同时相邻,然后再分解为下两个图像。

How do I do this? 我该怎么做呢?

You could create a div container, and for each image with name, you can wrap them into a div of 50% width from parent with a float: left; 您可以创建一个div容器,对于每个带有名称的图像,您可以将它们包装成距父级宽度为50%的div,并带有float:left;

HTML Structure: HTML结构:

<div class="container">
    <div class="half"><img src="..."> Name</div>
    <div class="half"><img src="..."> Name</div>
</div>

PHP: PHP:

if ($result->num_rows > 0) {
    // output data of each row
    <div class="parent">
    while($row = $result->fetch_assoc()) {
        echo "<div class='half'><img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "'> - Name: " . $row["firstname"]. " " . $row["lastname"]. "</div>";
    }
    echo "</div>";
} 
else {
    echo "0 results";
}

CSS: CSS:

.parent {
    width: 1000px; //your desired width
}
.parent .half {
    float: left;
    width: 50%;
}

This way you will ensure always that it's going to be 2 images per row. 这样,您将始终确保每行将有2张图像。

Remind: As jeroen said to you, before doing the logic, first try to do the structure the page will have, with it's HTML and CSS only. 提醒:正如jeroen对您说的那样,在执行逻辑之前,请首先尝试执行页面将具有的结构,仅使用HTML和CSS。 After that, create the logic and put the structure you will need to it. 之后,创建逻辑并将所需的结构放入其中。

Add float:left; 添加float:left;

while($row = $result->fetch_assoc()) {
    echo "<div style='float:left;margin-right:5px;'><img src='http://cdn.bithumor.co/uploads/" . $row["content_url"]. "'> - Name: " . $row["firstname"]. " " . $row["lastname"]. "</div>";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Woocommerce自定义结帐字段如何将它们彼此相邻? - Woocommerce custom checkout fields how to put them next to each other? 如何将2张图像并排放置,这些图像充当具有2个不同费用金额的Stripe付款按钮? - How can I put 2 images next to each other and the images act as my Stripe payment buttons with 2 different charge amounts? 当我将所有图像都设置为col-6时,为什么我的图像彼此位于较低的位置? - Why do my images get positioned below each other when I set all of them to col-6? 如何从mysqli查询中将文本并排放置 - How to put text next to each other from mysqli query 如何在 PHP 中显示即将到来的交替星期四,并让它们每周不互相覆盖 - How do I display the upcoming alternating Thursdays in PHP and have them not overwrite each other each week 如何阻止fwrite将字符串分成下一行? - How do I stop fwrite from breaking strings into the next line? 如何让这个 woocommerce 单选框水平显示,而不是垂直显示? - How do I make this woocommerce radio checkbox to display horizontally next to each other instead of vertically? 如何显示四个彼此对齐的类别帖子? -WordPress的 - How do I display four category posts that align next to each other? - Wordpress 来自foreach循环的彼此相邻的图像 - Images next to each other from a foreach loop 如何在不破坏CakePHP的MVC框架的情况下做到这一点? - How can I do this without breaking the MVC framework in CakePHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM