PDO MySQL product loop - php

Im a real beginner when it comes to queries and PDO, i need some assistance in a project i am busy with. What i want to accomplish is to display products from the database. However the template style i am using forces me to show 3 products per row, is there a way to show 3 products per row and loop the code once there is more than 3 (if there is 5 products, the first 3 will display in the first row, and the rest in the second).
Here is the template i am using, note the div class "top-box", this forces that only 3 products can be shown per row.
<div class="top-box">
<?php
$sql = "SELECT * FROM _products WHERE category = '$cat'";
$result = dbConnect()->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
$cols = $result->columnCount(); // Number of returned columns
// Generate ADS Feed for each ROW
foreach($result as $row) {
echo '<div class="col_1_of_3 span_1_of_3">
<a href="product.php?i=' . $row['model'] . '">
<div class="inner_content clearfix">
<div class="product_image">
<img src="images/' . $row['model'] . '.jpg" alt=""/>
</div>
<div class="price">
<div class="cart-left">
<p class="title">' . $row['name'] . '</p>
</div>
<div class="clear"></div>
</div>
</div>
</a>
</div>';
}
} else {
echo "<p>No products available at this moment, contact us for more information!</p>";
}
?>
<div class="clear"></div>
</div>

You can solve it like this:
<?php
if ($counter % 3 == 0 && $counter!=$total_row_fetched) {
echo '<div class="clear"></div>';
echo '</div>';
echo '<div class="top-box">';
}
if($counter==$total_row_fetched){
echo '<div class="clear"></div>';
echo '</div>'; // it will close the last open div
}
++$counter;
?>

You can use a count variable inside your products foreach-loop. Every three products you can close the top-box and open a new one (This is an assumption as I don't know exactly how your styles work).
At the end of the foreach-loop:
if ($count != 0 && $count % 3 == 0) {
echo '<div class="clear"></div>';
echo '</div>'; // close last .top-box
echo '<div class="top-box">';
}
++$count;
I don't see how your question is connected to PDO. Keep in mind that using unescaped variables inside a query is potentially dangerous. Have a look here for some help: https://stackoverflow.com/a/60496/2516377

Add a counter and use % arithmetic operator to calculate column number.
$counter=0;
foreach (...) {
$column_number=$counter % 3;
$counter++;
}

Related

php foreach display limit and after it display the rest until the limit again

I want to show 16 data from database per page and foreach 8 of them display
<div>There are 8 Data </div>
after showing the rest 8 to display again
<div>There are 16 Data </div>
something like that if possible of course
I tried to use the break after showing 8 data and trying to continue again but without success
the code I tried:
$sn_count = 1;
$html = '';
$display_datadiv_every = 8;
foreach($result as $point){
$html .= "<div class=\"name\">".$sn_count."</div>"
. "<div class=\"pointsurname\">"
. $point['name']
. "</div>";
if($sn_count++ % $display_datadiv_every == 0)
{
echo '<div class="card d-flex mt-2 mb-4" style="width: auto;
flex-direction: row;
height: 110px;">
<div class="card-body">
<h5 class="card-title">There are".$sn_count." data</h5>
</div>
</div>';
}
} echo $html;
}}
and it displays me like this:
<h5 class="card-title">There are".$sn_count." data</h5>
<h5 class="card-title">There are".$sn_count." data</h5>
foreach 8 data displayed one div and
$point['name'] 16 times
$point['name']
First define a counter variable outside the loop.
Then put a condition inside the loop that it should show the first 8 result in the first8 div and show rest result in else condition of second div.
Then counter++ inside loop
<?php
$counter = 0;
foreach($result as $point){
if($counter < 8){
?>
<div class="first8"><?php echo $point; ?></div>
<?php } else { ?>
<div class="second8"><?php echo $point; ?></div>
<?php } ?>
<?php $counter++;
}
?>

What is wrong with my if statement?

I had the below code which worked fine until I added an if statement to restrict the loop to only run on certain items in the array. I am now only getting a blank page which suggests there is an error somewhere in my code since adding the if statement, but I can't figure out where.
I'd really appreciate any help on solving this, as well as suggestions on how I could have solved myself (I'm still new to PHP and not sure how to effectively debug this type of issue).
Nb. There is an opening <?php tag not shown in the below snippet.
foreach ($portfolioProjects as $portfolio) {
if ($portfolio['featureContent'] == "Yes") {
?>
<div class="row featurette">
<?php
//for each odd number, change the layout so it looks nice :)
if ($loopCount % 2 == 0) {
echo '<div class="col-md-7">';
} else {
echo '<div class="col-md-7 col-md-push-5">';
}
?>
<h2 class="featurette-heading"><?php echo $portfolio[title]; ?> <span class="text-muted"><?php echo $portfolio[languages]; ?></span>
<?php
//Check array for newTag which will be added to show a tag to the user
if ($portfolio[newTag] == "Yes") {
echo '<span class="label label-success test pull-right"> New!</span>';
}
?></h2>
<p class="lead"><?php echo $portfolio[blurb]; ?></p>
</div><!--end of column1-->
<?php
if ($loopCount % 2 == 0) {
echo '<div class="col-md-5">';
} else {
echo '<div class="col-md-5 col-md-pull-7">';
}
?>
<img class="featurette-image img-responsive center-block" data-src="holder.js/200x200/auto" alt="200x200" src="assetts/200x200.gif" data-holder-rendered="true">
</div>
</div>
<?php
//if statement to stop divider being added if last item in array
$loopCount = $loopCount + 1;
if ($loopCount != $itemsCount) {
echo '<hr class="featurette-divider">';
}
}
}
?>
The easiest way to find out what the problem is, to check your webserver's log file, or turn on error_reporting and display_errors in your php.ini file.
Are newTag and blurb constants or keys in the $portfolio array? If they are keys, you should use apostrophes.
if ($portfolio['newTag'] == "Yes") {
and
<p class="lead"><?php echo $portfolio['blurb']; ?></p>
You forgot to wrap array keys in quotes
`$portfolio['title']
$portfolio['languages']
$portfolio['newTag']
$portfolio['blurb']`

Looping 2 set of results twice

I have a page where I need to pull the titles of 3 of the latest records in one bootstrap row and then few lines further in the next row I need to pull the image of each record (only 3) again.
How can I do this?
On your loop construct two series of HTML entries, one for your titles and another one for your pictrures. Then echo each HTML string to the div you want them to be.
<?php
titlesHTML = "";
imagesHTML = "";
foreach ($result as $row) {
$contentTitle = $row['cont_title'];
$contentImage = $row['cont_picture'];
$titlesHTML .= "<p>" . $contentTitle . "</p>";
$imagesHTML .= "<p>" . $contentImage . "</p>";
// if you want not to echo the image location but the image itself try the following code instead:
// $imagesHTML .= "<img src='" . $contentImage . "'>";
}
?>
<div class="row">
<div class="col-em-12">
<?php echo $titlesHTML; ?>
</div>
<div>
<div class="row">
<div class="col-em-12">
<?php echo $imagesHTML; ?>
</div>
<div>

Bootstrap how to foreach with columns inside

I have a foreach and I want to place 4 columns then break in a new row and start again but for some reason i got the column number 5(test2) bad pushed like this picture:
should be like this:
here is the code to generate that:
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
Secciones
</div>
<div class="panel-body">
<div class="row">
#foreach ($boards as $board)
<div class="col-md-3">
<h3>{{$board->boaName}}</h3>
#foreach ($subboards as $subboard)
#if($subboard->boaId == $board->id)
<ul>
<li>{{$subboard->subboaName}}</li>
</ul>
#endif
#endforeach
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
</div><!--End container-->
Try moving the foreach loop outside of the <div class="row">? If that doesn't work do you think you could mock this up in a jsFiddle example?
You need a test after 4 iterations to close and open again the row div.
If you know how many columns to expect, you could do the following (I've ommited your inner content of each column for my example - I've also left the counter in so you can see the incrementation for testing):
$i = 1;
foreach($boards as $board) {
if($i == 1 || $i == 5 || $i == 9) {
echo "<div class='row'>";
echo "<div class='col-md-3'>" . $i++ . "</div>";
}
elseif($i%4 == 0) {
echo "<div class='col-md-3'>" . $i++ . "</div>";
echo "</div>";
} else {
echo "<div class='col-md-3'>" . $i++ . "</div>";
}
}
What's happening here is if the column count is the 4th iteration, it should echo out an opening <div class="row"> as well as a column. If the current column is divisible by 4, it should echo out a column with an extra closing </div> (to close the row class.)
If the current count is neither of the above, it simply echo's out a column.
You can, of course, mod the above if the amount of potential columns is unknown.

Displaying each item in an array a number of times

Hi guys this seems to be a very basic question,
I have an array of categories, and would like to display each one 4 times.they are all floating left, so each category is displayed as a row of 4 boxes.
however there are some divs which are boxes with border that are displayed with each iteration. It seems to be working but i dont know if it's the correct way to do it, as all the
categories are not organised. I want a header of the specific category to appear first thing each time the loop jumps to the next item in array;
Any help is welcome. please see code below.
[CODE]
<section class="prodList">
<?php
$loop = 4;
$prodNum = 0;
$categories = array("All","Clothes","Gadgets","Games");
foreach($categories as $cat){
echo "<h2> new cat: ".$cat."</h2>";
while($prodNum < count($categories)*$loop){
echo "<div class='viewerWishlistProdContainer'>
<div class='prodImageContainer'>
</div>
</div>
";
echo " <p>
<button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>
</p>";
$prodNum ++;
}
}
[/CODE]
Would this work?
<section class="prodList">
<?php
$categories = array("All","Clothes","Gadgets","Games");
foreach($categories as $cat){
/** Since you know it should repeat four times, why not echo 4 times? */
echo "<h2> new cat: ".$cat."</h2>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
?>
<!-- You don't have to echo plain HTML -->
<div class='viewerWishlistProdContainer'>
<div class='prodImageContainer'>
</div>
</div>
<p>
<button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>
</p>
<?php } /** closing the foreach block */ ?>
</section>

Categories