half on the right and half on the left
I am currently trying to find a good solution for what to do with this
$all_products
this is an array with 7 products and i need to loop through them to create the dynamic page but i have kind of a structure issue with the page
<div class="left-pane left">
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
</div>
<div class="right-pane left">
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
<div class="item">
</div>
</div>
How will I know how many to put in the right and the left and can i alternate 开发者_C百科between then or do i need to do something like (count($all_product) / 2) on the left and then the rest on the right pane. Also how do i loop only half on one side using. how do i find out what to do because the array will be different count every time
<?php foreach ($all_products as $product) { ?>
You can construct each half separately by using something like:
$i = 0;
$left = '<div class="right-pane left">';
$right = '<div class="right-pane right">';
foreach ($products as $product) {
if ($i++ % 2 == 0) {
$left .= '<div class="item">' . $product . '</div>';
} else {
$right .= '<div class="item">' . $product . '</div>';
}
}
$left .= '</div>';
$right .= '</div>';
An alternative is to give them all a fixed width and float them all left in a container of fixed width.
.item {
float: left;
width: 300px;
}
Here's a kickoff example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>SO question 3654173</title>
<style>
#items {
width: 600px;
}
.item {
float: left;
width: 300px;
}
</style>
</head>
<body>
<div id="items">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
<div class="item">item5</div>
</div>
</body>
</html>
It will only go from left to right and then top to bottom, not the other way round as in your original question. Not sure if that hurts.
$i = 0;
$left = "";
$right = "";
foreach ($all_products as $product) {
if($i%2==0) $left.= '<div class="item">'.$product.'</div>';
else $right.= '<div class="item">'.$product.'</div>';
$i++;
}
echo '<div class="left-pane left">'.$left.'</div>'.'<div class="right-pane left">'.$right.'</div>';
精彩评论