PHP getting <div> in between after row count divided by 4 - php

I need to divided all data into 4 columns. i have used this method but its not coming column property
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
$tempcount = 1;
if ($stmt1->execute(array())) {
while ($row = $stmt1->fetch()) {
?>
<?php if ($tempcount == $pages) { ?>
<div class="column">
<div class="ui bulleted list">
<?php } ?>
<?php
if ($tempcount == $pages) {
$pages = $pages + $pages; ?>
</div>
</div>
<?php }
$tempcount++;
}
} ?>
how i can get this columns after getting count of data i have and add those divs in between.
for an Example 20 data rows 20/4 = 5 after each 5 data, i need start and end to be added
<div class="column">
<div class="ui bulleted list">
</div>
</div>
<div class="column">
<div class="ui bulleted list">
</div>
</div>
// And so on
result will be like this
thank you very much

Here's a proper code with comments
$tempcount = 0;
$pages = ceil($rowcount / 4);
while ($row = $stmt1->fetch()) {
// if result of % (Modulo) is zero - you need to start new column
if ($tempcount % $pages == 0) {?>
<div class="column">
<div class="ui bulleted list">
<?php
}?>
ITEM
<?php
// if result of % (Modulo) is (pages - 1) - you need to close previous column
if ($tempcount % $pages == ($pages - 1)) {?>
</div>
</div>
<?php
}
$tempcount++;
}
// check if you have to close previous column
// because it was not closed in a while loop
if (0 < $rowcount) {
if ($tempcount % $pages != 0) {?>
</div>
</div>
<?php
}
}

Whilst the modulo approach mentioned above will help in these situations, I often find that writing your loops from a different perspective can help in sorting out the situation, and making things more readable.
Rather than looping through every row, and then inserting the columns where you think they need to be. Instead loop every item, but in a controlled count, wrapped by the columns.
I've switched the code to using echo just due to personal preference, you can still use php-breakouts instead if preferred.
Please note this is untested example code, just to illustrate the point:
<?php
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
if ($rowcount) {
$stmt1->execute(array());
do {
$group = '';
$group .= '<div class="column">';
$group .= '<div class="ui bulleted list">';
for ( $i=0; $i<$pages; $i++ ) {
$row = $stmt1->fetch(); if ( !$row ) { break; }
// presumably something would be done with $row here
$group .= '';
}
$group .= '</div>';
$group .= '</div>';
echo $group;
} while ( $row );
}

% modulo will help you PHP operators. While you fetch, use it to create your divider. If using div and if needed, I usually add a tiny div with clear: both to make sure it breaks the line after the 4/8/12... results.
$cell++;
$jumpline = ($cell % 4) ? "" : "<div class=\"spacer\"></div><br />";
$endit = ($cell % 4) ? "" : "</div>";

Related

PHP: Dividing mysql result to four div

I have category table which include 35 category inside. I want them aline into four div with one while loop.
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
//same with pullcategories, just LIMIT $count,$per //
while ($topcats = mysqli_fetch_object($topcats)) {
?>
<div class="col3">
<ul class="list-unstyled list-dashed">
<li>
$topcats->title
</li>
</ul>
</div>
<?php
$count = $count + $percat;
$percat = $percat * 2;
}
?>
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
$topcats = pulltopcategories($count, $percat);
This breaks your lists down in to 4 parts, and you would need to call "$topcats = pulltopcategories($count, $percat);" 4 times to be able to get through all the data.
I would change it to be something like this.
Assuming that "pulltopcategories($count, $percat)" uses count as the limit, and percat as the offset in the SQL this should work.
Please try and let me know - I can fix any mistakes if there are any (my PHP is a little rusty and i dont have data to try this with)
<?php
$count = 0;
$cat = pullcategories();
$catcount = mysqli_num_rows($cat);
$percat = ceil($catcount / 4);
while ($count < catcount){ /* Loop until you have finished all cats */
$topcats = pulltopcategories($count, $percat);
echo "<div class="col3">
<ul class='list-unstyled list-dashed'>";
while ($topcats = mysqli_fetch_object($topcats)) {
echo "<li>
<a href=''>$topcats->title</a>
</li>"
}
echo "</ul>
</div>";
$count = $count + $percat;
}
?>

show div tag in php statement

I am now working on a WordPress theme base with the Advanced Custom Fields plugin, And I want to show a <div> tag when the if statement is true. Here is my code:
<?php
$rows = get_field('classification');
$sort = get_sub_field('sort');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
<div class="bor"></div>
<h3 style="text-align:center">
<?php echo $sort; ?>
<a id="browser"></a></h3>
<div class="bor"></div>
<?php } ?>
or something like that
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
echo '<div class="bor"></div>';
echo '<h3 style="text-align:center">';
<?php echo $fenlei; ?>
echo '<a id="browser"></a></h3>';
<div class="bor"></div>
<?php } ?>
But the content of the div tag doesn't show.
Any reply is appreciated!Thank you very much.
some of your echo statement are out php tags. use this:
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$fenlei."
<a id='browser'></a></h3>
<div class='bor'></div>";
}
}
?>
If you wanted to show the html in php, i suggest you use the below code.
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo '>div class="bor"<>/div<';
echo '>h3 style="text-align:center"<';
echo $fenlei;
echo '>a id="browser"<>/a<>/h3<';
echo '>div class="bor"<>/div<';
}
}
?>
I'm guessing you're using an ACF Pro Repeater field. in this case you need to use the_row() which will set the sub-field's correct content. look at this edited example from the Docs:
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
the_sub_field('sub_field_name');
endwhile;
endif;
?>
so, I think your code should look more like:
<?php
if(have_rows('classification')):
while (have_rows('classification') ) : the_row();
// Your Code...
endwhile;
endif;
?>
And, I've learned recently you need to count the rows outside the while loop. othewise it won't catch the rows amount.

close div tag after 4 entries - mysql loop

i want my script to add a closing div tag after every fourth entrie of the db. i tried something like this:
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i=0;
i++;
if ($i == 4){
echo "</div>";}
}
?>
would be great if you can help me here. thx
1) Get names of all cities
2) Initiate an indexing variable
3) Ieterate through the loop
4) Open <div class="row"> when $i==0 (Very First time) OR $i%4==0 (When fifth entry is to be printed). (Remember you are using 0 based indexing i.e. initializing $i=0).
5) Close the div tag for <div class="row"> when fourth city name has been printed i.e. $i%3==0 (Remember you are using 0 based indexing i.e. initializing $i=0).
Here's the code:
<?php
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
if ($i == 0 || $i%4==0){ // <div class="row"> opens on first entry and every fifth entry
echo "<div class=\"row\">";
}
echo "<div class=\"col-sm-4 col-md-3\">";
echo "<h3>".$zeile['name']."</h3>";
/*
Rest of your code
*/
echo "</div>";
$i++;
if ($i % 3 == 0){
echo "</div>"; // <div class="row"> closes here on every fourth entry
}
}
?>
Try this code,
<div class="row">
$ergebnis = $mysqli->query("SELECT name FROM pages Where city = '1';");
$i=0;
while($zeile = $ergebnis->fetch_array()) {
echo "<div class=\"col-sm-4 col-md-3\">
echo "<h3>".$zeile['name']."</h3>";
..
echo "</div>";
$i++;
if ($i == 4){
echo "</div>";
$i = 0;
}
}
?>

how can I dynamically add DIV class="clear" after every third result when fetching DB when exist

I am kinda lost with what I am trying to achieve here, really need some help with the following.
While fetching DB using script provided below, I need to add <div class="clear"></div> after every third occurrence and if it is less than 3, lets say 2 or even one and there is no more after that after that last one.
Here is my script
<?
$template_query = 'SELECT * FROM Files WHERE parentpageID = :id and show_in_category = "1" ORDER BY ID asc';
$res = $db->prepare($template_query);
$res->execute(array(':id' => $current));
$add_rowNum = 0;
while ($info = $res -> fetch()){
$add_rowNum++;
$templateTitle = $info['templateTitle'];
$add_refering_url = $info['referring_url'];
$templ_link = $category_folder.$add_refering_url;
$teaserText = $info['teaserText'];
$path_to_add_images = $image_path.$info['ImagePath'].DS;
$add_img_info = $path_to_add_images.$info['templateImage'];
$add_img_alt && $add_img_title && $templateTitle = $info['templateTitle'];
list($width, $height, $type, $attr) = getimagesize($add_img_info);
$last_class = ($add_rowNum == $res->rowCount()) ? 'frame' : 'frame frame_margin';
print<<<END
<div class="$last_class">
<div class="prod">
<div class="title"><a href="$templ_link">$templateTitle
<img src="$add_img_info" alt="$add_img_alt" $attr title="$add_img_title"></a>
</div>
</div>
<div class="prd">
$teaserText
</div>
</div>
END;
}
?>
your help is highly appreciated
Use below code:
If(($add_rowNum % 3) == 0) {
echo "<div class='clear'></div>";
}
Add this code after the END syntax.
Using with %
if ($add_rowNum % 3 == 1)
echo '<div class="clear"></div>';
Try this. I think this logic will solve this problem!
$total_records = 10;
for($i=1;$i<=$total_records;$i++) {
echo "Hi";
if($i%3 == 0) {
echo "<br/>==================<br/>";
}
if(($i == $total_records) && ($total_records%3 != 0)) {
echo "<br/>==================<br/>";
}
}

how can I get these if statements working

i am trying to get my data from the database and wrap it with html tags. here is the working code so far:
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
Here I am getting everything from the database (Select * from portfolio), but in the portfolio I have, websites, demos and graphics; so I wanted to get only the data where category = "web" from the above code, so I tried this:
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
if($row->category = "web"){
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row->thumb2 . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
}
now the nested if statements do not generate the divs I need, how can I get this working
thanks for your help
I can't see your SQL based on your question, but you could just modify your SELECT query to include WHERE category="web"
This way, you're only selecting the rows you need, instead of looping over every row in that table.
Additionally, it appears that you're using assignment = instead of comparison == for your if statement.
Do you just need to have == instead of =?
if($row->category == "web"){
But it would be best to restrict the query to the results you need at the database level, unless you need the other rows for some reason.
1)You missed an equal sign:
if($row->category = "web") => if($row->category == "web")
Or better yet
if($row->category === "web")
2)If you want to only get fields with a specific category field, you can simply change your query:
[rest of your query] WHERE category="web"
OK, it should go like this, assuming the fields are sorted as follows
ID, category, website, thumb2, demo, graphics
function homethumb(){ $this->count; $i = 0;
while($row = mysqli_fetch_object($this->result))
{
if($row[1] == "web"){
$this->count++; $i++;
if($i == 1){echo '<div class="gal1">';}
echo ' <div class="gal"><img src="img/' . $row[3] . '.jpg"></div>';
if($i == 2){
echo '</div> <!-- gal1 -->';
$i=0;
}
}
}
}
and there is no need for the nested if, you can just use it in one line as follows:
if($row[1] = "web")
{
echo '<div class="gal1">';
echo ' <div class="gal"><img src="img/' . $row[3] . '.jpg"></div>';
echo '</div> <!-- gal1 -->';
}
1) Change your query to contain a WHERE category="web" clause
2) You have an assignment operator in your if clause (=), when you need an equality operator (==)

Categories