why Bootstrap grid positioning leaves empty columns? - php

I am trying to create a Bootstrap positioning grid, generated by a PHP loop. The grid would contain 11 imgs, and the loop iterates 11 times. When the grid is created I don't understand why 2 columns always get skipped
this is the code I am using, I have uploaded a pictured of the resultenter image description here generated by my code
<style>
</style>
<?php
$counter = 11;
echo '<div class="row " >';
for ($i = 1; $i < $counter; $i++) {
echo "<div class=col-md-4 > ciao <img src=pictures/venue$i.jpg alt=Flowers class=img-fluid> </div>";
if (($i+1) % 4 == 0)
echo '</div><div class="row" id=my-row>';
}
echo '</div>';
?>

<?php
$counter = 11;
echo '<div class="row">';
for ($i = 0; $i <= $counter; $i++) {
$x = $i + 1; // for venue images starting at 1
echo '<div class="col-md-4"> ciao <img src="pictures/venue' . $x . '.jpg" alt="Flowers" class="img-fluid"> </div>';
if ( ( $x ) % 3 == 0 && $i != $counter) //added $i != $counter to make sure on the last iteration an extra row isn't added and changed modulus to 3 not 4
echo '</div><div class="row" id="my-row-' . $x . '">';
}
echo '</div>';
Bootstrap runs off 12 columns. You have a few problems with your code:
You are starting your counter at 1 causing the first block to display 3 but your next block to display 4 etc. col-md-4 takes 4 of the 12 columns so you can only fit 3 of these in a row.
You need to make sure you wrap your attributes in quotes, some browsers will still parse this and display as intended, others won't.
To display what you want properly (grid of 3 images along, 4 rows) you need 12 images or you will always be missing 1.
Each element that is provided an ID must be a unique ID you are repeating my-row

Change this line :
if (($i+1) % 4 == 0)
To this :
if (($i) % 3 == 0)
Because the former will split to three only on the first block, and the rest block are splitted to four.

Related

PHP start new row

I have wrote a script to show data from a MySql DB
<div class="row">
<?
for ($i = 1; $i <= mysql_num_rows($result41); $i++)
{
echo" <div class='col-md-3 col-sm-6'>
<div class='single-shop-product'>
<div class='product-upper'>
</div>
</div>
";
}
if ($i % 4 == 4) {
echo "</div><div class='row'>"; // it's time no move to next row
}
After 4 products have been shown I want to start a new row, I have added
if ($i % 4 == 4)
into the script thinking this would work, however it doesn't, can anyone help?
Replace your line of code with this.
if ($i % 4 == 0)
{
//Do something
}
You want $i % 4 == 0, that will tell you if the current value is evenly divisible by 4 (remainder = 0).

Assign different class names to div with php mysql

I have four divs in one page which are styled so that it has four alternating colours. And the even div i.e. 2nd and 4th have an extra class name 'r' like this.
<div class="x-1 liner"><div>
<div class="x-2 liner r"><div>
<div class="x-3 liner"><div>
<div class="x-4 liner r"><div>
The results are pulled from the database, I can use the modulus operator (%) to assign alternating colours for two rows as shown in here but how can I do this for four rows and also add 'r' to the even divs?
As kojow7 says you can use % 2 and % 4, see this code:
<?php
for($i = 1; $i <= 4; $i++) {
echo "div class='x-$i liner";
if($i % 2 == 0) echo ' r';
if($i % 4 == 0) echo ' color';
echo "'><div>\n";
}
?>
Output:
div class='x-1 liner'><div>
div class='x-2 liner r'><div>
div class='x-3 liner'><div>
div class='x-4 liner r color'><div>
EDIT:
With a foreach should be like this, but I don't know what values have $data
<?php
$i = 1;
foreach($data as $row) {
echo "div class='x-$i liner";
if($i % 2 == 0) echo ' r';
if($i % 4 == 0) echo ' color';
echo "'><div>\n";
$i++;
}
?>

How to break a long list into 4 columns

I've got a list of links generated through a loop in PHP. I generate a new div every time the first letter of the links changes. Now inside of this div (), I want to split the links in four columns (every 20 or so links).
Here is my code:
<div class="tab-content">
<?php
for($i = $infLimit; $i <= $supLimit; $i++){
$adherido = $lista_adheridos_filtrada[$i];
$nombreAdherido = $adherido->getNombreEntidad();
$initial = strtoupper($nombreAdherido[0]);
if($letter != $initial){
if($letter != ''){
echo '</div>';
}
$letter = $initial;
echo '<div id="'.$initial.'" class="tab-pane active">';
}
echo '<li><a target="_blank" title="'.$nombreAdherido.'" href="' . $protocol . '://'.$_SERVER["SERVER_NAME"].'/empresas/'.$adherido->getNombreFicha(). '.htm' . ((isset($_REQUEST["lang"]) && $_REQUEST["lang"] == "en") ? "?lang=en": "") . '">'.$nombreAdherido.'</a></li>';
} ?>
I recommend you use something similar to:
if ($i % 20 == 0) {
//create new column here
}
That will fire every time the $i variable hits a number divisible by 20. It is actually just checking the remainder of that division. This is called a modulus division operator, if you want to research more.

PHP nth iteration [duplicate]

This question already has answers here:
PHP: How do you determine every Nth iteration of a loop?
(8 answers)
Closed 9 years ago.
So I am trying to use PHP to generate some content with the criteria of the nth loop needs to either open or close a div element. So my goal is to display 8 elements on the page and on the 9th element, I would like to close the div for the 8th element and open a new one for the 9th if there are any more image. Here is my code.
$images; // object
$i = 1;
echo '<div class="outer-container">';
while ( $images->have_images() ) {
if ( $i === 1 ) {
echo '<div class="inner-container">';
}
echo 'content here';
if ( $i === 8 || $images->total === $images->current + 1 ) {
echo '</div><!--.inner-container-->';
}
$i++;
}
echo '</div><!--.outer-container-->';
End result should look something like this:
<div class="outer-container">
<div class="inner-container">
content here
content here
content here
content here
content here
content here
content here
content here
</div><!--.inner-container-->
<div class="inner-container">
content here
content here
content here
</div><!--.inner-container-->
</div><!--.outer-container-->
I searched around and came to a conclusion that I have to probably use modulus but I am not certain how I can incorporate that into my code.
Thanks for looking.
if ( $i % 8 == 0) {
echo '</div><!--.container--><div class="container">';
}
Mod operator returns the remainder. You can simply state $i % 8 or $i % 8 == 0 or $i % 8 === 0 as john stated
But you will need to also open the next div in your conditional depending on how you structure this. An example:
<div class="outer-container">
<div class="inner-container">
<?php
$images; // object
$i = 1;
while ( $images->have_images() ) {
echo 'content here';
//print_r($i % 8);
if ( $i % 8 == 0 && $images->total > 8) {
echo '</div><!--.inner-container--><div class="inner-container">';
}
$i++;
}
?>
</div>
</div>
Added conditional in case your images total happens to be 8 it won't create a rogue empty div.
I'm trying to understand your comment. Are you saying you have an outer container, then an inner container, and inside this inner container you want the loop you defined in the question where each group of 8 is contained in a container div?
I added a print r in the loop you can uncomment to verify $i is doing what you want it to. By this code and the fact that $i begins at 1 you should not experience the closing div conditional prematurely.
Modulus is indeed what you're looking for:
if ( $i % 8 === 0 ) {
echo '</div><!--.container-->';
}
You forgot to open a new div
modify the following code:
if ( $i === 8 || $images->total === $images->current + 1 ) {
echo '</div><!--.container-->';
echo '<div class="container">'; //ADD THIS LINE!!!
}
and after you get out of the while add the closing </div> tag

set every 1st and fourth element with a class

I have a jquery carousel set to 620px wide. I am using the grid 960 to place the images inside. I show 4 images at a time.
In order to do that I set the every first image class to 'grid_2 alpha' and every fourth to 'grid_2 omega' in the group of 4 shown while all in between I set to just grid_2.
This gives me the 620px I need. I am pulling from the database and I am trying to set the class dynamically but cant quite get every first and fourth in the group with the classes.
<?php $loopIndex = 1; ?>
<?php foreach ($pub_values as $v) {
if($v['pub_of_the_month'] == 1)
{
?>
<?php if ($loopIndex == 1 || $grid_class=="grid_2 omega") $grid_class="grid_2 alpha";
else if($loopIndex%4 == 0) $grid_class="grid_2 omega";
else $grid_class="grid_2";
$filename = "images/pub_images/120x160/".$v['id'].".jpg";
if (!file_exists($filename)) $filename = "images/pub_images/120x160/blank.gif";
?>
<div class="<?php echo $grid_class?>">
<a href="#">
<img src="<?=$filename;?>" alt="<?=$v['name'];?>" width="120" height="160" />
<?=$v['name'];?>
</a>
</div>
<?php $loopIndex = $loopIndex + 1; } }?>
The above code is my best attempt of achieving the following.
Images
1 - grid_2 alpha
2 - grid_2
3 - grid_2
4 - grid_2 omega
5 - grid_2 alpha
6 - grid_2
7 - grid_2
8 - grid_2 omega
9 - grid_2 alpha
Simple math, dividing with a remainder.
Maybe you can figure out by looking a this code:
for($i = 1; $i <= 9; $i++){
echo "\n $i ";
if($i % 4 == 1){
echo " alpha";
}elseif($i % 4 == 0){
echo " omega";
}
}
Live example: http://codepad.org/LIepxlJm
maybe you should just use a for-loop:
for ($i = 0; $i< sizeof($pub_values); $i++) {
$classes =['grid_2'];
if($i%4 == 0) $classes[] = 'alpha';
if($i%4 == 3) $classes[] = 'omega';
}
edit: this is just trying to show an approach, not trying to be syntactically correct.
in the end, you want to join (or implode in php) your classes to get the string.... (not implemented for you either)

Categories