简体   繁体   English

如何使该循环一次,然后中断然后继续循环?

[英]How do I make this loop once, break then continue the loop?

I am looping the div horizontally with float:left and it works the way it should but the problem is each div has a unique height that is influenced by the content contained within, so the result can be messy looking when the browser is re sized. 我用float:left在div中水平循环,它按应有的方式工作,但是问题是每个div都有一个唯一的高度,该高度受其中包含的内容的影响,因此当调整浏览器大小时,结果可能看起来很混乱。 I would like for the php to loop once, break, then start a new row so that it looks the same no matter how the browser is sized. 我希望php循环一次,中断,然后开始新的一行,以便无论浏览器的大小如何,它看起来都一样。

If it's not already obvious, I am not good with php and struggled to piece together what I have. 如果还不是很明显,那么我对php并不满意,并且努力拼凑我所拥有的东西。

<?php
$url = "XML.php";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 10; $i++){

    $poster = $xml->channel->item[$i]->children($namespaces['x'])->poster;
    $html .= "<div class='wrapper' style='float:left;'>$poster</div>";}

echo $html;
?>

If i understand what you want with the information i have here is what i suggest: 如果我了解您想要的信息,这里就是我的建议:

Your loop is fine. 您的循环很好。 So lets say that you want it to look the same no mater the size of the page. 因此,可以说您希望它看起来没有页面大小一样。 For exemple : you want 5 div on each row. 例如:您希望每行5格。 You could use css to do it. 您可以使用CSS来做。

You have a total of 10 div so you would need 2 rows. 您总共有10格,因此您需要2行。

add this to your loop : 添加到您的循环:

if($i = 5){
$html .= "<div class='wrapper' style='float:left;clear:right;'>$poster</div>";}
}
else{
$html .= "<div class='wrapper' style='float:left'>$poster</div>";}
}

This is very basic, the clear:right; 这是非常基本的, clear:right; CSS statement will prevent any other div to go to the right of the fifth div so it will start a new row; CSS语句将阻止其他任何div进入第五个div的右侧,因此它将开始新的一行;

You should probably add display:inline-block; 您可能应该添加display:inline-block; to your css as well. 以及您的CSS。

If this isnt working the way you want, an other option would be ta make each row inside a div with the same kind of if condition 如果这没有按照您想要的方式工作,则另一个选择是使用相同的if条件使div中的每一行

exemple: 为例:

html.= '<div class="row">'; // start the row here
 if($i = 5){
 // You might want to use another increment to keep track of the row
 //  because in this case you would only make 2 rows.
    $html .= '</div><div class="row">'; // end the first row and start a new one
    }
    else{
    // we do nothing
    }
html.= '</div>'; //end the row here

you might want to use a second increment that jump by five so with a bit of mat you can make something like 您可能希望使用第二个增量,该增量增加5,所以可以在垫子上加上一些类似的东西

after that you just have to tweak the CSS row class. 之后,您只需要调整CSS行类。

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

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