使用Flexbox水平和垂直显示PHP While循环

huangapple go评论65阅读模式
英文:

Display PHP While Loop Horizontally & Vertically Using Flexbox

问题

出现了一些问题,需要一些见解。我正在向访问我网站的用户显示用户创建的事件。我正在使用一个while循环来显示尚未过去的所有事件。我的目标是将两个不同的事件显示在一起,然后再下面显示另外两个,依此类推。目前,我正在使用flexbox CSS属性来实现这一目标,但它只以垂直方式显示输出,而不是我想要的方式,也就是每行只放置一个事件。以下是我目前用于显示事件的输出。

include 'db_connect.php';
$event_type = $_POST['event_type'];
$state = $_POST['state'];
$current_date = date("Y-m-d");
$sql = "SELECT * FROM events WHERE event_type LIKE '%".$event_type."%' AND state LIKE '%".$state."%' AND end_date > '$current_date' ORDER BY end_date ASC";
$result = mysqli_query($conn, $sql);
if (isset($_POST['search-events']) && !empty($event_type) && !empty($state)) {
    while($row = mysqli_fetch_array($result)) {
        $event_name= $row['event_name'];
        $image = $row['image'];
        $start_date = $row['start_date'];
        $end_date = $row['end_date'];
        $start_time = $row['start_time'];
        $end_time = $row['end_time'];
        $street = $row['street'];
        $city = $row['city'];
        $state = $row['state'];
        $zip_code = $row['zip_code'];
        $id = $row['event_id'];
        echo '<div class="filter-wrap">';
            echo '<div id="filter-boxes">';
                echo '<div id="list_image"><img src="'.$image.'"></div>';
                echo '<div id="list_name">'.$event_name.'</div>';
                echo '<div id="list_date">'.$start_date. ' - ' .$end_date.'</div>';
                echo '<div id="list_time">'.$start_time. ' - ' .$end_time.'</div>';
                echo '<div id="list_location">'.$street.' '.$city.' '.$state.' '.$zip_code.'</div>';
            echo '</div>';
        echo '</div>';
    }
}

接下来是我使用的CSS。

.filter-wrap {
    display: flex;
    flex-direction: row;
    padding: 10px;
    justify-content: center;
    align-items: center;
}

#filter-boxes {
    border: 2px solid #999;
    text-align: center;
    width: 50%;
    height: 150px;
}

如你所见,我在包含每个事件的容器内使用了flex属性。我将flex方向设置为"row",因为我希望它水平显示,然后在每一行的空间用尽后换行。

我尝试了一些方法。我尝试切换到css的column-count属性,但没有得到我期望的结果。老实说,我可能没有充分调整该属性,但我已经决心使用flexbox属性。我还尝试将flex方向设置为"column",并尝试为while循环中应该重复的框添加了内联块显示属性。我在网上找到了一些与我的问题有点相似的解决方案,但都不太一样。有一个使用JavaScript的,但显然也可以通过PHP以某种方式实现。我还找到了几篇关于使用flexbox居中内容的文章,但这不是本次的目标。

英文:

Having a bit of an issue here. Could use some insight. I'm displaying user created events to visitors to my website. I'm using a while loop to display everything that hasn't not yet already passed. My goal is to display two separate events right next to each other, then another two below that and so on. Currently I'm using the flex box css property to achieve this, but it's only displaying the output vertically and not the way I want it to, meaning it's only putting one event per line. Here is my current output for displaying the events.

include &#39;db_connect.php&#39;;
		$event_type = $_POST[&#39;event_type&#39;];
		$state = $_POST[&#39;state&#39;];
		$current_date = date(&quot;Y-m-d&quot;);
		$sql = &quot;SELECT * FROM events WHERE event_type LIKE &#39;%&quot;.$event_type.&quot;%&#39; AND state LIKE &#39;%&quot;.$state.&quot;%&#39; AND end_date &gt; &#39;$current_date&#39; ORDER By end_date ASC&quot;;
		$result = mysqli_query($conn, $sql);
		if (isset($_POST[&#39;search-events&#39;]) &amp;&amp; !empty($event_type) &amp;&amp; !empty($state)) {
			while($row = mysqli_fetch_array($result)) {
				$event_name= $row[&#39;event_name&#39;];
				$image = $row[&#39;image&#39;];
				$start_date = $row[&#39;start_date&#39;];
				$end_date = $row[&#39;end_date&#39;];
				$start_time = $row[&#39;start_time&#39;];
				$end_time = $row[&#39;end_time&#39;];
				$street = $row[&#39;street&#39;];
				$city = $row[&#39;city&#39;];
				$state = $row[&#39;state&#39;];
				$zip_code = $row[&#39;zip_code&#39;];
				$id = $row[&#39;event_id&#39;];
    			echo &#39;&lt;div class=&quot;filter-wrap&quot;&gt;&#39;;
    				echo &#39;&lt;div id=&quot;filter-boxes&quot;&gt;&#39;;
    					echo &#39;&lt;div id=&quot;list_image&quot;&gt;&lt;img src=&quot;&#39;.$image.&#39;&quot;&gt;&lt;/div&gt;&#39;;
						echo &#39;&lt;div id=&quot;list_name&quot;&gt;&#39;.$event_name.&#39;&lt;/div&gt;&#39;;
						echo &#39;&lt;div id=&quot;list_date&quot;&gt;&#39;.$start_date. &#39; - &#39; .$end_date. &#39;&lt;/div&gt;&#39;;
						echo &#39;&lt;div id=&quot;list_time&quot;&gt;&#39; .$start_time. &#39; - &#39; .$end_time. &#39;&lt;/div&gt;&#39;;
						echo &#39;&lt;div id=&quot;list_location&quot;&gt;&#39;.$street.&#39;&#39;.$city.&#39;&#39;.$state.&#39;&#39;.$zip_code.&#39;&lt;/div&gt;&#39;;
					echo &#39;&lt;/div&gt;&#39;;
				echo &#39;&lt;/div&gt;&#39;;
		}
	}

Then there's the css that I'm using.

.filter-wrap {
	display: flex;
	flex-direction: row;
	padding: 10px;
	justify-content: center;
	align-items: center;
}

#filter-boxes {
	border: 2px solid #999;
	text-align: center;
	width: 50%;
	height: 150px;
	}

As you can see, I'm using the flex property inside the container that holds each of the individual boxes that holds each event. I have the flex direction set to row since I want it to display horizontally, then go the next line after it runs out of room on each line.

I tried a few things. I tried switching to the css column count property but didn't get the results I was expecting. I honestly probably didn't tweak with that property enough, but I have my heart set on the flex box property. I also tried setting the flex direction to column and also tried adding an inline-block display property to the boxes that are suppose to repeat on the while loop with each event. I'm finding this online that are kind of similar to my issue, but not quite. One uses javascript, but this can obviously also be accomplished somehow with php. I also found several articles talking about centering the content using flexbox, which is not the goal here.

答案1

得分: 1

尝试将.filter-wrap<div>元素移出while() {}循环外。

你当前的编码如下:

while() {
  echo '<div class="filter-wrap">';
    echo '<div id="filter-boxes">';
      // 内容在这里...
    echo '</div>';
  echo '</div>';
}

会导致以下结构,其中每个.filter-wrap只包含一个单个的.filter-boxes子元素,这将始终导致垂直呈现:

<div class="filter-wrap">
  <div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
  <div id="filter-boxes"> content </div>
</div>
<div class="filter-wrap">
  <div id="filter-boxes"> content </div>
</div>

要实现水平呈现,正确的结构应该是一个.filter-wrap包含多个.filter-boxes子元素:

<div class="filter-wrap">
  <div id="filter-boxes"> content </div>
  <div id="filter-boxes"> content </div>
  <div id="filter-boxes"> content </div>
</div>

因此,你可以尝试更改你的编码如下:

echo '<div class="filter-wrap">';

while() {
  echo '<div id="filter-boxes">';
    // 内容在这里...
  echo '</div>';
}

echo '</div>';

这是一个演示代码片段,演示了编码逻辑的结果。虽然这是JS示例,但你可以在你的PHP中应用相同的原则。希望对你有帮助,祝你编码愉快!

英文:

Try move your .filter-wrap div element to outside of the while() {} loop.

Your current coding:

while() {
  echo &#39;&lt;div class=&quot;filter-wrap&quot;&gt;&#39;;
    echo &#39;&lt;div id=&quot;filter-boxes&quot;&gt;&#39;;
      // content goes here...
    echo &#39;&lt;/div&gt;&#39;;
  echo &#39;&lt;/div&gt;&#39;;
}

will result in following structure where each .filter-wrap only container a single child .filter-boxes, which will always results in vertical presentation:

&lt;div class=&quot;filter-wrap&quot;&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;filter-wrap&quot;&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;filter-wrap&quot;&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
&lt;/div&gt;

For horizontal presentation, the correct structure should be one .filter-wrap consists of multiple .filter-boxes childs:

&lt;div class=&quot;filter-wrap&quot;&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
  &lt;div id=&quot;filter-boxes&quot;&gt; content &lt;/div&gt;
&lt;/div&gt;

So you can try change your coding to:

echo &#39;&lt;div class=&quot;filter-wrap&quot;&gt;&#39;;

while() {
  echo &#39;&lt;div id=&quot;filter-boxes&quot;&gt;&#39;;
    // content goes here...
  echo &#39;&lt;/div&gt;&#39;;
}

echo &#39;&lt;/div&gt;&#39;;

Snippet for demo to you the coding logic result. It is in JS but you just have to apply the same in your PHP

Hope it helps and Happy coding!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var result_1 = document.getElementById(&#39;result_1&#39;);
var result_2 = document.getElementById(&#39;result_2&#39;);

var content_1 = &#39;&#39;;
var content_2 = &#39;&#39;;

content_2 = &#39;&lt;div class=&quot;filter-wrap&quot;&gt;&#39;;

for (var i = 1; i &lt;= 5; i++)
{
  // result 1
  content_1 += &#39;&lt;div class=&quot;filter-wrap&quot;&gt; &lt;div class=&quot;filter-boxes&quot;&gt;content &#39; + i + &#39;&lt;/div&gt; &lt;/div&gt;&#39;;
  
  // result 2
  content_2 += &#39;&lt;div class=&quot;filter-boxes&quot;&gt;content &#39; + i + &#39;&lt;/div&gt;&#39;;
}

content_2 += &#39;&lt;/div&gt;&#39;;

result_1.insertAdjacentHTML(&#39;beforeend&#39;, content_1);
result_2.insertAdjacentHTML(&#39;beforeend&#39;, content_2);

<!-- language: lang-css -->

.filter-wrap {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 10px;
  justify-content: center;
  align-items: center;
}

.filter-boxes {
  border: 2px solid #999;
  text-align: center;
  width: 50%;
  height: 50px;
  
  /* for two columns display */
  max-width: 49%;
}

#result_1,
#result_2 {
  background-color: lightgray;
  border: 1px dashed black;
  margin: 3px;
}

<!-- language: lang-html -->

result 1
&lt;div id=&quot;result_1&quot;&gt;&lt;/div&gt;

result 2
&lt;div id=&quot;result_2&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 10:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定