Friends thumb in the picture when the picture was selected, I could not do the Active incident.
<div id="carousel-product" class="carousel slide carousel-thumbnails mb-3" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<?php
$alt_img = '';
$query = $db->query("SELECT * FROM urun_img WHERE urun_id = '{$urun['id']}'", PDO::FETCH_ASSOC);
if($query->rowCount())
{
foreach($query as $row)
{
echo '<div class="carousel-item active"><img class="img-responsive" src="upload/'.$row['img'].'"></div>';
$alt_img.= '<li data-target="#carousel-product" data-slide-to="1" class="verso-transition active"><img src="upload/'.$row['img'].'" alt="'.$urun['baslik'].'"></li>';
}
}
?>
</div>
<ol class="carousel-indicators -mb--2">
<?php echo $alt_img; ?>
</ol>
</div>
Inside your if statement, you could simply do this:
$row_count = 1;
foreach($query as $row)
{
echo '<div class="carousel-item'; if($row_count == 1) { echo ' active'; } echo '"><img class="img-responsive" src="upload/'.$row['img'].'"></div>';
$alt_img.= '<li data-target="#carousel-product" data-slide-to="1" class="verso-transition active"><img src="upload/'.$row['img'].'" alt="'.$urun['baslik'].'"></li>';
$row_count++;
}
To explain this:
You create a $row_count variable with 1 at the value (this is the first row you're going to treat in your loop)
You create a condition to say "if this is the first row only, then we add the class"
You increment $row_count at the end of the loop, so for each new row, it will change its value to 2, then 3, then 4, etc... which then won't match the if($row_count == 1) condition.
Related
I'm using PDO to insert images from a database to a Bootstrap carousel. By that I mean ( loading The image path ), but the problem is when I use fetchall(); or fetch I get all rows value like this:
require_once '..\Config.php';
$dbCon = "mysql:host=$host;dbname=$db_name";
$conn = new PDO($dbCon, $username, $password);
$getquery = $conn->prepare('SELECT (imageurl) FROM special');
$getquery->execute();
$result = $getquery->fetchall();
echo $result['imageurl'];
The output is: Efexor.jpg path upload/17.jpg upload/17.jpg upload/19.jpg upload/18.jpg upload/18.jpg* that is all rows from imageurl column, so when I loop foreach ($result) to load images from database to a Bootstrap carousel. It dose't work
<div id="demo" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ul class="carousel-indicators">
<?
$i = 0;
foreach($result as $row){
$actives = '';
if($i == 0){
$actives ='active';
}
?>
<li data-target="#demo" data-slide-to="<?= $i; ?>" class="<?= $actives;?>"></li>
<? $i++ ; }?>
</ul>
<!-- The slideshow -->
<div class="carousel-inner">
<?
$i = 0;
foreach($result as $row){
$actives = '';
if($i == 0){
$actives ='active';
}
?>
<div class="carousel-item <?= $actives;?>">
<img src="<?= $row['imageurl']?>">
</div>
<? echo $row['imageurl'];?>
<? $i++; }?>
</div>
<!-- Left and right controls -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
</div>
Try like this:
<!-- The slideshow -->
<div class="carousel-inner">
<?
$i = 0;
$full_url = 'YOUR URL AND PATH TO IMAGE FOLDER HERE';
foreach($result as $row){
$actives = '';
if($i == 0){
$actives ='active';
}
?>
<div class="carousel-item <?= $actives;?>">
<img src="<?= $full_url . $row['imageurl']?>">
</div>
<? echo $full_url . $row['imageurl'];?>
<? $i++; }?>
</div>
thanks for taking a look at my question.
I'm trying to get PHP to output bootstrap rows that only contain 2 col-md-6 columns, instead of outputting 1 row with 1 col for each iteration.
Searching here on Stackoverflow I found a solution that makes sense but when I implement it, the HTML that I get makes no sense!
I should be getting this:
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
...But I'm getting this:
<div class="col-md-6">
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
<div class="row">
<div class="col-md-6"></div>
<div class="col-md-6"></div>
</div>
</div>
CODE:
<?php
require_once('somedb.php');
$query = mysqli_query($conn, "SELECT * FROM notyourbusiness");
$rowCount = mysqli_num_rows($query);
$i = 0;
echo '<div class="row">';
if($rowCount > 0){
while($row = mysqli_fetch_assoc($query)){
?>
<?php echo '<div class="col-md-6">'; ?>
<img src="img/project/<?php echo $row['thumb'] ?>" class="work-thumbnail" width="100">
<h2><?php echo $row["name"]; ?></h2>
<?php echo '</div>'; ?>
<?php
$i++;
if ($i%2 == 0) echo '</div><div class="row">';
} ?>
<?php } ?>
</div>
Any help will be greatly appreciated, thanks!
Can you provide us more information? It seems that the first is comming from outside of this php code.
You can try this
<?php
$query = mysqli_query($conn, "SELECT * FROM notyourbusiness");
$rowCount = mysqli_num_rows($query);
$i = 0;
if($rowCount > 0){
while($row = mysqli_fetch_assoc($query)){
$row_draw = ($i % 2 == 0) ? true : false;
# Start row div
if ($row_draw)
{
print "<div class='row'>";
}
# Print Column
print "<div class='col-md-6'>";
?>
<img src="img/project/<?php echo $row['thumb'] ?>" class="work-thumbnail" width="100">
<a href="javascript:void(0);">
<h2><?php echo $row["name"]; ?></h2>
</a>
<?php
print "</div>";
# End row div
if ($row_draw)
{
print "</div>";
}
$i++;
}
}
?>
The search results should display like this
But my results are stacking on top of each.
Here is my code :
<div class="container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("thesis") or die(mysql_error());
$search = trim( $_POST['SearchKeywords']);
$query = " SELECT * FROM new_data WHERE Product_Title or Product_link LIKE'%$search%' ";
$sql = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($sql);
$count == 0;
if ($count == 0) {
echo "Sorry, Nothing Found !!";
}else{
while ($row = mysql_fetch_array($sql))
{
$img = $row ['Product_Img'];
$link = $row ['Product_link'];
$title = $row ['Product_Title'];
$price = $row ['Product_Price'];
?>
<div class="card">
<div class="front alert alert-success">
<?php echo "$title";
echo "<img src='$img' width='80px' height='100px'>";
echo "$price"; ?>
</div>
</div>
<?php
};
};
?>
</div> <!-- Container -->
Those div blocks are inside a container.
I added a bootstrap class in order for better a design.
You can use thumbnails with custom content
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="..." alt="...">
<div class="caption">
<h3>Thumbnail label</h3>
<p>...</p>
<p>Button Button</p>
</div>
</div>
</div>
</div>
I used a counter inside while loop.
Which will check, when there are already 4 blocks/ products in a single row then it will create a new row
<?php
if($productCount == 0)
{
echo "<div class='row'>"; }
}
$productCount++;
if($productCount==4)
{
echo "</div>" ;
$productCount = 0;
}
?>
Overall I would like to echo all elements of the DB table 'products' as buttons on a row, and for each 10th element, a lineshift should be implemented. Each line should be surrounded by a specific ().
I have printed the code I have been working with to do this:
products_backend.php:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
$i++;
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if ($i%10 == 0):
echo "<br>";
endif;
}
}
And then I have the "products.php page", where the extracted data is shown:
<div class="container-fluid">
<section class="wrapper">
<div class="row">
<div class="row-eq-height">
<?php require("products_backend.php"); ?>
</div>
</div>
</section>
</div>
But the output of this is all the products in the DB printed on one line, without any lineshift for every 10th line:
The image shows the output, however due to the window size, only 11 products are shown, but i keeps going outside the window.
To summarize, I want the output to insert a lineshift for every 10th product printed, and that each row should be inside the div, like in the manipulated Picture below: (Notice, that in order to create the Picture, I have just copied the output from the row line in order to create the second row, Thus I do not want the same output in the second row as in the first row)
So what am I doing wrong in my code above?
the class "col-md-1" looks like a floating div
so to break this floating divs, try this :
echo "<br style=\"clear : both;\">";
You can use % moduler to add div for after every 10 record;
Updated code :
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 0;
foreach($table_row as $data) {
if(($i%10) == 0){ echo "<div class="clearfix"></div>";}
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
$i++;
}
}
Try this:
$stmt = $db->prepare("SELECT * FROM products");
$stmt->execute();
$count = $stmt->rowCount();
if($count > 1){
$table_row = $stmt->fetchAll();
$i = 1;
echo "<div>";
foreach($table_row as $data) {
?>
<div class="col-md-1 cash_register pants">
<button class="table_add" value="Bukser">
<h4><?php echo $data['product_name']; ?></h4>
</button>
</div>
<?php
if(($i%10) == 0){ echo "</div><div>";}
$i++;
}
echo "</div>";
}
I need a little help with a php loop for OpenCart.
I need to do is wrap a div around the output of the data every 4 loops.
I have the following
<?php foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php } ?>
I get this
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
But what I'm hoping to get is
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
Any help would be greatly appreciated :)
try this
<?php
$i=0;
$wrap_count = 4; // you can change this no of divs to wrap
foreach ($categories as $category)
{
$i+=1;
if($i%$wrap_count==1)
{
echo '<div class="row">';
}
?>
<div class="col-lg-3 col-md-3">.....</div>
<?php
if($i%$wrap_count==0)
{
echo '</div>';
}
}
if($i%$wrap_count!=0)
{
echo '</div>';
}
?>
<?php
$i = 0;
foreach ($categories as $category) {
if (($i % 4) === 0) {
echo "<div class='row'>";
}
echo '<div class="col-lg-3 col-md-3"></div>';
if (($i % 4) === 3) {
echo "</div>";
}
$i++;
}
?>
Ps, If $categories is a non keyed array, ie array('a', 'b', 'c') then you can get rid of the i = 0; and i++; and change the foreach to foreach ($categories as $i => $category) {
And as zerkms mentioned in a comment, the magic here is coming from % (Modulo operation), which is essentially like a clock- the numbers will increment from 0 upwards until they hit 12, at which point it is reset to 0 again. So the clock is said to be modulo twelve. In this instance we're using modulo four to cyclically print our our opening and closing row elements.
Modulo four would be the sequence 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc
Try this :-
<div class="row">
<?php
$i = 1;
foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php if ($i % 4 == 0){ ?>
</div><div class="row">
<?php } ?>
<?php $i++; ?>
<?php } ?>
You may use a counter variable. Initially set it to equal 0. Then for each loop check its value. If it is equal to 0 then output <div class="row"> and add 1 to the variable's value. If its value is > 0 and < 5 then output <div class="col-lg-3 col-md-3">.....</div> and add 1 to it's value. If it is equal to 5 output </div> and reset the counter variable to 0.
Something like (code tested):
<?php
$counter = 0;
foreach ($categories as $category) {
If ($counter == 0) {
echo '<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
';
$counter++;
} elseif ($counter > 0 && $counter < 3) {
echo '<div class="col-lg-3 col-md-3">.....</div>
';
$counter++;
} elseif ($counter == 3) {
echo '<div class="col-lg-3 col-md-3">.....</div>
</div>
';
$counter = 0;
}
}
if ($counter > 0) {
echo '</div>
';
}
?>
The Modulo operator will be equal to zero when used in a comparison with the number of items you would like in a row. So you can output your starting <div> there.
To add a closing div you will need to check the modulo of 4 is equal to 3 or if we have reached the total number of items, to prevent a missing close </div>.
<?php
// set a counter
$rowCount = 0;
// store the total number of categories minus 1 as we will be
// counting from 0 with the counter
$categoryTotal = count($categories) - 1;
foreach($categories as $c){
if($rowCount % 4 == 0){
echo '<div class="row">';
}?>
<div class="col-lg-3 col-md-3""></div>
<?php
// add your closing div if it is the end of a row or if there are no more items
if($rowCount % 4 == 3 || $rowCount == $categoryTotal){
echo '</div>';
}
// increment your counter
$rowCount++;
} ?>
I have an alternative option that might work depending on your array.
$new_array = array_chunk($your_array, 4);
foreach($new_array as $group_of_$four){
echo '<div class="row">';
foreach($group_of_four as $one){
// Single Item
}
echo '</div>';
}
Well, add this to your code:
//before foreach
$i = 0;
// inside the foreach before its closing }
$i++;
}//this closes the foreach
And then before adding any div (probably in the beginning of the foreach:
if($i % 4 == 0)
{
echo "<div class='each-four-iterations'>";
}
And before the $i++ statement, add this :
if($i % 4 == 0)
{
echo "</div>";
}
<?php
$wrap_count = 2; // you can change this no of divs to wrap
foreach ($categories as $category) :
$i+=1;
if($i%$wrap_count==1):
echo '<div class="row">';
endif;?>
<div class="col-lg-3 col-md-3">.....</div>
if($i%$wrap_count==0) : echo '</div>'; endif; endforeach;
?>
Works Fine For me
Try this one. It works for me.
<?php $count = 1; ?>
<?php foreach ($model->userProfile->portfolio as $item): ?>
<?php if ($count % 4 == 1): ?>
<div class="row" style="margin-bottom: 30px;">
<?php endif ?>
<div class="col-md-3">
</div>
<?php if ($count % 4 == 0): ?>
</div>
<?php endif ?>
<?php $count++ ?>
<?php endforeach ?>