I have a question. Heres my code
<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
<p><?php echo $key['symptoms'] ?></p>
<p><strong style="color: green;">AND</strong></p>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
But, The output is like this
IF
symptoms A
AND
symptoms B
AND
symptoms C
AND
THEN
disease A
I want the last AND to be disappear. Can anyone help me pls? :( I'm sorry because I'm a newbie in here. Thanks!
You need to check if $i is less than the last element in the array: $i < count($data) - 1. Here is an example:
<p><strong style="color: blue;">IF</strong></p>
<?php foreach ($data as $i => $key) : ?>
<p><?php echo $key['symptoms'] ?></p>
<!-- only print AND if it is not the last element -->
<?php if ($i < count ($data) - 1) { ?>
<p><strong style="color: green;">AND</strong></p>
<?php } ?>
<?php endforeach ?>
<p style="color: red;"> <strong>THEN</strong></p>
<p><?php echo $key['disease_name'] ?></p>
Related
I'd like to achieve on adding a comma between my authors in my foreach but each time I make any references, it adds a comma at the end also.
Here is what I have below:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($recipe->get_authors() as $profile): ?>
<?= $profile->get_name() ?>,
<?php endforeach; ?>
<p><?= $recipe->get_content() ?></p>
</div>
Which gives me the following results:
How can I prevent a comma at the end?
Since you were skeptical about implode, here's a simple example (just showing the loop):
<?php foreach ($recipe->get_authors() as $profile):
$links[] = '' . $profile->get_name() . '';
endforeach; ?>
<?= implode(', ', $links); ?>
An alternative using join:
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> - <?= join(", ", array_map(function ($profile) { return sprintf('%s', $profile->get_url(), $profile->get_name()); }, $recipe->get_authors())) ?>
<p><?= $recipe->get_content() ?></p>
</div>
Wasn't able to test it but basically store results to $authors first, and in this way you can count it without recalling the method get_authors. While $cnt keeps track base 1 and can be compared directly on the length.
<?php
$authors = $recipe->get_authors();
$cnt = 0;
?>
<div class="four-sixths first">
<img class="mb-20" src="<?= $recipe->get_thumbnail() ?>" alt=""/>
<h2 class="mb-5"><?= $recipe->get_title() ?></h2>
<span><?= $recipe->get_date() ?></span> -
<?php foreach ($authors as $profile) { $cnt++; ?>
<?= $profile->get_name() ?><?php
if ($cnt < count($authors)) echo ",";
?>
<?php } ?>
<p><?= $recipe->get_content() ?></p>
</div>
I am trying to fetch some info from database under ZF1 environment. I am using a for loop here. What I am trying to accomplish is that inserting a new css division (div) for each four iteration. In other words, I would like to insert a block followed by four loop iterations. For example, if I run a loop twelve times, there should be three new blocks after each four iterations. It is basically an advertisement block that may contain image and link. I need an idea.
Here is an example block.
<div class="flexblock-image">
<a href="https://offers.example.com/offer/600112?cid=BWO_mh.com_html_getbackinshape&keycode=254194">
<img alt="" title="" typeof="foaf:Image" src="http://www.example.com.gif">
</a>
</div>
Here is the loop I have coded in my app.
<?php if ($this->numArticles > 0) : ?>
<?php for ($i = 0; $i < 13; $i++) : ?>
<div class="channel-image ">
<div class="img">
<h2 class="primary-tag">
<a href="/tags/weight-loss-tips" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo $this->articles[$i]->slug ; ?>
</a>
</h2>
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<img typeof="foaf:Image" src="
<?php echo $this->articles[$i]->getCover('crop'); ?>" width="650" height="412" alt="
<?php echo addslashes($article->title); ?>" title="
<?php echo addslashes($article->title); ?>">
</a>
</div>
<div class="channel-content">
<p class="date">
<span property="dc:date dc:created" content="2015-06-04T11:06:18-04:00" datatype="xsd:dateTime">
<?php echo $this->dateFormatter()->diff(strtotime($this->articles[0]->activated_date), $this->timeDiffFormats); ?>
</span>
</p>
<h2 class="article-title" id="title-node-93726">
<a href="
<?php echo $this->articles[$i]->getViewUrl(); ?>">
<?php echo addslashes($this->articles[$i]->title); ?>
</a>
</h2>
<div class="byline-container">
<span class="byline-role">By </span>
<span class="field-author">
<a href="#" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">
<?php echo sprintf($this->translator()->_('show.by'), $this->articles[0]->getAuthor()); ?>
</a>
</span>
</div>
</div>
</div>
<?php endfor; ?>
<?php endif; ?>
Not an expert on PHP, but wouldn't an if statement inside of your for loop work by using the modulus operator?
<?php for ($i = 0; $i < 13; $i++) : ?>
<?php if ($i % 4 === 0) : ?>
// Custom HTML/CSS here
<?php endif; ?>
<?php endfor; ?>
More on the modulus operator can be found here
I have a page that lists a set of questions and answers that are held on a database. Because these pages will be printed off on to A4, the questions and answers must fit within the page. However, because of the way the system is used, the answers are quite lengthy and over run a set dimension for an A4 piece of paper.
I have worked out that for my specific need, each A4 piece of paper will have a word limit of 680. The test data I am using has exceeded this, and currently stands are 972 words in total.
Currently this is the code I am using (be aware that there is an individual word count recorded for every individual answer and every individual question):
<div class="page" style="page-break-before: always;">
<div class="smallheading">
<h2>Management Information</h2>
</div>
<div class="content v2">
<?php
$totalCharacters = 0;
$totalcharacters1 = 0;
$totalcharacters2 = 0;
foreach($sqsi as $si) {
$totalcharacters1 += $si['Swmpquestion']['wordcount'] + $si['Swmpanswer']['wordcount'];
} foreach ($sqsicust as $wr) {
$totalcharacters2 += $wr['Swmpanswer']['wordcount'] + $wr['Swmpanswer']['cqwordcount'];
}
$totalCharacters = $totalcharacters1 + $totalcharacters2;
?>
<?php foreach($sqsi as $si) { ?>
<p><strong><?php echo $si['Swmpquestion']['question']; ?></strong></p>
<p><?php echo $si['Swmpanswer']['answer']; ?></p>
<hr />
<?php } foreach($sqsicust as $wr) { ?>
<p><strong><?php echo $wr['Swmpanswer']['custquestion']; ?></strong></p>
<p><?php echo $si['Swmpanswer']['answer']; ?></p>
<hr />
<?php } ?>
</div>
What I want to achieve is that while the PHP script goes through it's foreach, every time the questions and answers in that foreach reach 680 words an end and start div would be inserted, so the long list of data would be split up properly. How can I achieve this?
If i understood you correctly, then you just need to assign the variable to be iterated and make a comparison against it in your loop.
foreach($sqsi as $i => $si) { // note $i =>
if($i == 679) echo '</div><div>'; ?>
<p><strong><?php echo $si['Swmpquestion']['question']; ?></strong></p>
<p><?php echo $si['Swmpanswer']['answer']; ?></p>
<hr />
<?php }
You said you're already keeping a count of words, so why not just use a simple if statement in the second 2 foreach loops? Instead of counting the words in the first two foreach loops like you're doing. Keep track of how many words in the 2nd two foreach loops and then a simple if statement will do the trick. Something like this should work if I understood you correctly:
<div class="page" style="page-break-before: always;">
<div class="smallheading">
<h2>Management Information</h2>
</div>
<div class="content v2">
<?php
$totalCharacters = 0;
$totalcharacters1 = 0;
$totalcharacters2 = 0;
foreach($sqsi as $si) {
$totalcharacters1 += $si['Swmpquestion']['wordcount'] + $si['Swmpanswer'] ['wordcount'];
} foreach ($sqsicust as $wr) {
$totalcharacters2 += $wr['Swmpanswer']['wordcount'] + $wr['Swmpanswer']['cqwordcount'];
}
$totalCharacters = $totalcharacters1 + $totalcharacters2;
?>
<?php foreach($sqsi as $si) {
$totalcharacters1 = $si['Swmpquestion']['wordcount'] + $si['Swmpanswer'] ['wordcount']
if($totalcharacters1 == 680){
echo "</div>";
echo "<div class="content v2">";
$totalcharacters1=0;
}
?>
<p><strong><?php echo $si['Swmpquestion']['question']; ?></strong></p>
<p><?php echo $si['Swmpanswer']['answer']; ?></p>
<hr />
<?php } foreach($sqsicust as $wr) {
$totalcharacters2 += $wr['Swmpanswer']['wordcount'] + $wr['Swmpanswer']['cqwordcount'];
if($totalcharacters2 == 680){
echo "</div>";
echo "<div class="content v2">";
$totalcharacters2=0;
}
?>
<p><strong><?php echo $wr['Swmpanswer']['custquestion']; ?></strong></p>
<p><?php echo $si['Swmpanswer']['answer']; ?></p>
<hr />
<?php } ?>
</div>
add an $i=0; //a starting point
and an $qtyToLpThru=x; //ie every 25 iterations
before the foreach.
within foreach add just after the first { an if statement..
foreach(){
if (is_int($i/$qtyToLpThru)) && $i!=0) {
echo '</div><div>';
}
$i++;
}
then add an $i++; just before the last }.
your iteration will increment the $i and you'll write your end and open div every time the if statement processes true.
I have this code, and I need to close a row every 4 post. Every post is inside a div. I tried some things but I coudn't implement to my code.
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
$count = 1;
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php
if($count == 4){
echo "<div class='seperator'></div>";
$count =1;
}
?>
<?php $count++; } //endforeach ?>
<?php wp_reset_query(); ?>
I'd actually solve this 100% in CSS, so you don't need any counting or handling inside your PHP code.
Have a look at this JSFiddle.
float: left will cause the single elements to all follow each other (left aligned).
clear: left on every 4 * n + 1-th element (nth-child(4n+1)) will clear this, essentially forcing a line break.
There is one caveat to this: If there's no room for all 4 entries in one row, you'll end up with additional wrapping, which can be avoided by defining a fixed width for the container.
A simplified in-code version for PHP would just count the fields written and add a line break as necessary:
$i = 1; // counter
foreach ($events as $event) { // iterate over all events
if ($i++ % 4 == 0) // a % b will be 0 for 4, 8, etc.
echo '<br />'; // print the line break using whatever HTML you see fit.
print_event($event); // print the actual event
}
You might ask whether I check for the line break before actually printing an event: That's to prevent additional line breaks if the number of entries is a multiple of 4, i.e. I avoid having an empty trailing line.
You need the modulo operator. It works like this:
$i == 0;
foreach($some_array as $some_value){
if ($i % $number_to_divide_by == 0) {
// do something here every nth time
}
$i++;
}
I would like to output some specific HTML on the third iteration of a loop in PHP. Here is my code:
<?php foreach ($imgArray as $row): ?>
<div class="img_grid"><?= $row ?></div>
<?php endforeach; ?>
On the third iteration of this loop, Instead of displaying:
<div class="img_grid"><?= $row ?></div>
I would like to display:
<div class="img_grid_3"><?= $row ?></div>
I would like to end up with this if my array looped 8 times:
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
Thanks
Assuming $imgArray is an array and not an associative array (i.e. it has numeric indices), this is what you want:
<?php foreach($imgArray as $idx => $row): ?>
<?php if($idx % 3 == 2): ?>
<div class="img_grid_3"><?php echo $row; ?></div>
<?php else: ?>
<div class="img_grid"><?php echo $row; ?></div>
<?php endif; ?>
<?php endforeach; ?>
You could tighten it up a bit like this:
<?php foreach($imgArray as $idx => $row):
if($idx % 3 == 2) {
$css_class = 'img_grid_3';
} else {
$css_class = 'img_grid';
}
?>
<div class="<?php echo $css_class; ?>"><?php echo $row; ?></div>
<?php endforeach; ?>
Or even more (some folks would just go with a ternary conditional inline in the HTML), but the law of diminishing returns kicks in eventually with regard to readability. Hopefully this gives you the right idea, though.