[英]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.