Select first 10 rows and display into a slideshow - php

I'm creating a website and I want to display my data into a slideshow from database. The slideshow is made using bootstrap.
My code looks like this:
<?php
include 'header.php';
require 'includes/dbh-inc.php';
function make_query($conn)
{
$query = " SELECT * FROM notes ORDER BY id ASC";
$result = mysqli_query($conn,$query);
return $result;
}
function make_slide_indicators($conn)
{
$output = '';
$count = 0;
$result = make_query($conn);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'" class="active"></li>
';
} else {
$output .= '<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'"></li>';
}
$count = $count + 1 ;
}
return $output;
}
function make_slides($conn)
{
$output = '';
$count = 0;
$result = make_query($conn);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '<div class="item active">';
} else {
$output .= '<div class="item">';
}
$output .= '<img src="images/notes/'.$row["image"]
.'" alt="'.$row["denomination"].'" style=" width: 100%;height: 300px;" />
<div class="carousel-caption">
<h3>' .$row['price'].'</h3>
</div>';
$count = $count +1;
}
return $output;
}
?>
<main>
<div class="container">
<h2 align="center">Pedigree Notes</h2>
<br />
<div id="dynamic_slide_show" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php echo make_slide_indicators($conn); ?>
</ol>
<div class="carousel-inner">
<?php echo make_slides($conn); ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</main>
<?php
include 'footer.php';
?>
It is working somehow, but it displays both pictures from database in first slide, half slide first pictures the other half the second picture.
Can you please tell me what I'm supposed to do?

In your function make_slides, change the class item to carousel-item
if($count == 0)
{
$output .= '<div class="carousel-item active">';
} else {
$output .= '<div class="carousel-item">';
}

Related

How to set first menu active in mega menu?

I am trying to create a hoverable mega menu, all works fine except the active one.
I checked in source codes and active class is added, but it doesn't show up the content until mouseenter on it.
see image please :
And this is when I go on it with mouse, it show up with no problem.
I gues I need to set main (FINANCE is main menu in pic) menu active, but dont know how to pass it to submenu.
Here is my full code :
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([0]);
if ($stmt_menu->rowCount() > 0) {
$query = $stmt_menu->fetchAll();
foreach ($query as $row) {
if ($row['cat_type'] == 'mega') {
?>
<li class="dropdown megamenu-fw mega-li-<?php echo $row['catId']; ?>">
<?php echo $row['catName']; ?><span class="caret"></span>
<ul class="dropdown-menu megamenu-content dropdown-top" role="menu" aria-expanded="true" data-mega-ul="<?php echo $row['catId']; ?>">
<li>
<?php
$stmt_menu = $pdo->prepare("SELECT * FROM categories WHERE parentId = ? LIMIT 5");
$stmt_menu->execute([$row['catId']]);
if ($stmt_menu->rowCount() > 0) {
$subquery = $stmt_menu->fetchAll();
?>
<div class="sub-menu-left">
<ul class="nav-sub-categories">
<?php
$i = 0;
foreach ($subquery as $subq) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<li data-category-filter="<?php echo $subq['cat_seo_url']; ?>-<?php echo $subq['catId']; ?>" class="li-sub-category<?php echo $actives; ?>"><?php echo $subq['catName']; ?></li>
<?php
$i++;
}
?>
</ul>
</div>
<div class="sub-menu-right">
<?php
$i = 0;
foreach ($subquery as $subcats) {
$actives = '';
if($i == 0){
$actives = ' active';
}
?>
<div class="sub-menu-inner filter-<?php echo $subcats['cat_seo_url']; ?>-<?php echo $subcats['catId']; ?><?php echo $actives; ?>">
<div class="row row-menu-right">
<div class="col-sm-3 menu-post-item">
<div class="post-item-image">
<a href="is-allowance-instantly-strangers-applauded">
<img src="assets/img/img_bg_md.png" data-src="uploads/images/202203/image_380x226_624239afd7295.jpg" alt="Is allowance instantly strangers applauded" class="lazyload img-responsive img-post" width="1" height="1"/>
</a>
</div>
<h3 class="title"><?php echo $subcats['cat_seo_url']; ?> Is allowance instantly strangers applauded</h3>
<p class="post-meta">
admin
<span>Mar 28, 2022</span>
<span><i class="icon-comment"></i>2</span>
<span class="m-r-0"><i class="icon-eye"></i>894</span>
</p>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
<?php
}
?>
</li>
</ul>
</li>
<?php
} elseif ($row['cat_type'] == 'dropdown') {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">test</a></li>';
} else {
echo ' <li class="nav-item"><a class="nav-link" href="' . $row["cat_seo_url"] . '">' . $row["catName"] . '</a></li>';
}
}
}

Adding Image to Carousel Slider

enter image description here
<div id="demo" class="carousel slide" data-ride="carousel">
<?php $count = count($veriler);?>
<ul class="carousel-indicators">
<?PHP
$i=0;
for ($i=0;$i<$count;$i++) { ?>
<li data-target="#demo" data-slide-to="<?php echo $i; ?>"></li>
<?php } ?>
</ul>
<div class="carousel-inner">
<?php
$j=0;
foreach ($veriler as $row) { ?>
<div class="item <?php if($j==0) { echo "active"; } ?>">
<div style="max-width:1100px; max-height:500px;">
<img src=" <?php echo $row["haberekle_konu"]; ?>" alt="Los Angeles">
</div>
<div class="carousel-caption">
<h3> <?php echo $row["haberekle_baslik"]; ?> </h3>
</div>
</div>
<?php $j++; } ?>
</div>
<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>
i want to add in Carousel Slider a few images.
i posted image and caption from admin panel and i saved to database. when i extracted data from the database, result that: See i added the image. No picture display.
enter image description here
<?php
$veriler = $db->query("SELECT * FROM haberlerekle",PDO::FETCH_ASSOC)->fetchAll();
?>
i think, that codes need.
The symbol the top left cornor(https://i.stack.imgur.com/7UgIN.jpg) indicates a "broken link".
This means, the path does not point on a File.
Or u maybe need a absolute/relativ path(HTML showing broken image)
Make sure the path of the image u try to paste in the src-attribute is realy there.
U can try it by pasting the content of
$row["haberekle_konu"]
behind the URL.
If the image not appear, the path ist not correct.
Eventually i found the codes i was looking for. i found youtube. The codes and youtube link below.
Youtube Link
slider.php
<?php
$veriler = $db->query("SELECT * FROM haberlerekle ORDER BY haberekle_id DESC LIMIT 12",PDO::FETCH_ASSOC)->fetchAll();
/* $veriler = $db->query("SELECT * FROM haberlerekle ORDER BY haberekle_id DESC LIMIT 7"PDO::FETCH_ASSOC)->fetchAll(); */
?>
<div id="demo" class="carousel slide" data-ride="carousel">
<ul class="carousel-indicators">
<?php
$i = 0;
foreach ($veriler as $row) {
$actives = '';
if($i == 0) {
$actives = 'active';
}
?>
<li data-target="#demo" data-slide-to="<?php echo $i; ?>" class="<?php echo $actives; ?>"></li>
<?php $i++; } ?>
</ul>
<div class="carousel-inner">
<?php
$i = 0;
foreach ($veriler as $row) {
$actives = '';
if($i == 0) {
$actives = 'active';
}
?>
<div class="carousel-item <?php echo $actives; ?>">
<img src="upload/images/<?php echo $row['haberekle_konu']; ?>" width="832" height="502">
<div class="carousel-caption">
<p><?php echo $row["haberekle_baslik"]; ?></p>
</div>
</div>
<?php $i++; } ?>
</div>
<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>
admin.php
<?php
if(isset($_POST['resimyukle'])) {
$yazi = $_POST['haberekle_baslik'];
$yukleklasor = "../../../upload/images";
$tmp_name = $_FILES['yukle_resim']['tmp_name'];
$name = $_FILES['yukle_resim']['name'];
$boyut = $_FILES['yukle_resim']['size'];
$tip = $_FILES['yukle_resim']['type'];
$uzanti = substr($name,-4,4);
$rasgelesayi1 = rand(10000,50000);
$rasgelesayi2 = rand(10000,50000);
$resimad = $rasgelesayi1.$rasgelesayi2.$uzanti;
//dosya var mı kontrol
if(strlen($name) == 0) {
echo "Bir Dosya Seçiniz";
exit();
}
//boyut kontrol
if($boyut > (1024*1024*3)) {
echo "Dosya Boyutu Çok Fazla";
exit();
}
//tip kontrol
if($tip != 'image/jpeg' && $tip != 'image/png' && $uzanti != '.jpg') {
echo "Yalnızca jpeg veya png formatında olabilir";
}
move_uploaded_file($tmp_name, "$yukleklasor/$resimad");
$resimsor = $db->prepare("INSERT INTO haberlerekle set haberekle_konu=:ad, haberekle_baslik=:baslik");
$resimyukle = $resimsor->execute(array('ad'=> $resimad, 'baslik'=> $yazi));
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="haberekle_baslik" size="100"></input><br><br>
<input type="file" name="yukle_resim" /><br>
<input type="submit" value="Yükle" name="resimyukle" />
</form>
Thank you for your help.

Loading images to Bootstrap carousel from a MySQL database using PDO (fetchall problem)

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>

PHP Find value in array and echo something different

I have the following code:
$media = $db->query("SELECT * FROM uploaded_photos WHERE user_id='".$profile->id."' LIMIT 20");
$picheck = $db->query("SELECT * FROM users WHERE id='".$profile->id."'");
$picheck = $picheck->fetch_object();
if($media->num_rows == 0) {
$uploaded_photos = 0;
} else {
$uploaded_photos = array();
while($photo = $media->fetch_object()) {
$photos[] = array('type'=>'uploaded','id' => $photo->id, 'path' => $photo->path);
}
}
//IN A RELATED PHP FILE:
<?php
$c=0;
if(!empty($photos)) {
for($i = 0; $i < count($photos); $i++) {
if($photos[$i]['type'] == 'instagram') {
} else {
$c++;
echo '<div class="col-sm-3 col-md-3 col-lg-3 thumbnail m-5">';
echo '<div class="image-container">';
echo '<img src="'.$system->getDomain().'/uploads/'.$photos[$i]['path'].'" class="img-responsive">';
echo '
<div class="caption">
<h4>';
//HERE IS MY PROBLEM, IF THE PATH OF THE USERS CURRENT PROFILE PHOTO IS THE SAME AS THE PATH OF THE PHOTO DISPLAYED, IT SHOULD ECHO SOMETHING DIFFERENT.
if(in_array($picheck->profile_photo, $photos['path'], true)){ echo ' <i class="fa fa-exclamation" data-toggle="tooltip" data-placement="right" data-title="Your Profilepicture" placeholder="" data-original-title="" title=""></i> ';
}
else { echo ' <i class="fa fa-trash" data-toggle="tooltip" data-placement="right" data-title="'.$lang['Delete_Photo'].'" placeholder="" data-original-title="" title=""></i> ';
}
echo '
<i class="fa fa-user" data-toggle="tooltip" data-placement="left" data-title="'.$lang['Profile_Photo'].'" placeholder="" data-original-title="" title=""></i>
</h4>
</div>
';
echo '</div>';
echo '</div>';
}
}
}
if($c==0) {
echo 'YOU DONT HAVE ANY PICTURES';
}
?>
I have been searching high and low to solve this, I hope someone can help me out. I would like to thank all who put efford in helping me out on this beforehand.
The problem is because of this line,
if(in_array($picheck->profile_photo, $photos['path'], true)){ ...
^^^^^^^^^^^^^^^
The second argument of in_array() function i.e $photos['path'] is wrong because $photos array doesn't have an index named path.
So the correct condition for if block would be,
if(in_array($picheck->profile_photo, $photos[$i], true)){ ...

delete row from db using php/mysqli

Please take a look at the loop:
$category = $mysqli->query('SELECT * FROM category');
$divcounter = 0;
while($row = $category->fetch_assoc()) {
print '<div class="item col-xs-12 col-sm-6 col-md-3 col-lg-2">';
print '<h4><span class="glyphicon glyphicon-triangle-right" aria-hidden="true"></span> '.$row["catname"].'</h4>';
print '<div id="tvb'.$row["id"].'">';
$link = $mysqli->query("SELECT * FROM links WHERE category='{$row["id"]}'");
while($row = $link->fetch_assoc()) {
print '<li>'.$row["name"].' <span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span></li>';
}
$link->free();
print '</div>';
print '</div>';
$divcounter++;
!($divcounter % 2) ? print '<div class="clearfix visible-sm-block"></div>' : '';
!($divcounter % 4) ? print '<div class="clearfix visible-md-block"></div>' : '';
!($divcounter % 6) ? print '<div class="clearfix visible-lg-block"></div>' : '';
}
Please advise how do I create a button that sit next to the li to delete that current listed row from db? Thanks
<span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span>
This is the content of delete.php:
if(isset($_POST['catid']) and is_numeric($_POST['catid']))
$id=$_POST['catid'];
$query = "DELETE FROM category WHERE id=(?)";
$statement = $mysqli->prepare($query);
$statement->bind_param('i', $id);
$statement->execute();
$statement->close();
while($row = $link->fetch_assoc()) {
print '<li>delete'.$row["name"].' <span class="glyphicon glyphicon-remove text-danger" aria-hidden="true"></span></li>';
}

Categories