set every 1st and fourth element with a class - php

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)

Related

why Bootstrap grid positioning leaves empty columns?

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.

Simple Rating system in PHP

I created this code to Show the Current Rating of a product using stars.
The number of stars to be displayed will be determined by the results we get when we divide the review points by 2.
For example:
Product Shoe has a review points of 4 that means 4/2 =2 (2 stars)
or lets say 6 points 6/2 = 3 (3 stars).
Since the maximum number of stars is 5.
If the results we get when we divide points is greater than 5 the stars will stay at 5.
Here is my example code, but it's not working correctly and it's confusing.
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = $product_review / 2;
for($i=1; $i<=5; $i++) {
if($product_review >= $i) {
if($total_stars) {
echo $star;
}
}
}
Thanks in advance.
If you want to still use your code above, Here is the fixed code.
<?php
$star = "<li><a href='#'><i class='fa fa-star' aria-hidden='true'></i></a></li>";
$total_stars = (int)($product_review / 2);
for ($i=0; $i < $total_stars ; $i++) {
if ($i === 5) {
break;
}else{
echo $star;
}
}
?>

PHP: If number is divisble by 3, minus 1 (2, 5, 8, 11 etc)

I have a loop that creates divs with some content from a database. I have a variable $current_count which I'm starting at value '0' which is the first iteration of my loop.
I am using:
if ($current_count == 0 || $current_count % 3 == 0) { echo '<div class="parent">'; }
To create a parent div at the very top of the loop, then again on every iteration divisible by 3. It looks like this (with numbers representing iteration):
0 <div class="parent">
0 <div class="child"></div>
1 <div class="child"></div>
2 <div class="child"></div>
3 <div class="parent">
3 <div class="child"></div>
4 <div class="child"></div>
5 <div class="child"></div>
But the problem is I can't work out how to close those divs, as they would close on different iterations. For example, the parent opened on iteration 0 would need to be closed at the end of iteration 2.
I need to basically say (pseudo-code):
IF $current_count is equal to (division of 3, minus 1) { etc }
I have tried:
if ($current_count % 3 == (0 - 1)) {}
if ($current_count % (3 == 0) - 1) {}
if ($current_count % 3 == 0 - 1) {}
But none of these are returning true. Does anyone know a way I can do this?
Cheers,
Lee.
UPDATE 1: Here's an example of the PHP code currently, to better explain what I'm trying to accomplish:
$current_count = '0';
$ret = '';
foreach ( $brands as $index => $brand ) :
if ($current_count == 0 || $current_count % 3 == 0) {
$ret.= '<div class="parent">'; //Start parent
}
$ret.= '<div class="child"></div>'; //Child
if ($current_count % 3 == (0 - 1)) { // IF LINE 2, 5, 8, 11 etc, NOT WORKING
$ret.= '</div>'; // End the parent
}
$current_count++;
endforeach;
try this,
for($i = 0; $i <= 10; $i++) {
if($i % 3 == 0 && $i > 0)// $i > 0 condition because. 0 % 3 is equal to 0 only.
echo $i - 1;// will echo 2,5,8
echo "</div>";// in your case.
}
if you do it like that it still divided by 3 and not solving the question.
it should be :
if( $key % 3 == 2 ){
</div>
}

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++;
}
?>

Categories