简体   繁体   English

如何在PHP和CSS中创建3列?

[英]How can I create 3 columns in PHP and CSS?

I am having issues making this work within my website. 我在网站上无法正常工作。 I have 3 columns in CSS. 我在CSS中有3列。 I am using the class of one_third for the first two columns and one_third column_last for the final column. 我在前两列中使用one_third类,在最后一列中使用one_third column_last类。 I have multiple rows: 我有多行:

<div class="one_third">
  <img src="images/content/about-grow.jpg" alt="" />
  <h4>Grow a Garden</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>

<div class="one_third">
  <img src="images/content/about-wildlife.jpg" alt="" />
  <h4>Protect Wildlife</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>

<div class="one_third column_last">
  <img src="images/content/about-volunteer.jpg" alt="" />
  <h4>Volunteer</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>

Here is my php. 这是我的PHP。 It is only outputting two columns as of now. 到目前为止,它仅输出两列。 I cannot figure out how to make the last column output correctly. 我无法弄清楚如何正确输出最后一列。 Please help! 请帮忙!

<?
include("includes/connect.php");
$staff = mysql_query("SELECT * FROM programs ORDER BY title ASC");
$numrows=@mysql_num_rows($staff);
if($numrows != 0) {
while ($result_staff = mysql_fetch_array($staff)) {
?>

<?
if ($result_staff['image'] == "") {
?>

<div align="center" style="padding-bottom:2%" class="one_third">
    <a href="images/content/<? echo $result_staff['image2']; ?>" class="fancybox" title="<? echo $result_staff['name']; ?>"><img src="images/content/<? echo $result_staff['image2']; ?>" alt="<? echo $result_staff['name']; ?>" border="1" /></a><br /><h4><? echo strtoupper($result_staff['title']); ?></h4><p><? echo $result_staff['content']; ?></p>
</div>

<? } else { ?>

<div align="center" style="padding-bottom:2%" class="one_third">
    <a href="images/content/<? echo $result_staff['image']; ?>" class="fancybox" title="<? echo $result_staff['name']; ?>"><img src="images/content/<? echo $result_staff['image']; ?>" alt="<? echo $result_staff['name']; ?>" border="1" /></a><br /><h4><? echo strtoupper($result_staff['title']); ?></h4><p><? echo $result_staff['content']; ?></p>
</div>

<? } ?>
<? } } ?>

Use css float left. 使用css向左浮动。 That will give column layout for dynamic content. 这将为动态内容提供列布局。 Sample working url available here: http://sugunan.net/demo/float.php 可在此处获得示例工作网址: http//sugunan.net/demo/float.php

Following is the source code. 以下是源代码。

<style>
.one_third{
    float: left;
    border: 1px solid;
    width:200px;
}
.wrapper{
    width: 610px;
}
.clear{
    clear: boath;
}
</style>


<div class="wrapper">

<?php for($i=0; $i<5; $i++){ ?>

<div class="one_third">
  <img src="photo.jpg" alt="" />
  <h4>Grow a Garden</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>

<div class="one_third">
  <img src="photo.jpg" alt="" />
  <h4>Protect Wildlife</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>

<div class="one_third column_last">
  <img src="photo.jpg" alt="" />
  <h4>Volunteer</h4>
  <p>Sed posuere consectetur est at lobortis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sed odio dui. sed.</p>
</div>
<div class="clear"></div>

<?php } ?>

</div>

In your example markup, it looks like the third column has a specific CSS class which differentiates it from the others: 在您的示例标记中,看起来第三列有一个特定的CSS类,该类与其他类有所区别:

class="one_third column_last"

But your PHP code isn't outputting anything with that class: 但是您的PHP代码不会对该类输出任何内容:

class="one_third"

This is mostly a guess based on the limited information provided, but you probably need to have a div with the column_last CSS class to be the third column. 这主要是根据提供的有限信息做出的猜测,但您可能需要将带有column_last CSS类的div作为第三列。 In fact, you're not outputting a third div at all. 实际上,您根本不会输出第三个div

You aren't adding your column_last class to any of the columns. 您没有将column_last类添加到任何列中。

First, put an iterator on your while loop. 首先,将一个迭代器放在while循环中。 Define $i = 0; 定义$ i = 0; right before the while loop. 就在while循环之前。 Then $i++; 然后$ i ++; right before the while loop ends. 在while循环结束之前。

Then you can use this iterator to determine where you are in the results set and append the class accordingly. 然后,您可以使用此迭代器来确定您在结果集中的位置,并相应地附加该类。

class="one_third <?php if($i == $numrows) echo 'column_last'; ?>"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM