Here is my code snippet:
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\" data-slide-number=".$index.">".$sth."</div>";
}
It iterates the code depending on $sth. But I want to add active class to the first occurence of the code, so it will be:
<div class="active item" data-slide-number="0">"sth"</div>
<div class="item" data-slide-number="1">"sth"</div>
<div class="item" data-slide-number="2">"sth"</div>
I'm trying to do something like this, but it doesn't work:
for($x=0;$x<=$flaga1;$x++){
$active=0;
$divclass="<div class=\"item\"";
do {
$divclass="<div class=\"active item\"";
$active++;
} while ($active<=0);
$index++;
echo "".$divclass." data-slide-number=".$index.">".$sth."</div>";
}
As per code submitted by you. You can try this.
for($x=0;$x<=$flaga1;$x++){
$active = "";
if($x === 0 ) {
$active = "active";
}
$index++;
echo "<div class=\"item\ <?php echo $active; ?>" data-slide-number=".$index.">".$sth."</div>";
}
Or you can do this, as suggested by Niet the Dark Absol.
for($x=0;$x<=$flaga1;$x++){
$index++;
echo "<div class=\"item\ <?php if($x === 0) {echo "active"; } ?>" data-slide-number=".$index.">".$sth."</div>";
}
You can also set this up in your for loop definition if you like:
for ($x = 0, $a = 'active'; $x < $flaga1; $x++, $a = '') {
echo "<div class=\"item $a\" data-slide-number=". ++$index .">" . $sth . "</div>\n";
}
Related
I have a foreach statement in my app that echos a list of my database results:
<?php
foreach($featured_projects as $fp) {
echo '<div class="result">';
echo $fp['project_name'];
echo '</div>';
}
?>
I would like to:
On every third result, give the div a different class. How can I achieve this?
You can use a counter and the modulo/modulus operator as per below:
<?php
// control variable
$counter = 0;
foreach($featured_projects as $fp) {
// reset the variable
$class = '';
// on every third result, set the variable value
if(++$counter % 3 === 0) {
$class = ' third';
}
// your code with the variable that holds the desirable CSS class name
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
foreach ($featured_projects as $i => $fp) {
echo '<div class="result' . ($i % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
If the $featured_projects array is based on incremental index you could simply use the index and the modulo % operator.
Otherwise you would have to add a counter.
http://php.net/manual/en/language.operators.arithmetic.php
add a counter in this loop and check if counter equals three and apply class.
Using a counter and modulo operator this is easy to implement
<?php
foreach($featured_projects as $fp) {
if(++$i % 3 === 0) {
$class = ' something';
} else {
$class = '';
}
echo '<div class="result' . $class . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
<?php
$i = 0;
foreach($featured_projects as $fp) {
echo '<div class="'.($i++%3 ? 'result' : 'other_class').'">';
echo $fp['project_name'];
echo '</div>';
}
?>
What leaves your code mostly in tact would be
<?php
$i = 1;
foreach($featured_projects as $fp) {
printf ('<div class="%s">',(($i % 3) ? "result" : "result_every_third" ));
echo $fp['project_name'];
echo '</div>';
$i++;
}
?>
But you may want to consider using a for or while construct around "each($featured_projects)" (see http://php.net/manual/en/function.each.php) which may result in neater code.
<?php
$counter = 0;
foreach ($featured_projects as $fp) {
echo '<div class="result' . ($counter++ % 3 === 0 ? ' third' : '') . '">';
echo $fp['project_name'];
echo '</div>';
}
?>
You can add a counter in loop ...try the following...
<?php
$i = 0;
foreach($featured_projects as $fp) {
$i = ++$i;
if(($i%3) == 0)
{
$class1 = 'test1';
}
else
{
$class1 = 'test2';
}
echo '<div class="'.$class1.'">';
echo $fp['project_name'];
echo '</div>';
}
?>
This is the working version, sorry for my prev version:
<?php
$featured_projects[0]['project_name'] = "pippo";
$featured_projects[1]['project_name'] = "pippo2";
$featured_projects[2]['project_name'] = "pippo3";
$class[0] = "class1";
$class[1] = "class2";
$i=0;
foreach($featured_projects as $fp) {
$i++;
if ($i == 3) {
$c = $class[1];
$i=0;
} else {
$c = $class[0];
}
echo "<div class=\"$c\">";
echo $fp['project_name'];
echo "</div>\n";
}
?>
Produces:
<div class="class1">pippo</div>
<div class="class1">pippo2</div>
<div class="class2">pippo3</div>
I'm looking for a way to wrap every too items of this very basic for loop within a div :
<?php
for( $i=1; $i<=50; $i++ )
{
echo "<div><a href='item-".$i."'>".$i."</a></div>";
}
?>
This produces the following :
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
The output i need would be :
<div>1 2</div>
<div>3 4</div>
Thanks
Easiest way i can think of is by doing this. This also works if you have a resultset from a database, or an array of item objects, just replace the range() function with the array.
<?php
foreach (array_chunk(range(1, 50), 2) as $chunk) {
echo "<div>";
foreach ($chunk as $itemId) {
echo "<a href='item-" . $itemId . "'>" . $itemId . "</a>";
}
echo "</div>" . PHP_EOL;
}
Use modulo operator
code:
<?php
$chunks = 2;
$display = true;
for( $i=1; $i<=6; $i++ )
{
if ($display && ($i % $chunks || $chunks === 1)) {
echo "<div>";
$display = false;
$last = true;
}
echo "<a href='item-".$i."'>".$i."</a>";
if (!($i % $chunks)) {
echo "</div>" . PHP_EOL;
$display = true;
$last = false;
}
}
if ($last) {
echo "</div>" . PHP_EOL;
}
generates:
<div><a href='item-1'>1</a><a href='item-2'>2</a></div>
<div><a href='item-3'>3</a><a href='item-4'>4</a></div>
<div><a href='item-5'>5</a><a href='item-6'>6</a></div>
and with:
$chunks = 3;
you will get:
<div><a href='item-1'>1</a><div><a href='item-2'>2</a><a href='item-3'>3</a></div>
<div><a href='item-4'>4</a><div><a href='item-5'>5</a><a href='item-6'>6</a></div>
and so one.
i want to add active class in while loop first div.
but its not working
$ban_sql = mysql_query("SELECT * FROM banners");
while ($row = mysql_fetch_assoc($ban_sql)){
echo '<div class="item ';
if($row <= 1) {
echo 'active ">';
}
else
{
echo '">';
}
echo '<img src="'.$row['banner_img'].'"/> </div>';
}
This should do the trick:
$ban_sql = mysql_query("SELECT * FROM banners");
$count = 0;
while ($row = mysql_fetch_assoc($ban_sql)){
echo '<div class="item ';
if($count == 0) {
echo 'active ">';
}
else
{
echo '">';
}
echo '<img src="'.$row['banner_img'].'"/> </div>';
$count++;
}
$row will contain a MySQL rowset so you need a separate variable to store the current row iteration count.
I have a php script that echoes a series of uploaded images and I need to apply a class to the first image echoed in the series. Is this possible? For example, if I want to display 10 images and apply a class to only the first image, how would I go about it?
Here is the code, I am working with:
<div id="gallery">
<?php
query_posts('cat=7');
while(have_posts())
{
the_post();
$image_tag = wp_get_post_image('return_html=true');
$resized_img = getphpthumburl($image_tag,'h=387&w=587&zc=1');
$url = get_permalink();
$Price ='Price';
$Location = 'Location';
$title = $post->post_title;
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
echo "rel=\"<div class='gallery_title'><h2>";
echo $title;
echo "</h2></div>";
echo "<div class='pre_box'>Rate:</div><div class='entry'>\$";
echo get_post_meta($post->ID, $Price, true);
echo "</div><div class='pre_box'>Location:</div><div class='entry'>";
echo get_post_meta($post->ID, $Location, true);
echo "</div>\"";
echo "'/></a>";
echo "";
}
?>
On line 13, the code looks like this:
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
For the first item only, I need it to look like this:
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
EDIT:
So I have updated my code after reading everyones suggestions to look like this:
<?php
query_posts('cat=7');
while(have_posts())
{
the_post();
$image_tag = wp_get_post_image('return_html=true');
$resized_img = getphpthumburl($image_tag,'h=387&w=587&zc=1');
$url = get_permalink();
$counter = 0;
$Price ='Price';
$Location = 'Location';
$title = $post->post_title;
while(have_posts())
{
$counter++;
if ($counter > 1) {
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
} else {
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
}
}
echo "rel=\"<div class='gallery_title'><h2>";
echo $title;
echo "</h2></div>";
echo "<div class='pre_box'>Rate:</div><div class='entry'>\$";
echo get_post_meta($post->ID, $Price, true);
echo "</div><div class='pre_box'>Location:</div><div class='entry'>";
echo get_post_meta($post->ID, $Location, true);
echo "</div>\"";
echo "'/></a>";
echo "";
}
?>
Then my browser crashed, so I suspect I didn't put this together correctly....Any suggestions?
Use a Loop Counter variable to count the iterations:
query_posts('cat=7');
$count = 0
while(have_posts())
Then, use it in your echo:
if ($count == 0){
// echo with class
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
}
else {
// echo without class
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
}
Then at the bottom of your loop, make sure to increment it:
echo "";
$count = $count + 1;
}
?>
You could use a counter, to determine on which iteration you are :
$i_posts = 0;
query_posts('cat=7');
while(have_posts())
{
if ($i_posts == 0) {
echo "first iteration";
}
$i_posts++;
}
This means your line 13 would be replaced by something like this :
if ($i_posts == 0) {
// First iteration of the loop : special class
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
} else {
// Next iterations : no special class
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
}
Just do:
$c = 0;
query_posts('cat=7');
while(have_posts())
{
*** snip ***
if ($c == 0) {
$class = " class='show'";
} else {
$class = "";
}
echo "<a href='$url'$class><img src='$resized_img' width='587' height='387' ";
*** snip ***
$c++;
}
Add a counter
$counter = 0;
while(have_posts())
{
$counter++;
if ($counter > 1) {
echo "<a href='$url'><img src='$resized_img' width='587' height='387' ";
} else {
echo "<a href='$url' class='show'><img src='$resized_img' width='587' height='387' ";
}
}
Just add a boolean:
$firstTime = true;
while(have_posts())
{
if($firstTime)
{
$firstTime = false;
$class = 'class="show"';
}
else
{
$class = '';
}
}
You might want to use a for loop instead of the while and add the class just during the first iteration.
Let's say I have a user that entered 12 links into the database but when they are displayed I want to break up the links into groups of 3 and repeat the groups of three until all 12 kinks are displayed for example.
<div>
<p>Link 1</p>
<p>Link 2</p>
<p>Link 3</p>
</div>
Here is part of the code I'm working with. How should my code look like?
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<div>';
echo '<p>' . $row['category'] . '</p>';
echo '</div>';
}
}
Something like this gets you three links per div. We add the extra conditional echo at the end for the case that there's not a multiple of 3 links
$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
if ($ctr%3 == 0) {
echo '<div>';
}
$ctr ++;
echo '<p>' . $row['category'] . '</p?';
if ($ctr%3 == 0) {
echo '</div>';
}
}
}
if ($ctr%3 != 0) {
echo '</div>';
}
if you want to avoid incrementing/resetting i every time you reach three, you can just use the modulus operator:
http://php.net/manual/en/language.operators.arithmetic.php
Something like:
if( $i%3 == 0 ){
// divisible by 3...
}
An easier method would be more like:
while(true) {
$c = 0;
echo "<div>";
while ($row = mysqli_fetch_assoc($dbc) && $c++ < 3) {
if (!empty($row['category']) && !empty($row['url'])) {
echo '<p>' . $row['category'] . '</p>';
}
}
echo "</div>";
if(empty($row))
break;
}