Pick items from while loop php - javascript

For a school project I have to make a simple webshop.
To display my featured products on the hopepage I used a while loop. But now I want to add a shoppingcart. So after you pressed the button add to cart the right data has to be picked and display on another page. I will do that with cookies. So all I need is to get the right data in a variable.
I tried to use the result right away but that didn't work.
<?php
$sql = "SELECT * FROM product WHERE featured = 1 AND nr_available > 0";
$resultquery = $conn->query($sql);
?>
<div class="preview">
<?php while ($result = mysqli_fetch_assoc($resultquery)) : ?>
<div class="productpreview">
<div class="previewheader">
<img src="images/products/<?=$result['picture']; ?>" class="previewimg" style="max-width: 300px;">
</div>
<div class="description">
<h2><?=$result['title']; ?></h2>
<p><?=$result['description']; ?></p>
<p>Size: <?=$result['size']; ?></p>
<p>Price: € <?=$result['price']; ?></p>
<button class="add" onclick="window.location.href='buy.php'">Buy</button>
<button class="add" onclick="window.location.href='shoppingcart.php'">Add to cart</button>
</div>
</div>
<?php endwhile; ?>
</div>
I want to store the right data in variables which I can display on another page.

I have tried this:
<?php
$sql = "SELECT * FROM product WHERE featured = 1 AND nr_available > 0";
$resultquery = $conn->query($sql);
?>
<div class="preview">
<?php while ($result = mysqli_fetch_assoc($resultquery)) : ?>
<div class="productpreview">
<div class="previewheader">
<img src="images/products/<?=$result['picture']; ?>" class="previewimg" style="max-width: 300px;">
</div>
<div class="description">
<h2><?=$result['title']; ?></h2>
<p><?=$result['description']; ?></p>
<p>Size: <?=$result['size']; ?></p>
<p>Price: € <?=$result['price']; ?></p>
<button class="add" onclick="window.location.href='buy.php'">Buy</button>
<button class="add" onclick="window.location.href='shoppingcart.php/id=<?=$result['product_id']; ?>'">Add to cart</button>
</div>
</div>
<?php endwhile; ?>
</div>
and this is the page where I want to show the user his shoppingcart data.
<?php
include "core/core.php";
include "parts/head.php";
include "parts/navbar.php";
include "core/helpers.php";
if ($_SESSION['loggedIn'] == 'true') {
echo "welkom";
$productid = $_GET['product_id'];
$sql = "SELECT * FROM product WHERE id = '$productid'";
$query = $conn->query($sql);
$result = mysqli_fetch_assoc($query);
} else {
echo "log in ";
}
?>
<h1>Your cart</h1>
<p><?=$result['title']; ?></p>
<?php
include "parts/footer.php";
?>
for some reason this doesn't work. I have no errors.

Related

Give different ID to each element in for loop in PhP

This is my output, clicking like button should add +1 to the likes column in MySQL .
I use a while loop to iterate buttons
For example, the "button" is displayed multiple times in the picture below. I have a tag in the while loop, so it outputs the button several times. And that name comes from the database.
MY Question. All the buttons in the will have the same ID. Currently, the user can only click the first button. I would like to give each element a different ID if possible. Then I would like to use jQuery to add a click event. So, if I click on the 4th button, the like count for that comment should be increased.
What I need.
How I can assign a different ID to each element in the far loop, so it does only make the first Image clickable but instead all elements' clickables?
<?php
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class="single-item">
<div class="cmt_user"><div class="circle2">
<h5>
<?php
$name = $row['name'];
$f_letter = strtoupper($name[0]);
echo $f_letter;
?>
</h5>
</div>
<h4><?php echo $row['name']; ?></h4>
</div>
<p><?php echo $row['comment']; ?></p><div class="lcr">
<ul>
<li id = "modified" > </li>
</ul>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" class="form">
<button onclick="imageClick(<?php echo $row['id_vnr']; ?>)" name="like" value="<?php echo $row['id'] ?>" class="like_btn"><i class="fa fa-heart"></i> Like</button>
</form>
this is Php Code to insert likes count
<?php
$like = $_POST['like'];
if($like){
$sql = "UPDATE comments set likes = likes + 1 where id = '".$row['id']."'";
echo $sql;
$result=mysqli_query($link,$sql);
}
?>
It's really simple. do it like this
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<div class="single-item">
<div class="cmt_user"><div class="circle2">
<h5>
<?php
$name = $row['name'];
$f_letter = strtoupper($name[0]);
echo $f_letter;
?>
</h5>
</div>
<h4><?php echo $row['name']; ?></h4>
</div>
<p><?php echo $row['comment']; ?></p><div class="lcr">
<ul>
<li id = "modified" > </li>
</ul>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" class="form">
<input name="like" value="1">
<input name="row_id" value="<?php echo $row['id'] ?>">
<button class="like_btn"><i class="fa fa-heart"></i> Like</button>
</form>
And get POST values like this
<?php
$like = $_POST['like'];
$row_id = $_POST['row_id'];
if(isset($like) && $like == 1){
$sql = "UPDATE comments set likes = likes + 1 where id = '".$row_id."'";
echo $sql;
$result=mysqli_query($link,$sql);
}
?>

loop returns same value multiple times

I have a WordPress loop where I list the data. I create a button dynamically and set their ids.
When I set that id into a textbox I can see that all buttons have different ID because I increment the number each time. Until here everything is okay and working fine.
but need to use javascript for setting the textbox value with value of button ID.
I use this following code:
<script>
jQuery(document).ready(function($){
$('.p-name-x').click(function(){
$('#dynamic-field').val($(".the-prod-counter").val());
});
alert($(".the-prod-counter").val());
});
</script>
on my $('.p-name-x').click(function() x should be changed with the number if ID i get from the loop.
on my alert it always gives the first ID. For example if I have 3 buttons the alert gives 3 times the first id!
How can get the alert to show the other numbers as well?
------------- EDIT --------------
Here is part of my code
<?php
$type = 'stiel_privatkunden';
$args=array('stiel_privatkunden_cat' => $post_slug, 'post_type' => $type, 'posts_per_page'=>-1, 'order'=>'ASC');
$my_query = new WP_Query($args);
$i=1;
$idh=1;
while ($my_query->have_posts()) : $my_query->the_post();
if($i%2==0) {
?>
<div class="row" style="background:#f3f3f3" id="<?php the_field('prod_slug'); ?>">
<div class="col-xs-12 col-sm-6">
<figure class="pro-innerpage-img">
<?php the_post_thumbnail(); ?>
</figure>
</div>
<div class="col-xs-12 col-sm-6" style="padding:20px;">
<div class="title_content">
<div class="the-title"><?php the_title();?></div>
<div class="the-desc"><?php the_field('description'); ?></div>
<?php if( get_field('passende_Produkte') ): ?>
<div class="vc_toggle vc_toggle_square vc_toggle_color_black vc_toggle_color_inverted vc_toggle_size_md">
<div class="vc_toggle_title" tabindex="0"><h4>passende Produkte</h4><i class="vc_toggle_icon"></i></div>
<div class="vc_toggle_content" style="display: none;">
<p><?php the_field('passende_Produkte'); ?></p>
</div>
</div>
<?php endif; ?>
<?php if( get_field('link_mehr_erfahren') ): ?>
<span class="theme-button-inner">mehr erfahren</span>
<?php endif; ?>
<?php if( get_field('link_jetz_anfragen') ): ?>
<span class="theme-button-inner">jetzt anfragen</span>
<?php endif; ?>
</div>
<!--<input type="text" name="the-prod-name" class="the-prod-name-<?php echo $idh; ?>" value="<?php the_field('prod_slug');?>">-->
<input type="hidden" name="the-prod-counter" class="the-prod-counter" value="<?php echo the_field('prod_slug') . '-'. $idh; ?>">
<!--<input type="text" name="the-prod-idk" class="the-prod-idk" value="<?php //echo $idh; ?>"> -->
<script>
jQuery(document).ready(function($){
$('.p-name').click(function(){
$('#dynamic-field').val($(".the-prod-counter").val());
});
//alert($(".the-prod-counter").val());
});
</script>
</div>
</div>
<br/>
<?php } else { ?>

How to add different numbers at automatically added divs?

Im working on a confession website, and on the website I have a section "Top of the Week" which displays 3 most liked confessions from current week, it displays them in a horizontal slider by most liked order, so it goes most liked, second most liked, third most liked confession. The thing that Im trying to do is to put #1, #2, #3 number on each one, but I can't achieve that because I only style one div and website automatically create two other and add confession to it.
This is my php + html code that I use to display them:
<div class="sidebox">
<?php
$select = "SELECT confessions.confessId,
(IFNULL(confessions.firstName, '')) AS firstName,
confessions.confessText,
DATE_FORMAT(confessions.postDate,'%b %d %Y %h:%i %p') AS postDate,
hasImage,
UNIX_TIMESTAMP(confessions.postDate) AS orderDate,
confessions.isActive,
(SELECT COUNT(*) FROM views WHERE views.confessId = confessions.confessId ) as totalViews,
(SELECT COUNT(*) FROM likes WHERE likes.confessId = confessions.confessId ) as totalLikes,
(SELECT COUNT(*) FROM dislikes WHERE dislikes.confessId = confessions.confessId ) as totalDislikes
FROM
confessions
WHERE isActive = 1
ORDER BY totalViews DESC , orderDate DESC limit 3";
$resss = mysqli_query($mysqli, $select) or die('-3' . mysqli_error()); ?>
<div id="sticky-nav" style="height:36px;" class="absolute" style="z-index:0">
<div id="width-limit">
<div class="options">
<ul class="menu">
<li><a class="carousel_prev previous" href="#"><b style="font-size: 20px;">‹</b></a></li>
<li><a style="opacity:1;padding-top:1px;position:relative;left:-54px;"><i class="fas fa-crown" id="crownicon"></i> Top of the Week</a> </li>
<li><div class="menu-clear"></div></li>
<li><a class="carousel_next next" href="#"><b style="font-size: 20px;">›</b></a></li>
</ul>
</div>
<div class="options" id="opt2" style="float:right; width:280px;display:none;">
<ul style="display: inline-block;float:right;">
</ul>
</div><div class="options" id="opt2" style="float:right; width:280px;display:none;">
<ul style="display: inline-block;float:right;">
</ul>
</div>
<div id="small-logo"></div>
<div class="clearfix"></div>
</div>
</div><div class="sidecontainer">
<div class="slick">
<?php
while ($row = mysqli_fetch_assoc($resss)) {
// Get Total Comments
$comssql = "SELECT 'X' FROM comments WHERE confessId = ".$row['confessId']." AND isActive = 1";
$commentstotal = mysqli_query($mysqli, $comssql) or die('-4'.mysqli_error());
$totComments = mysqli_num_rows($commentstotal);
if ($totComments == '1') { $comText = 'Comment'; } else { $comText = 'Comments'; }
if ($row['totalViews'] == '1') { $viewText = 'View'; } else { $viewText = 'Views'; }
$shareURL = $set['installUrl'].'page.php?page=view&confession='.$row['confessId'];
?>
<div class="confession" style="margin-left: 0;width: 300px;">
<div class="left"><span class="label2 label-confess1"><?php echo $row['totalViews'].' '.$viewText; ?></span></div>
<div class="right"><span class="bestthisweek">
<?php if ($row['totalLikes'] == '12') { echo "Top of the Day!"; } else { echo "Top!"; } ?>
</span></div>
<div class="confessionstyle" style="margin-top:20px;"><p>
<font color="#fff3b2">
<?php
if ($filterProfanity == '1') {
echo nl2br(htmlspecialchars(filterwords($row['confessText'])));
} else {
echo nl2br(htmlspecialchars($row['confessText']));
}
?>
</font>
</p></div>
<input type="hidden" id="confessId" name="confessId_<?php echo $count; ?>" value="<?php echo $row['confessId']; ?>" />
<?php
$chkLikes = mysqli_query($mysqli,"SELECT 'X' FROM likes WHERE confessId = ".$row['confessId']." AND likeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
$hasLike = mysqli_num_rows($chkLikes);
$likeCSS = $hasLike > 0 ? 'text-info' : 'white';
$chkDislikes = mysqli_query($mysqli,"SELECT 'X' FROM dislikes WHERE confessId = ".$row['confessId']." AND dislikeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
$hasDislike = mysqli_num_rows($chkDislikes);
$dislikeCSS = $hasDislike > 0 ? 'text-danger' : 'white';
?>
<div class="confession-actions">
<div class="likes" style="width: 75px;">
<span class="label2 label-confess first liked">
<a href="" id="likeIt_<?php echo $row['confessId']; ?>" class="likeIt_<?php echo $count; ?> <?php echo $likeCSS; ?>" style="text-decoration:none;outline:none;">
<i class="fas fa-thumbs-up"></i> <span style="color:white;" id="likesVal_<?php echo $row['confessId']; ?>"><?php echo $row['totalLikes']; ?></span>
</a>
</span>
</div>
<div class="dislikes" style="width: 75px;">
<span class="label2 label-confess disliked">
<a href="" id="dislikeIt_<?php echo $row['confessId']; ?>" class="dislike_<?php echo $count; ?> <?php echo $dislikeCSS; ?>" style="text-decoration:none;outline:none;">
<span style="color:white;" id="dislikesVal_<?php echo $row['confessId']; ?>"><?php echo $row['totalDislikes']; ?></span> <i class="fas fa-thumbs-down"></i>
</a>
</span>
</div>
<?php if ($row['hasImage'] != '0') { ?>
<span class="label label-confess"><i class="fa fa-picture-o img"></i></span>
<?php } ?>
<div class="comments">
<div class="divide" style="width: 75px;"><div id="comments-hvr"><a href="page.php?page=view&confession=<?php echo $row['confessId']; ?>">
<i class="fa fa-comments"></i> <?php echo $totComments.' '; ?>
</a></div></div>
</div>
<div class="divide2" style="width: 75px;"><div class="fb-share-button" style="top:-6.5px;transform: scale(0.93);"
data-href="page.php?page=view&confession=<?php echo $row['confessId']; ?>"
data-layout="button_count"></div></div></div>
<div class="clearfix"></div>
</div>
<?php
}
?>
</div>
I tried everything and this is the best solution that I have, but I can't figure out what should I put inside if, elseif, else, I need something that will place #1 on first, #2 on second, #3 on third confession. So I would need something like this but something that will not require me to manually insert > likes.
<?php if ($row['totalLikes'] > '11') { echo "#1"; } elseif ($row['totalLikes'] > '5') { echo "#2"; } else { echo "#3"; }?>
Please visit my website, its still in development, but check Top of the Week on the right and you'll have a clue of what I actually want: http://confessions.byethost31.com
FINAL:
<?php $i = 1; ?>
<?php
echo '#'.$i;
$i++;
?>
Easiest way to to just create a variable that increments with every while iteration.
$i = 1; // set initial value
while ($row = mysqli_fetch_assoc($resss)) {
echo 'This number will grow by 1 for every row - '.$i;
$i++; // this is equal to $i = $i + 1; so will increment by 1 every time.
}
EDIT:
Instead of attempting to assume what the total likes would be, it would be easier to sort them by total likes (putting the most liked first and assigning this #1).
ORDER BY totalLikes DESC, totalViews DESC, orderDate DESC
You can then just use
echo '#'.$i;
Where you want to display the number, you can do this because you have LIMIT 3 so only the top 3 will display.

modal popup keep populating only first item data on all item buttons

I am implementing cloth shopping website in which i am uploading cloth items with their images and description from admin panel. on home page i am retrieving all cloth items that are in stock and displaying using while loop. in order to view details i have put "view button" which on click opens modal popup and displays whole description of the item. But i am facing problem that every button of item on click only displays first item description in modal popup on all item button. I want that every item button should display item owns description. but it keeps populating first item data on every button.
Code:
<?php
$link = mysql_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysql_select_db("login", $link) or die("Cannot select the database!");
$sql = "SELECT * FROM add_stock";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {?>
<div class="grid_element">
<div class="show-image">
<form method="post" action="" id="myform" >
<img src="<?php echo $row['image'] ?>" name="image" onclick="openModal()" id="image" name="image1" target="_parent">
<figcaption>
<b>Product Code: <?php echo $row['id']; ?> <br/>
<?php echo $row['dress_description']; ?> <br/>
PKR <?php echo $row['price']; ?></b>
</figcaption>
</form>
<!-- view button -->
<button class="update fa fa-eye" id="popupview" onclick="openModal1()" title="View" type="image" /></button>
<!-- View Item modal popup -->
<div id="mpopupBox" class="mpopup">
<!-- mPopup content -->
<div class="mpopup-content">
<div class="mpopup-head">
<span class="close7">×</span>
<h2 style="font-family:Cooper Black;">Item Description</h2>
</div>
<div class="mpopup-main" ><br/>
<img src="<?php echo $row['image']; ?>" style="width: 300px; height: 300px; border-radius: 25px;">
<p style="margin-top: -250px; margin-left: 380px; "><font size="4"><b>Product Code: <?php echo $row['id']; ?> <br/>
PKR <?php echo $row['price']; ?> <br/>
Brand: <?php echo $row['brand_name']; ?> <br/>
Gender: <?php echo $row['gender_name']; ?><br/>
Category: <?php echo $row['category_name']; ?><br/>
Size: <?php echo $row['size_name']; ?> <br/>
Material: <?php echo $row['material_name']; ?> <br/>
Description: <?php echo $row['dress_description']; ?></font></b> </p>
<button style="margin-left: 380px; margin-top: 20px; width: 135px;" class="button button4 add-to-cart"><i class="fa fa-shopping-cart"></i>Add to Cart</button>
</div>
<div class="mpopup-foot">
<!-- <p>created by CodexWorld</p> -->
</div>
</div>
</div>
<script type="text/javascript">
var mpopup = document.getElementById('mpopupBox');
// get the link that opens the mPopup
var mpLink = document.getElementById("popupview");
// get the close action element
var close7 = document.getElementsByClassName("close7")[0];
// open the mPopup once the link is clicked
mpLink.onclick = function () {
mpopup.style.display = "block";
}
var imagess = document.querySelectorAll('button[title="View"]');
for (var i = 0, len = imagess.length; i < len; i++) {
imagess[i].addEventListener('click', openModal1);
}
function openModal1() {
mpopup.style.display = "block";
}
// close the mPopup once close element is clicked
close7.onclick = function () {
mpopup.style.display = "none";
}
</script>
</div>
</div>
1) The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead. Stop using mysql functions.
2) Why to connect database connection on each and every file. Create one common db file where database connection code is written and include in each file. Otherwise, when database or username or password get changed, then you have to change it in each and every file. So, to avoid it, create one common db file.
db.php
<?php
$link = mysql_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysql_select_db("login", $link) or die("Cannot select the database!");
?>
3) IDs can't be same through out a page. So, change it accordingly.
YourPage.php
<?php
include("db.php");
$result = mysql_query("SELECT * FROM add_stock");
while ($row = mysql_fetch_assoc($result)) {?>
<div class="grid_element">
<div class="show-image">
<form method="post" action="" id="myform" >
<img src="<?php echo $row['image'] ?>" name="image" onclick="openModal()" id="image" name="image1" target="_parent">
<figcaption>
<b>Product Code: <?php echo $row['id']; ?> <br/>
<?php echo $row['dress_description']; ?> <br/>
PKR <?php echo $row['price']; ?></b>
</figcaption>
</form>
<button class="update fa fa-eye openPopUp" data-url="ajax_pop_up.php?id=<?php echo $row['id'];?>" title="View" type="image" /></button>
</div>
</div>
<?php }?>
<style>
.displayBlock{display:block;}
.displayNone{display:none;}
</style>
<div id="mpopupBox" class="mpopup displayNone">
</div>
<script>
//For Opening Pop Up
$(document.body).on('click', '.openPopUp', function () {
$("#mpopupBox").removeClass("displayNone").addClass("displayBlock");
$.ajax({url:$(this).attr('data-url'),cache:false,success:function(result){
$("#mpopupBox").html(result);
}});
});
//For Closing Pop Up
$(document.body).on('click', '.close7', function () {
$("#mpopupBox").removeClass("displayBlock").addClass("displayNone");
});
</script>
4) Create one common ajax pop up file in same directory, where pop up content will appear.
ajax_pop_up.php
(if you are planning to change file name here, change in data-url of YourPage.php page, both are related to each other)
<?php
include("db.php");
$result = mysql_query("SELECT * FROM add_stock WHERE id=".$_GET['id']);
while ($row = mysql_fetch_assoc($result)){?>
<!-- mPopup content -->
<div class="mpopup-content">
<div class="mpopup-head">
<span class="close7">×</span>
<h2 style="font-family:Cooper Black;">Item Description</h2>
</div>
<div class="mpopup-main" ><br/>
<img src="<?php echo $row['image']; ?>" style="width: 300px; height: 300px; border-radius: 25px;">
<p style="margin-top: -250px; margin-left: 380px; "><font size="4"><b>Product Code: <?php echo $row['id']; ?> <br/>
PKR <?php echo $row['price']; ?> <br/>
Brand: <?php echo $row['brand_name']; ?> <br/>
Gender: <?php echo $row['gender_name']; ?><br/>
Category: <?php echo $row['category_name']; ?><br/>
Size: <?php echo $row['size_name']; ?> <br/>
Material: <?php echo $row['material_name']; ?> <br/>
Description: <?php echo $row['dress_description']; ?></font></b> </p>
<button style="margin-left: 380px; margin-top: 20px; width: 135px;" class="button button4 add-to-cart"><i class="fa fa-shopping-cart"></i>Add to Cart</button>
</div>
<div class="mpopup-foot">
<!-- <p>created by CodexWorld</p> -->
</div>
</div>
<?php }?>
Similar Question
GET PHP variable value from HTML data-id
Passing data via Modal Bootstrap and getting php variable?
Quick Links
Can an html element have multiple ids?
Go through it. And, if any problem comes, feel free to ask.

having issue opening modal on javascript and php

i am facing some kind of issue here in my javascript i think, it was working to open a small windows, but i don't know how it's not working now. here's below my javascript code ad my php and html codes
<?php
require_once '../core/init.php';
$id = $_POST['id'];
if(is_numeric($id) && $id > 0 && $id == round($id, 0)){
$id = $_POST['id'];
} else {
exit;
}
$id = (int)$id;
$sql = "SELECT * FROM products WHERE id = '$id'";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$brand_id = $product['brand'];
$sql = "SELECT brand FROM brand WHERE id = '$brand_id'";
$brand_query = $db->query($sql);
$brand = mysqli_fetch_assoc($brand_query);
$sizestring = $product['sizes'];
$size_array = explode(',', $sizestring);
?>
<!-- Details Modal -->
<?php ob_start(); ?>
<div class="modal fade details-1" id="details-modal" tabindex="-1" role="dialog" aria-labelledby="details-1" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" onclick="closemodal()" aria-label="close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title text-center"><?php echo $product['title']; ?></h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="center-block">
<img src="<?php echo $product['image']; ?>" alt="<?php echo $product['title']; ?>" class="details img-responsive">
</div>
</div>
<div class="col-sm-6">
<h4>Details</h4>
<p><?php echo $product['description']; ?></p>
<hr>
<p>Price: $<?php echo $product['price']; ?></p>
<p>Brand: <?php echo $brand['brand']; ?></p>
<form action="add_cart.php" method="post">
<div class="form-group">
<div clss="col-xs-3">
<label for="quantity">Quantity:</label>
<input type="text" class="form-control" id="quantity" name="quantity">
</div>
</div>
<div class="form-group">
<label for="size">Size:</label>
<select name="size" id="size" class="form-control">
<option value=""></option>
<?php foreach($size_array as $string) {
$string_array = explode(':', $string);
$size = $string_array[0];
$quantity = $string_array[1];
echo '<option value="'.$size.'">'.$size.' ('.$quantity.' Available)</option>';
} ?>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-default" onclick="closemodal()">Close</button>
<button class="btn btn-warning" type="submit"><span class="glyphicon glyphicon-shopping-cart"></span>Add To Cart</button>
</div>
</div>
</div>
</div>
<script>
function closemodal(){
jQuery ('#details-modal').modal('hide');
setTimeout(function(){
jQuery('#details-modal').remove();
jQuery('.modal-backdrop').remove();
},500);
}
</script>
<?php echo ob_get_clean(); ?>
After looking at your var_dump($product); result:
array(10) {
["id"]=> string(1) "1"
["title"]=> string(12) "Levi's Jeans"
["price"]=> string(5) "29.99"
["list_price"]=> string(5) "39.99"
["band"]=> string(1) "1"
["categories"]=> string(1) "6"
["image"]=> string(24) "images/products/men4.png"
["description"]=> string(16) "Description here"
["featured"]=> string(1) "1"
["sizes"]=> string(14) "28:3,32:2,36:5"
}
It's obvious that you do not have a field named 'brandin yourproducts` table.
The nearest field name to brand is band which I think is a typo. So if you change this line of your code:
`$brand_id = $product['brand'];`
To this
`$brand_id = $product['band'];`
Then you will solve the error
Notice: Undefined index: brand
And in this query $sql = "SELECT * FROM products WHERE id = '$id'"; since $id is integer, there is no need to enclose it in single quotes and you can have your query like this:
$sql = "SELECT * FROM products WHERE id = $id";
Also looking at your queries, you have two queries which you can combine them to a single query using a simple join
You have `"SELECT * FROM products WHERE id = '$id'" and "SELECT brand FROM brand WHERE id = '$brand_id'"
In table products, the field band is foreign key to table brand field id So you can simply write this query:
$sql = "SELECT products.*, brand.brand FROM products left join brand on products.band = brand.id WHERE products.id = $id";
So your php section at the top of your detailsmodal.php file will become like this:
<?php
require_once '../core/init.php';
$id = $_POST['id'];
if(is_numeric($id) && $id > 0 && $id == round($id, 0)){
$id = $_POST['id'];
} else {
exit;
}
$id = (int)$id;
$sql = "SELECT products.*, brand.brand FROM products left join brand on products.band = brand.id WHERE products.id = $id";
$result = $db->query($sql);
$product = mysqli_fetch_assoc($result);
$sizestring = $product['sizes'];
$size_array = explode(',', $sizestring);
?>
and then only at this line of your html:
<p>Brand: <?php echo $brand['brand']; ?></p>
You have to change it to
<p>Brand: <?php echo $product['brand']; ?></p>
Note: Since you are using mysqli, It's highly advised to use prepared statements instead of just putting your variable directly in your query

Categories

Resources