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.
Related
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.
I have the following code which with the following variables set numberofColumns = 2 and numberArticles = 10, will create 2 columns for articles, the article order is from left to right (column1 to column2) going down the page.
Id just like to add some code </div><div class="whatever"> after every 2nd article.
Any help would be much appreciated.
if ($numberColumns >= 1) {
$columnArticles = intval(($numberArticles + $numberK2Articles) / $numberColumns);
}
$columns = array();
for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
$columns[$columnIndex] = '<div class="column col-' . ($columnIndex + 1) . '">';
}
$articleIndex = 0;
while($articleIndex < count($articles)) {
foreach ($columns as $columnIndex => $column) {
if (isset($articles[$articleIndex])) {
$columns[$columnIndex] .= modCTRandomArticleHelper::getArticleHtml($params, $articles, $articleIndex);
$articleIndex++;
}
}
}
for($columnIndex = 0; $columnIndex < $numberColumns; $columnIndex++) {
echo $columns[$columnIndex] . '</div>';
}
I've always used the Modulus operator (%) to determine if my current index was one of n rows. $index % 2 returns 0 for every second row. $index % 3 returns 0 for every third row, and so on.
http://php.net/manual/en/internals2.opcodes.mod.php
The first comment on that page suggests that a bitwise operation is more efficient when you just need to determine odd or even. So, $index & 1 (odd) is a faster alternative to !$index % 2 (odd).
Is it possible to control a for-loop when it reaches a certain condition?
Explanation:
I'm retrieving the folder path to a collection of images from a database: these images are then printed out via a for-loop. What I would need to do is control how these images are displayed on the page (say, 5 images per row).
As of now, the for-loop prints out 40 images in a single row, which makes you scroll to the furthest right of the page.
Is there a solution for controlling the for-loop, as in for instance, after 5 successful loops, echo out a < br >? Here's a vulgar thought:
for ($i = 1; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path']; //since it's an array
echo '<img src="' . $folder_path . '">';
//pseudocode
if ($i == 5) {
echo '<br>';
...continue with the loop
}
}
I know the pseudocode looks crazy, but that's pretty much what I need to do: loop for x amount of instances, add something, then continue.
As per #m69's comment, the best option would be to use the % (modulus) comparison operator. As explained by the PHP docs:
$a % $b returns the remainder of $a divided by $b.
So, in your case:
for ($i = 0; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path']; //since it's an array
echo '<img src="' . $folder_path . '">';
if ($i % 5 == 0) { //do this if $i divided by 5 has a remainder of 0
echo '<br>';
}
}
As a side note, you should set $i to 0 at the beginning of your for loop, assuming $rows is set to the number of rows returned from your query. Setting it to 1 will keep it from iterating through the last row, because $i will == 40 (assuming 40 rows), and so will NOT be < 40.
The same loop. The condition for inserting the is ($i % 5 == 0), which means (if this element is the fifth one of his series) will be useful for you.
<?php
for ($i = 1; $i < $rows; $i++) {
$path = $image[$i];
$folder_path = $path['folder_path'];
echo '<img src="' . $folder_path . '">';
if ($i % 5 == 0) {
echo '<br>';
}
}
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++;
}
?>
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)