delete row from db using php/mysqli - php

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>';
}

Related

How to show database record in div with good looking row by row separated by " | "

I have show data record from db but can't good style, i want show that row by row with separated by " | ", one to many relationship data record.
<?php
$kolom = 4;
$i = 1;
$mysqli = new mysqli("x", "y", "z", "a");
$result = mysqli_query($mysqli, "SELECT * FROM daftar_penawaran ORDER BY RAND () LIMIT 4");
while($row = mysqli_fetch_array($result)){
if (($i) % $kolom == 1){
echo '<div class="row">';
}
echo '<div class="smart-column-compact wide25-compact">
<div class="boxmodeloffer" style="width:100%;">
<div class="w3-black">
<h3 style="padding:15px;">'.$row['JudulPenawaran'].'</h3>
</div>
<div class="" style="padding:10px;">
<img class="img-thumbnail class="w3-left w3-circle w3-margin-right" src="'.$row['Gambar'].'" width="100%" height="150px"/>
<label class="smart-bottom-border">'.$row['isipenawaran'].'</label><br>
<i class="bi bi-person-fill" style="padding-right:5px;"></i><span>
<label>'.$row['Penerbit'].'</label><br>
<label id="getnomerpenawaran" style="padding-right:5px;">'.$row['NoPenawaran'].'</label><span><label>'.$row['Timestamp'].'</label></span>
</div>
Detail
</div></div>';
if (($i) % $kolom == 0){
echo '</div>';
}
$i++;
}?>

How to correctly generate the chapter design structure and its video series?

The following method works perfectly, it correctly generates the following HTML structure, you can see it in the following link: https://jsfiddle.net/L6wo42ar/
$id_course = 1;
$stmt = $con->prepare("SELECT id_chapter, chapter
FROM tbl_chapters
WHERE id_course=?
");
$stmt->bind_param("i", $id_course);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) {
$stmt->bind_result($id_chapter, $chapter);
while ($stmt->fetch()) {
$stmtA = $con->prepare("SELECT preview,
title_video,
description_video,
type_format,
multimedia,
detail,
time_video,
url_website
FROM tbl_videos
WHERE id_course=? AND id_chapter=?
");
$stmtA->bind_param("ii", $id_course, $id_chapter);
$stmtA->execute();
$stmtA->store_result();
if ($stmtA->num_rows>0) {
$stmtA->bind_result($preview, $title_video, $description_video, $type_format, $multimedia, $detail, $time_video, $url_website);
$Div = '<div class="modules">
<button class="accordion">'.$chapter.'</button>';
$Nav = '<div id="enlaces" class="section videolist" style="display: none;">';
while ($stmtA->fetch()) {
//if($type_format === 'video') {
$Nav.='<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">'.$title_video.'</span></div>
<div><span class="time-video">'.$time_video.'</span></div>
</div>
</a>';
//}
}
}
$Nav .= '</div></div>';
echo $Div;
echo $Nav;
}
}
So since I needed to generate that same structure and my proposed code generates it the same without problems, here you can see the result: https://jsfiddle.net/L6wo42ar/
Now the question is that I am avoiding making a query within a cycle, for this I combine both tables resulting in the following:
$id_course = 1;
$stmt = $con->prepare("SELECT c.chapter,
v.preview,
v.title_video,
v.description_video,
v.type_format,
v.multimedia,
v.detail,
v.time_video,
v.url_website
FROM tbl_chapters c
JOIN tbl_videos v ON v.id_course = c.id_course AND v.id_chapter = c.id_chapter
WHERE c.id_course=?
");
$stmt->bind_param("i", $id_course);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows>0) {
$stmt->bind_result($chapter, $preview, $title_video, $description_video, $type_format, $multimedia, $detail, $time_video, $url_website);
$tempChapter = "";
$i = 1;
while ($stmt->fetch()) {
if ($tempChapter != $chapter) {
$Div = '<div class="modules">
<button class="accordion">'.$chapter.'</button>';
$Nav = '<div id="enlaces" class="section videolist" style="display: none;">';
echo $Div;
$tempChapter = $chapter;
}
//if($type_format === 'video') {
$Nav.='<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">'.$title_video.'</span></div>
<div><span class="time-video">'.$time_video.'</span></div>
</div>
</a>';
//}
$Nav .= '</div></div>';
echo $Nav;
}
} else {
echo "no data!";
}
I knew that I was going to have problems especially with the subject of the title of the chapter that was going to be repeated due to the number of records in the video table, in the end I solved it as follows:
if ($tempChapter != $chapter) {
echo $chapter;
$tempChapter = $chapter;
}
But, here is the problem that the HTML structure is generating wrong, here the result: https://jsfiddle.net/3Lfgbpkc/
Can you explain how to solve this error?
To avoid your confusions, Simply create an array of chapters and its videos like this
$chapters = array();
while ($stmt->fetch()) {
$chapters[$chapter][] = array(
"title" => $title_video,
"time" => $time_video
);
}
Your $chapters array will be like this,
Then use this function to print the html contents
printChapters($chapters);
function printChapters($chapters) {
foreach ($chapters as $chapter => $videos) {
echo '<div class="modules">
<button class="accordion">'.$chapter.'</button>
<div id="enlaces" class="section videolist" style="display: none;">';
foreach ($videos as $key => $video) {
echo '<a class="link playing" href="#">
<div class="chapter flex">
<div><span class="check-mark"></span></div>
<div><span class="play-title-course">'.$video["title"].'</span></div>
<div><span class="time-video">'.$video["time"].'</span></div>
</div>
</a>';
}
echo '</div>
</div>';
}
}

"i want to display number of records using select option with ajax"

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit 0,6' at line 1"
"I have write query to select 2 rows with select sub category but when i select option it gives me sql syntax error"
<div class="pull-right">
<div class="page-filter">
<input type="hidden" value="<?php echo $_GET['sub']?>" id="sub_val"/>
<select id="select_menu" onchange="show_all();">
<span class="text-uppercase">Show:</span>
<option value="1">10</option>
<option value="2">20</option>
<option value="3">30</option>
<option value="4">50</option>
</select>
</div>
<?php
$i = $_GET['index'];
$s = $_GET['sub'];
//echo $c;
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
$productPerPage = 6;
$startNum = ($page - 1)* $productPerPage ; // (2-1) * 3
require_once("connection.php");
if($i==0)
{
$result = mysqli_query($con,"select TOP 3 * from products where subcategory='".$s."' order by asc limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));
}
if($i==1)
{
$result = mysqli_query($con,"SELECT TOP 3 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));
}
if($i==2)
{
$result = mysqli_query($con,"SELECT TOP 4 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));
}
if($i==3)
{
$result = mysqli_query($con,"SELECT TOP 5 * FROM products where subcategory='".$s."' limit ".$startNum.",".$productPerPage) or die(mysqli_error($con));
}
while($row = mysqli_fetch_array($result))
{
?>
<div class="col-md-4 col-sm-6 col-xs-6">
<div class="product product-single">
<div class="product-thumb">
<div class="product-label">
<span><?php echo $row[4];?></span>
<span class="sale"><?php echo $row[5];?></span>
</div>
<button class="main-btn quick-view"><i class="fa fa-search-plus"></i> Quick view</button>
<img src="<?php echo $row[9];?>" height="150" width="100" style="margin-top:60px;"/>
</div>
<div class="product-body">
<h3 class="product-price"><?php echo $row[3];?><del class="product-old-price"><?php echo $row[2];?></del></h3>
<div class="product-rating">
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star"></i>
<i class="fa fa-star-o empty"></i>
</div>
<h2 class="product-name"><?php echo $row[1];?></h2>
<div class="product-btns">
<button class="main-btn icon-btn"><i class="fa fa-heart"></i></button>
<button class="main-btn icon-btn"><i class="fa fa-exchange"></i></button>
<button class="primary-btn add-to-cart"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
</div>
</div>
</div>
</div>
<?php
}
?>
"I want number of records using select option but it gives me sql syntax error"
This error occurs with the MySQL version.
You can use the following query (example):
SELECT * FROM products LIMIT 0,5
In your case:
"SELECT *
FROM products
where subcategory=$s
ORDER BY column_name ASC
LIMIT $startNum , $productPerPage"

Select first 10 rows and display into a slideshow

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">';
}

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)){ ...

Categories