Assign different class names to div with php mysql - php

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

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.

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).

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)

PHP echo in rows of three?

I am attempting to show data in rows of three like this (notice the number of items will not always be even):
abcd defg hijk
lmno pqrs tuvw
xyz1 2345 6789
1011 1213
I am struggling to get the logic right to do this (this is in a foreach() loop).
I know I have to have some if($i %3 == 0) logic in there.. But I'm a bit stuck.
Can anyone help me out?
$a = array('abcd','defg','hijk','lmno');
for ($i = 0; $i < count($a); $i++) {
if ($i && $i % 3 == 0)
echo '<br />';
echo $a[$i].' ';
}
It's better to use a for loop as:
// run $i for each index in the array.
for($i=0 ; $i<count($arr) ; $i++) {
// if $i is non-zero and is divisible by 3 print a line break.
if ($i && $i % 3 == 0) {
echo "<br />";
}
// print the element at index $i.
echo $arr[$i].' ';
}
Code in action
Pseudo-code since I don't know PHP (and you asked for the logic which tends to be the same across all procedural languages):
perline = 3
i = 0
foreach item in list:
if i > 0 and (i % perline) == 0:
print newline
if (i % perline) != 0:
print space
print item
i = i + 1
This will both output a line separator before elements 3, 6, 9 and so on (first element being 0) and place whatever desired spacing you want before the second and third elements on each line. You can just use a different value for perline to change the number output on each line.

PHP - Tricky... array into columns, but in a specific order

<?php
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$i = 0;
foreach ($combinedArray as $r ){
/*** use modulo to check if the row should end ***/
echo $i++%$num_cols==0 ? '<div style="clear:both;"></div>' : '';
/*** output the array item ***/
?>
<div style="float:left; width:33%;">
<?php
echo $r;
?>
</div>
<?php
}
?>
<div style="clear:both;"></div>
The above code will print out the array like this:
apple --- banana --- watermelon
lemon --- orange --- mango
However, I need it like this:
apple --- watermelon --- orange
banana --- lemon --- mango
Do you know how to convert this? Basically, each value in the array needs to be placed underneath the one above, but it must be based on this same structure of 3 columns, and also an equal amount of fruits per column/row (unless there was like 7 fruits there would be 3 in one column and 2 in the other columns.
Sorry I know it's confusing lol
Thanks everyone for your help... I realized a better way to do it though. Simple put, I have 3 columns floating next to eachother. And in each column, I add a list of the items into it and stop when I hit the max items per row.
working code:
<div style="float:left; width:33%;">
<?php
$combinedArraySizeOf = sizeof($combinedArray);
$num_cols = 3;
$iPerRow = $combinedArraySizeOf / $num_cols;
for ($i=0; $i!=$combinedArraySizeOf; $i++){
if ($i % $iPerRow == 0){
echo '</div><div style="float:left; width:33%;">';
}
echo $combinedArray[$i]."<br />";
}
?>
</div>
<div style='clear:both;'></div>
Don't forget to clear both at the end if necessary :P
Why aren't you doing exactly what you want to do? I mean show them in columns, instead of rows?
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rowCount = ceil(count($combinedArray)/$num_cols);
$i = 1; // in order the modulus to work correctly
?>
<div style="float: left; width:33%"> <!-- this is the first column -->
foreach ($combinedArray as $r ){
?>
<div> <!-- just a div containing $r -->
<?php
echo $r;
?>
</div>
<?php
// this is where the magic happens
// check if we have enough rows and start another column
if ($i % $rowCount == 0) {
?>
</div> <!-- close the previous column and start a new one -->
<div style="float: left; width:33%"> <!-- this is the new column -->
<?php
}
$i++;
}
?>
</div> <!-- closing the last open column -->
<div style="clear:both;"></div>
This should do just the job you wish. Marvin's answer is better if you want to use only tables without divs.
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$step = 2;
$i = 0;
$new_array = array();
foreach ($combinedArray as $r ){
$remainder = ($i % $step);
$new_array[$remainder][] = $r;
$i++;
}
foreach($new_array as $array)
{
echo implode(' --- ', $array)."<br>";
}
Would this work?
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $num_cols; $j++){
echo $combinedArray[(($i+$j) * $rows)-$i];
}
echo "<br />";
}
This would also need to check that the value existed for cases where the number of items wasn't precisely divisible by the number of columns, you could do that with the following change:
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $num_cols; $j++){
$cell = (($i+$j) * $rows)-$i;
if($cell > count($combinedArray)) break;
echo $combinedArray[$cell];
}
echo "<br />";
}
If this order is what you ideally want, but it's not critical that it works in all browsers, perhaps you should look at coloumn layout (still very experimental css3 draft). If you use dispay inline block for the element in each coloumn you'll have the current order as a fallback.
You could also use a table for the layout and use a for loop something like this (pseudo php code, it's been a while since I've coded any php):
maxHeight = Math.ceil(array.length / 3) // meaning length=6 will give 3,
// length=7 will give 4
$x = -1; // horizontal index
for(i = 0; i < array.length(); i++){
$y = i % maxHeight; // vertical index
if($y == 0){
$x++;
}
addToTable($x,$y, array[i]);
}

Categories