how to split while with every 4 time using continue - php

how setting product category wise with limit 4 that's why i have to split while loop with every 4 count
I want something like this:
category name 1
product 1
product 2
product 3
product 4
category name 2
product 5
product 6
product 7
product 8
I have tried the following code:
$count=0;
while($row_pt = $result_2->fetch_array())
{
if($count%4==0)
{
$output .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<h3>CATEGORY NAME'.$row_pt['cat_id'].'</h3>
<div style="border:1px solid #ccc; border-radius:0px; padding:16px; margin-bottom:16px; height:250px;">
<img src="image/'. $row_pt['product_image'] .'" alt="" class="img-responsive" >
<p align="center"><strong>'. $row_pt['product_name'] .'</strong></p>
Course Code : '. $row_pt['product_name'] .' <br />
Course Name : '. $row_pt['product_name'] .' <br />
</div>
</div>';
continue;
}
$count++;
}

Something like this, so you're wrapping every four products in a div by utilising the count variable:
$count = 1;
while ($row_pt = $result_2->fetch_array())
{
if (($count - 1) % 4 === 0)
{
$output .= '<div class="products">';
}
$output .=
'<h3>CATEGORY NAME' . $row_pt['cat_id'] . '</h3>
<div class="product">
<img src="image/' . $row_pt['product_image'] . '">
' . $row_pt['product_name'] . '
<span>Course Code : ' . $row_pt['product_name'] . '</span>
<span>Course Name : ' . $row_pt['product_name'] . '</span>
</div>';
if ($count % 4 === 0)
{
$output .= '</div>';
}
$count++;
}
if (($count - 1) % 4 !== 0)
{
$output .= '</div>';
}

Related

combination php and html code have error - inside hr tag in all li tag except last li tag

this is my code :
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
} .
'</li>';
in this line - i want add "hr tag" in all "li tag" - except last li - but this line have error - what i must do?
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
}
You need to move your condition outside the concatenation.
// here i am using ternary operator for your condition.
$condition = (($key + 1) != $comments->count() ? '<hr>' : '');
Then, you can use like that:
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.$condition. // change this part
'</li>';
Using IF condition with concatenation (.) will produce "syntax error, unexpected 'if'".

how to display data (devided in 2 by table column name of type bool) in php

In my php class i get a result from query and would like to display the data on 2 columns bases on the value from the db table(active or inactive). I would like to have something like this:
active inactive
supplier name a supplier name d
supplier name b supplier name e
supplier name c supplier name f
This is my code:
while ($sensor_row = $result_of_query->fetch_assoc()) {
if ($sensor_row['active'] == 0) {
$status = 's_on';
$thisSensor = '<div class="statusTitle">Active Suppliers</div>';
$thisSensor .= '<div class="row clearfix sensor ' . $sensor_row['id'] . '">';
$thisSensor .= ' <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">';
$thisSensor .= ' <div class="card">';
$thisSensor .= ' <div class="body ' . $status . '"> </div>';
$thisSensor .= ' </div>';
$thisSensor .= ' </div>';
$thisSensor .= '</div>';
echo $thisSensor;
} else if ($sensor_row['active'] == 1) {
$status = 's_on';
$thisSensor = '<div class="statusTitle">Inactive Suppliers</div>';
$thisSensor .= '<div class="row clearfix sensor ' . $sensor_row['id'] . '">';
$thisSensor .= ' <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">';
$thisSensor .= ' <div class="card">';
$thisSensor .= ' <div class="body ' . $status . '"> </div>';
$thisSensor .= ' </div>';
$thisSensor .= ' </div>';
$thisSensor .= '</div>';
echo $thisSensor;
}
}

Adding A Div For Every 4th MySQL Result With Pagination

I have a script that displays images from a database.
Every 4 pictures is contained in a <div class="row">.
I added a pagination script to this page to limit the number of images and I ran into a problem.
When there are 4 or 8 images on a page the script works fine, but if there are only 1 or 7 images on the page, the closing </div> for the <div class="row"> doesn't get added.
This is my entire script:
$conn = getConnected("lucycypher");
$img_start=0;
$img_limit=8;
if(isset($_GET['page'])) {
$page=$_GET['page'];
$img_start=($page-1)*$img_limit;
}
else { $page = 1; }
$img_total=mysqli_num_rows(mysqli_query($conn, "select * from gallery_img"));
$img_total_count=ceil($img_total/$img_limit);
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
?>
</div>
</div>
<?php
if($_COOKIE['age_verification'] == "adult") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img order by img_time desc LIMIT $img_start, $img_limit;";
}
else if($_COOKIE['age_verification'] == "child") {
$img_query = "SELECT img_name, img_category, img_location FROM gallery_img WHERE img_category NOT LIKE '%nude' order by img_time desc LIMIT $img_start, $img_limit;";
}
$img_result = mysqli_query($conn, $img_query);
if (mysqli_num_rows($img_result) > 0) {
// output data of each row
$img_count = 1;
while($img_row = mysqli_fetch_assoc($img_result)) {
$tags = $img_row["img_category"];
if ( $img_count%4 === 1 ) { echo '<div class="row">' . PHP_EOL; } // Create new row for every 4th image
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-default">' . PHP_EOL;
echo '<img src="http://lucycypher.com/h/400/w/300/a/c/thumb/'.$img_row["img_location"].'" class="img-responsive img-protected">' . PHP_EOL;
echo '<div class="panel-footer"><span class="glyphicon glyphicon-tag"></span> Tags: ' . $tags . '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row
$img_count++;
}
} else {
echo '<div class="col-md-3">' . PHP_EOL;
echo '<div class="panel panel-primary">' . PHP_EOL;
echo '<div class="panel-heading"><span class="glyphicon glyphicon-picture"></span> Sorry</div>' . PHP_EOL;
echo 'No recent uploads.' . PHP_EOL;
echo '</div>' . PHP_EOL;
echo '</div>' . PHP_EOL;
}
?>
<div class="row">
<div class="col-md-12">
<?php
echo '<nav aria-label="Page navigation">
<ul class="pagination">' . PHP_EOL;
if($page>1) {
echo '<li><span aria-hidden="true">Previous</span></li>' . PHP_EOL;
}
for($i=1;$i<=$img_total_count;$i++) {
if($i==$page) { echo "<li class='active'><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
else { echo "<li><a href='?page=".$i."'>".$i."</a></li>" . PHP_EOL; }
}
if($page!=$img_total_count) {
if(!isset($page)) { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
else { echo '<li><span aria-hidden="true">Next</span></li>' . PHP_EOL; }
}
echo '</ul>
</nav>' . PHP_EOL;
Once I figure this out I'm going to limit the images to 8 per page so on the last page if there are only 3 total images then it's missing the final </div> tag since there isn't a total of 4 which completely throws off the rest of the page layout.
Best way to explain it is a visual I assume:
When images total 4 per row with a limit of 8:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
</div>
But if the final page only has 3 images:
<div class="row">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<img src="http://example.com/img.jpg">
<!--Missing </div> Tag-->
The final </div> tag is set by this line: if ( $img_count%4 === 0 ) { echo '</div>' . PHP_EOL; } // Close row.
You may calculate the total count of images, which will be uploaded.
Then, use if ( $img_count%4 === 0 || $img_count==$total_count )
In my example $img_count is the number of added image (beginning from 1)

PHP rendering two columns, but I need three columns

I have some php that creates two columns of website content categories with child articles from that category. I've tried messing with the code that inserts the after two posts are listed, but it's not working (because I don't know what I'm doing). The current php renders as follows:
<div class="row">
<div class="column col-half">...</div>
<div class="column col-half">...</div>
</div>
I'd like it to render as:
<div class="row">
<div class="column col-third">...</div>
<div class="column col-third">...</div>
<div class="column col-third">...</div>
</div>
Here is the theme's code that renders the HTML:
$st_categories = get_categories($st_hp_cat_args);
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
foreach($st_categories as $st_category) {
$st_cat_counter++;
if ((!is_int($st_cat_counter / 2)) && $st_cat_counter != 1) {
echo '</div><div class="row">';
} elseif ($st_cat_counter == 1) {
echo '<div class="row">';
}
echo '<div class="column col-half '. $st_cat_counter.'">';
echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' . sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' . $st_category->name.'</a>';
if (of_get_option('st_hp_cat_counts') == '1') {
echo '<span class="cat-count">(' . $st_category->count.')</span>';
}
echo '</h3>';
Thanks in advance.
You need to change just a couple of things (I improved the code style in terms of lines and indentation as well):
$st_categories = get_categories($st_hp_cat_args);
$st_categories = wp_list_filter($st_categories,array('parent'=>0));
if ($st_categories) {
foreach($st_categories as $st_category) {
$st_cat_counter++;
if (1 === $st_cat_counter % 3 && $st_cat_counter !== 1) { // change 2 -> 3 and use mod operator %
echo '</div><div class="row">';
} elseif ($st_cat_counter == 1) {
echo '<div class="row">';
}
echo '<div class="column col-third '. $st_cat_counter.'">'; // half -> third
echo '<h3> <a href="' . get_category_link( $st_category->term_id ) . '" title="' . sprintf( __( 'View all posts in %s', 'framework' ), $st_category->name ) . '" ' . '>' . $st_category->name.'</a>';
if (of_get_option('st_hp_cat_counts') == '1') {
echo '<span class="cat-count">(' . $st_category->count.')</span>';
}
echo '</h3>';

adding for each clause into variable

I have the following variable $prod_list_contents which is a mix of html and php METHODS to display the products from my database:
$prod_list_contents .= '<div style="width:100%;">';
$prod_list_contents .= '';
while ($listing = tep_db_fetch_array($listing_query)) {
$rows++;
$prod_list_contents .= '
<div class="cat_prod_box">
<div class="image">
<div class="cat_image_table">
<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES_CAT . $listing['products_image'], $listing['products_name'], 255, 340, 'class="cat_image_round"') . '
<div class="hidden_thing">Click to View</div>
</a>
</div>
</div>
<div class="cat_product_info" >';
$prod_list_contents .= '<div class="span_style_num">Style: '. $listing['products_model'] . '</div>';
$prod_list_contents .= '<div class="span_colors">Colors: red, blue</div>';
$prod_list_contents .= '<div class="span_product_name">' . $listing['products_name'] . '</div>';
$prod_list_contents .= '<div class="span_price">CAD ' .$currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) .'</div>' ;
$prod_list_contents .= '</div></div>';
}
$prod_list_contents .= ' </div>' .
' '
;
echo $prod_list_contents;
I am trying to incorporate a color table into the same div that is being displayed through this same variable. My color table is derived through the following bit of php:
<div >
<?php
$ctr = 0;
$clr=1;
foreach($products_options_array as $products_options_array2) {
$ctr++;
//if it is a color image, or a color hex
if($products_options_array2['color_image']!='')
{
$clr_sec = "style=\"background-image:url(images/pattern/".$products_options_array2['color_image'].") !important; height:28px; width:28px;\"" . "class=\"testy\"";
}
else {
$clr_sec = "style=\"background-color:".$products_options_array2['color_code']." !important; height:28px; width:28px;float:left;\"" . "class=\"testy\"";
}
?>
<div <?php echo $clr_sec;?>> </div>
<?php
$clr++;
} ?>
</div>
I have tried adding it into the prod_list_contents variable but I don't think I can because of the php here is not METHODS. and requires semi colons etc. Is there another way to go about this? Or am I able to add it in and I'm just being stupid?
If i understand your question correctly you wan to add $clr_sec to $prod_list_contents.
Just change
<div <?php echo $clr_sec;?>> </div>
to
$prod_list_contents.='<div '.$clr_sec.'</div>';

Categories