Displaying ads in a foreach loop content - php

I have a foreach loop like this
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
$sn_count++;
}
echo $html;
This code output is like this
1
First point description
2
second point description
etc...
Now i want to display ads after every 2,4,6,... points. I mean in-between 2-3,4-5,6-7,...
Can anyone help me with the code?
Thanks

You'll want to use the modulus
$sn_count = 1;
$html = '';
$display_ad_every = 3;
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count++ % $display_ad_every == 0)
{
// append ad code here
}
}
echo $html;

This should do it
$sn_count = 1;
$html = '';
foreach($points as $point){
$html .= "<div class=\"points\">"
. "<div class=\"serial\">".$sn_count."</div>"
. "<div class=\"pointsdesc\">"
. $point['points_description']
. "</div></div>";
if($sn_count % 2 === 0){
$html .= get_ad_code()
}
$sn_count++;
}
echo $html;

Related

Is there a way to make a NxM table where each cell has two subcells on the bottom?

How do I make a NxM table where each cell has two rows and its second row has two columns, but by using rowspan or colspan? I want to make something like this:
Here is my attempt.
Artikal.php:
public function get_html()
{
$html = "";
$html .= "<td>";
if (isset($this->sifra) && isset($this->naziv) && isset($this->cena) && isset($this->kolicina)) {
if ($this->kolicina > 0) {
$html .= "<img src=\"images/" . str_replace(" ", "-", $this->naziv) . jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
} else {
$html .= "<img src=\"images/none.png\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">{$this->sifra}</span>";
$html .= "<span class=\"cena\">{$this->cena} DIN</span>";
}
} else {
$html .= "<img src=\"images/none.jpg\" width=\"100\">";
$html .= "<br>";
$html .= "<span class=\"sifra\">X</span>";
$html .= "<span class=\"cena\">X</span>";
}
$html .= "</td>";
return $html;
}
index.php
if (isset($artikli)) {
for ($i = 0; $i < 6; $i++) {
echo "<tr>";
for ($j = 0; $j < 9; $j++)
echo $artikli[$i * 9 + $j]->get_html();
echo "</tr>";
}
}

Adding background color to element within php if-statement

I have the following code in which I want to add an element to my if statement, but I am unsure how to approach this. What I am looking to do is add a background-color to the else part of it. Within this:
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
So if $status is not 0, I want to set background color for the class goal-box-right.
Is there anyway I can manipulate the CSS through php like this?
foreach ($rows as $row) {
$status = $row['status'];
if ($status == 0) {
$status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
$goal_date = $row['date'];
$fixed_goal_date = fixDate($goal_date);
$html = "";
$html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
$html .= '<div class="goal-box-left">'; //added
$html .= '<div class="goal-post-status">'.$status_img. '</div>';
$html .= '</div>'; //added
$html .= '<div class="goal-box-right">'; //added
$html .= '<div class="goal-post-title">'.$row['title']. '</div>';
$html .= '<div class="goal-post-date">'.$fixed_goal_date. '</div>';
$html .= '<div class="goal-post-description">'.$row['description']. '</div>';
$html .= '</div>'; //added
$html .= '</div>';
Separate the logic - add a CSS class based on the status then set the colours in the stylesheet.
$class = $status != 0 ? 'status-nonzero' : '';
$html .= '<div class="goal-box-right ' . $class . '">';
CSS:
.goal-box-right.status-nonzero { background-color: #foo }
Advantage is keeping the style logic in the CSS where it belongs and away from the code that's generating the HTML.
So what's the problem:
$style = '';
if ($status == 0) {
$style = ' style="your-style:here"';
$status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
// ...
$html .= '<div class="goal-box-right"' . $style . '>'; //added

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )

How can I find last <td> in table with PHP

I wonder how can i find out last in table with PHP, aka if that is last then i want to apply different style and content to it.
This is part of my code generating table content.
$content .= '</tr>';
$totalcols = count ($columns);
if (is_array ($tabledata))
{
foreach ($tabledata as $tablevalues)
{
if ($tablevalues[0] == 'dividingline')
{
$content .= '<tr><td colspan="' . $totalcols . '" style="background-color:#efefef;"><div align="left"><b>' . $tablevalues[1] . '</b></div></td></tr>';
continue;
}
else
{
$content .= '<tr>';
foreach ($tablevalues as $tablevalue)
{
$content .= '<td>' . $tablevalue . '</td>';
}
$content .= '</tr>';
continue;
}
}
}
else
{
$content .= '<tr><td align="center" style="border-bottom: none;" colspan="' . $totalcols . '">No Records Found</td></tr>';
}
I know there is option to do something like that with jQuery later on, but I don't want to complicate it and just solve it with php at time when i generate table.
Keep a count:
$count = 1;
foreach ($tablevalues as $tablevalue)
{
$count++;
if( $count > count( $tablevalues)) {
echo "Last td tag is up next! ";
}
$content .= '<td>' . $tablevalue . '</td>';
}
you can execute any code in last loop:
for($i=0;$i<count($tablevalues);$i++)
{
$tablevalue=$tablevalues[$i];
$content .= '<td>' . $tablevalue . '</td>';
if($i==count($tablevalues)-1){
// last loop execution
}
}
here is a somehow trick
foreach ($tablevalues as $k => $tablevalue)
{
if ($k==count($tablevalues)-1)
{
// last td of current row
$content .= '<td>' . $tablevalue . '</td>';
}
else
{
$content .= '<td>' . $tablevalue . '</td>';
}
}
I just hope that your keys of your $tablevalues set match this code.
You should use a for($i = 0; i < count($tablevalues); $i++) {} loop and consider count($tablevalues)-1 to be the last key of your array.
So that $tablevalues[count($tablevalues)-1] is your last <td>.
Why dont you use jQuery?It has provisions to do something like that.

Categories