I try to loop multiple values from a database and every time I click a specific item on the menu, Javascript only catches the first value that comes up in the database even if the function is inside the loop, Even I click the last item, the first item still displays... How to fix? Here is my code. (The display of the result is on top, I didn't include it)
<?php
$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
while($rows = mysqli_fetch_array($res)){
$f_id = $rows['Menu_id'];
$fname = $rows['Food_name'];
$fprice = $rows['Price'];
?>
<div class="col-12 helv menu rounded">
<ul class="nav navbar-nav navbar-expand-md">
<li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
<li class="nav-item marginleft">
<br>
<input id="foodname" value="<?php echo $fname;?>" style="display: none;"><h5><b><?php echo $fname;?></b></h1></input><br>
<input id="foodprice" value="<?php echo $fprice;?>" style="display: none;"><h5>Php <?php echo $fprice;?>.00</h1></input>
</li>
<li class="nav-item" style="position: absolute; margin-left: 90%">
<button class="fa fa-plus btn btn-danger" onclick="add()"> Add</button>
</li>
</ul>
<br>
</div>
<script>
function add() {
var fdname = document.getElementById("foodname").value;
var fdprice = document.getElementById("foodprice").value;
document.getElementById("display").innerHTML = fdname;
document.getElementById("display1").innerHTML = fdprice;
}
</script>
<?php } ?>
<?php
// As Jeto said, you should use prepared statement
$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
while($rows = mysqli_fetch_array($res)){
$f_id = $rows['Menu_id'];
$fname = $rows['Food_name'];
$fprice = $rows['Price'];
?>
<div class="col-12 helv menu rounded">
<ul class="nav navbar-nav navbar-expand-md">
<li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
<li class="nav-item marginleft">
<br>
<input class="foodname" value="<?php echo $fname;?>" style="display: none;"><h5><b><?php echo $fname;?></b></h1></input><br>
<input class="foodprice" value="<?php echo $fprice;?>" style="display: none;"><h5>Php <?php echo $fprice;?>.00</h1></input>
</li>
<li class="nav-item" style="position: absolute; margin-left: 90%">
<button class="fa fa-plus btn btn-danger" onclick="add('<?php echo $fname;?>', <?php echo $fprice;?>)"> Add</button>
</li>
</ul>
<br>
</div>
<?php } ?>
<script>
function add(fdname, fdprice) {
document.getElementById("display").innerHTML = fdname;
document.getElementById("display1").innerHTML = fdprice;
}
</script>
First get count of how many items are in there before the while query
$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
$count_of_items = mysqli_num_rows($res);
Then run the while query, then Run a for loop inside the form as per the number of Items
for($x=0;$x<count($count_of_items); $x++){
<input id="foodname" value="<?php echo $fname[$x];?>" style="display: none;"><h5><b><?php echo $fname[$x];?></b></h1></input><br>
<input id="foodprice" value="<?php echo $fprice[$x];?>" style="display: none;"><h5>Php <?php echo $fprice[$x];?>.00</h1></input>
}
There are some more mistakes.
1) IDs are unique
Please check this https://stackoverflow.com/a/12889416/3742228
2) Duplicate JS function
Every loop you make, you create another JS function named "add". If you create 3 functions named "add", which function do you want to call when you code add()? :)
For your code there are more solutions, but it depends on what exactly you want to do. Take a look at this:
<?php
$res = mysqli_query($db,"select * from menu where Restaurant_id='$id' order by Food_name asc") or die("Error: " . mysqli_error($db));
while($rows = mysqli_fetch_array($res)){
$f_id = $rows['Menu_id'];
$fname = $rows['Food_name'];
$fprice = $rows['Price'];
?>
<div class="col-12 helv menu rounded">
<ul class="nav navbar-nav navbar-expand-md">
<li class="nav-item"><img class="rounded" src="data:image/gif;base64,R0lGODlhAQABAIAAAHd3dwAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="Generic placeholder image" width="180" height="180"></li>
<li class="nav-item marginleft">
something here
</li>
<li class="nav-item" style="position: absolute; margin-left: 90%">
<button class="fa fa-plus btn btn-danger" foodName="<?php echo $fname;?>" foodPrice="<?php echo $fprice;?>" onclick="add(this)"> Add</button>
</li>
</ul>
<br>
</div>
<?php } ?>
<script>
function add(e) {
var fdname = e.getAttribute("foodName");
var fdprice = e.getAttribute("foodPrice");
document.getElementById("display").innerHTML = fdname;
document.getElementById("display1").innerHTML = fdprice;
}
</script>
Related
I'm trying to scroll the end of the div when I open the ng-show chat window, but it only works on the second click. I've tried it in many ways but I'm not getting it to make the first click work already.
HTML CHAT:
<div class="chat">
<div class="" ng-show="chatdis">
<div class="chat-header vertical-center clearfix">
<img ng-src="user/pix.php/{{ctrl.idchat}}/f1.jpg" class="imgperfil img-circle" alt="avatar" ng-class="{'online': ctrl.useronline, 'offline': !ctrl.useronline}"/>
<div class="chat-about">
<div class="chat-with">{{ctrl.namechat}}</div>
</div>
<div class="close pull-right" style="opacity: 0.6;" ng-click="ctrl.fecharconversa()">
<i class="fa fa-times" aria-hidden="true" style="display: block;font-size: 16px;position: absolute;top: 6px;right: 8px;color: #797979;"></i>
</div>
</div> <!-- end chat-header -->
<div class="chat-history">
<ul style="margin: 0;padding: 0;">
<li ng-id="message-{{obj.id}}" class="clearfix" ng-repeat="obj in ctrl.mensagens | orderBy:'-'">
<div class="message" ng-class="{'other-message': obj.useridto == <?php echo $USER->id ?>, 'my-message float-right': obj.useridto != <?php echo $USER->id ?>}">
<p ng-bind-html="obj.smallmessage"></p>
<span class="tail-container"></span>
<span class="tail-container highlight"></span>
</div>
<div class="message-data" ng-class="{'align-right': obj.useridto != <?php echo $USER->id ?>}">
<span class="message-data-time" ng-class="{'text-right': obj.useridto != <?php echo $USER->id ?>}" ng-if="obj.useridto != <?php echo $USER->id ?>">
{{obj.firstname}} {{obj.lastname}} - {{obj.timecreated | date:"dd/MM/yyyy HH:mm"}}
</span>
<span class="message-data-time" ng-if="obj.useridto == <?php echo $USER->id ?>">
{{obj.timecreated | date:"dd/MM/yyyy HH:mm"}} - {{obj.firstname}} {{obj.lastname}}
</span>
</div>
</li>
</ul>
</div> <!-- end chat-history -->
<div class="chat-message clearfix">
<textarea name="message-to-send" ng-model="message" id="message-to-send" placeholder ="Escreva sua mensagem" rows="2"></textarea>
<button ng-click="ctrl.inseremensagem(ctrl.idchat, message, ctrl.namechat, ctrl.useronline)">Enviar</button>
</div> <!-- end chat-message -->
</div>
</div> <!-- end chat -->
CONTROLLER FUNCTION:
self.abrirconversa = function(useridfrom, name, useronline){
$rootScope.chatdis = true;
chatservice.conversachat(useridfrom).then(function(d) {
self.mensagens = d;
for(var i = 0; i < self.mensagens.length; i++){
//converte datas em Javascript
if(self.mensagens[i].timecreated != null && self.mensagens[i].timecreated != '12/31/1969 21:00'){
self.mensagens[i].timecreated = new Date(self.mensagens[i].timecreated);
}
}
self.idchat = useridfrom;
self.namechat = name;
self.useronline = useronline;
$('.chat-history').animate({scrollTop: $('.chat-history')[0].scrollHeight}, 0);
}, function(errResponse){
console.error('Error while fetching Currencies');
});
};
The controller function, I call it when I click on a list of people in the side menu. Can someone help me? :D
i have created a search page that gives results based on the entered values.What i want is to get specific details of the ad when the user presses the enroll button. But i am getting the details of all the ads instead of the one the user wants to enroll into.
here is the code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="css/basic.css">
<script src="js/jQuery/jquery.min.js"></script>
<script src="css/bootstrap/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
<script src="Forms/form-validation.js"></script>
</head>
<body>
<?php
session_start();
require 'connection.php';
//include_once 'utlities.php';
$userEmail=$_SESSION['user'];
$form_string = 'd';
$sqlQuery ='SELECT * FROM `course` WHERE `course_title` LIKE "%'.$form_string.'%" OR `user_teacher` in (SELECT user_teacher FROM teacher where email in (SELECT email from person where fname like "%'.$form_string.'%" OR lname like "%'.$form_string.'%"));';
$queryExe=mysqli_query($connection,$sqlQuery);
$row = mysqli_fetch_assoc($queryExe)
?>
<div class="panel-body">
<?php while($row = mysqli_fetch_assoc($queryExe)) { ?>
<article class="row panel panel-default">
<div class="col-xs-6 col-xs-offset-3 col-sm-3 col-sm-offset-0">
<img src="images/profile-default.jpg" alt="Lorem ipsum" />
</div>
<div class="col-xs-12 col-sm-3">
<ul class="list-group">
<li class="list-group-item">
<i class="glyphicon glyphicon-book"></i>
<span>
<?php
echo " ".$row["description"];
?>
</span>
</li>
<li class="list-group-item">
<i class="glyphicon glyphicon-tags"></i>
<span id="courseid" >
<?php
echo " Course ID ".$row["courseid"];
?>
</span>
</li>
<li class="list-group-item">
<i class="glyphicon glyphicon-tags"></i>
<span>
<?php echo" Rs. ".$row["fees"];
?>
</span>
</li>
</ul>
</div>
<div class="col-xs-12 col-sm-6">
<h3>
<a href="#" title="">
<?php
$sql_profile =
"SELECT `fname`,`lname`,`email`
FROM `person`
WHERE `email`=(
SELECT `email`
FROM `teacher`
WHERE `user_teacher`=".$row["user_teacher"]."
);
";
$result_sql=mysqli_query($connection,$sql_profile);
$tName = mysqli_fetch_assoc($result_sql);
echo $tName["fname"]." ".$tName["lname"];
$_SESSION['temail']= $tName["email"];
?>
</a>
</h3>
<h6><?php echo $tName['email']; ?></h6>
<p class="hidden-xs"><?php echo $row["course_title"]; ?></p>
<span class="plus">
<a href="#" title="Enroll" class="btn btn-success"
<?php
/*if(loggedIn()){
echo 'data-toggle="modal" data-target="#enroll "';
}else{
echo 'data-toggle="modal" data-target="#LOGIN "';
}
/**/
?>>
<i class="glyphicon glyphicon-plus" id="enroll" ></i><span>Enroll</span>
</a>
</span>
</div>
</article>
<?php
}
?>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#enroll').click(function() {
alert($("article").find("#courseid,h6").text());
});
});
</script>
</body>
</html>
Identifiers in HTML must be unique You are generating the element in a loop, which will create duplicate ID hence the generated HTML will be invalid.
Use CSS class with elements then attach event handlers using it. You can associate the arbitrary data using data-* custom attribute which can be fetched using .data().
HTML
<i class="glyphicon glyphicon-plus enroll" data-email="<?php echo $tName['email']; ?>" data-courseid="<?php echo $row['courseid']; ?>"></i><span>Enroll</span>
SCRIPT
$('.enroll').click(function() {
var email = $(this).data('email');
var courseid = $(this).data('courseid');
});
set unique identifier to what you want to show
<?php
echo "<span id=\"courseid-".$row['courseid']." \" >";
echo " Course ID ".$row["courseid"];
?>
and to the enroll button
<?php
echo "<i class=\"glyphicon glyphicon-plus\" id=\"enroll\" data-id=\"courseid-".$row["courseid"]." \">";
?>
and in your script you show the specific text
$('#enroll').click(function() {
courseid = $(this).attr('data-id');
alert($("#"+courseid+",h6").text());
});
I'm creating dynamic menu. I wanted to highlight the menu once it is selected.
We use php. just need to figure out how to append the class active to the current page. Can anyone help me with this?
<div id="sidebar-wrapper" class="sidebar-toggle">
<ul class="sidebar-nav">
<?php
$i=1;
if(count($shipments) > 0)
{
foreach($shipments as $row)
{
$shipment_id = $row['id'];
$containerdest = $row['no']."-".$row['destination'];
?>
<li id="shipment<?php echo $shipment_id; ?>"><?php echo $containerdest; ?>
</li>
<?php $i++;
}
} ?>
<li>
Logout
</li>
</ul>
</div>
</div>
Change your html line with below line
<li id="shipment<?php echo $shipment_id; ?>" class="menu"><a href="<?php echo ite_url("users/get_details/$shipment_id");?>" shipmentid="<?php echo $shipment_id; ?>" ><?php echo $containerdest; ?></a>
And write javascript code as below (assuming you have included jquery library file)
<script>
$(document).ready(function(){
var url = location.pathname;
var arr = mystr.split("/");
var id = arr[2];
$(#shipment'+id).attr('shipmentid');
$('.menu').removeClass('active'); //this will remove all active class after page loading
$('#shipment'+id).addClass('active'); // this will append active class to the current clicked menu
});
</script>
I am a beginner. I am developing an e-commerce website. My problem is that I can't seem to add multiple items in my cart. If I clicked an item it will be added to the cart but when I clicked another item it will just replaced the first item that I picked. Any suggestions on how I can fixed that? Thank you
<!-- This is my index.html. You can browse my products here. This is just a sample of my code. It's too long. Sry. -->
<form method = "GET" action = "php/addtocartprocess.php">
<p>Price: 9,199 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart1" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p>
<p>Price: 28,890 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart2" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p>
<p>Price: 46,995 Php<br><input class = "btn btn-success"type = "submit" name = "add_to_cart3" value = "Add to Cart"><span class = "glyphicon glyphicon-shopping-cart"></span></p></form>
<?php
// this is my php code. I don't know if it right or not
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$connection = mysql_connect("localhost", "root", "");
if(isset($_GET['add_to_cart1'])){
$_SESSION['prod_id'] = 1;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart2'])){
$_SESSION['prod_id'] = 2;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart3'])){
$_SESSION['prod_id'] = 3;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart4'])){
$_SESSION['prod_id'] = 4;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart5'])){
$_SESSION['prod_id'] = 5;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart6'])){
$_SESSION['prod_id'] = 6;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart7'])){
$_SESSION['prod_id'] = 7;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart8'])){
$_SESSION['prod_id'] = 8;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart9'])){
$_SESSION['prod_id'] = 9;
header("location: ../android.php");
}
if(isset($_GET['add_to_cart10'])){
$_SESSION['prod_id'] = 10;
header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart11'])){
$_SESSION['prod_id'] = 53;
header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart12'])){
$_SESSION['prod_id'] = 54;
header("location: ../iphone.php");
}
if(isset($_GET['add_to_cart13'])){
$_SESSION['prod_id'] = 55;
header("location: ../windows.php");
}
if(isset($_GET['add_to_cart14'])){
$_SESSION['prod_id'] = 56;
header("location: ../windows.php");
}
if(isset($_GET['add_to_cart15'])){
$_SESSION['prod_id'] = 57;
header("location: ../windows.php");
}
if(isset($_GET['add_to_cart16'])){
$_SESSION['prod_id'] = 58;
header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart17'])){
$_SESSION['prod_id'] = 59;
header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart18'])){
$_SESSION['prod_id'] = 60;
header("location: ../smartwatch.php");
}
if(isset($_GET['add_to_cart19'])){
$_SESSION['prod_id'] = 61;
header("location: ../computer.php");
}
if(isset($_GET['add_to_cart20'])){
$_SESSION['prod_id'] = 62;
header("location: ../computer.php");
}
if(isset($_GET['add_to_cart21'])){
$_SESSION['prod_id'] = 64;
header("location: ../computer.php");
}
$gadgets = $_SESSION['prod_id'];
mysql_close($connection);
?>
<!-- This is my cart.php code -->
<?php
include('php/userloginprocess.php'); // Includes Login Script
include('php/addtocartprocess.php');
/*$id = $_SESSION['prod_id'];*/
$gadgets = $_SESSION['prod_id'];
if($_SESSION['login'] == FALSE){
header("location: login.php");
}
if($_SESSION['login'] == TRUE)
?>
<?php
//connection
mysql_connect("localhost","root", "") or die(mysql_error());
//database connection
mysql_select_db("marketech_db") or die(mysql_error());
//sql query
$sql = "SELECT * FROM user_tbl";
$records=mysql_query($sql);
?>
<html>
<head>
<title> Marketech | Buy Genuine Gadgets Online </title>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.js"></script>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="shortcut icon" href="images/marketechlogo.ico" />
</head>
<body>
<!-- First Navigation Bar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Marketech</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><span class = "glyphicon glyphicon-shopping-cart"></span> Cart</li>
<li class="dropdown">
<?php echo $_SESSION ['fname']; ?><span class="caret"></span>
<ul class="dropdown-menu">
<li>Account Settings</li>
<li>Logout</li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<!-- Second Navigation Bar -->
<br><br><br>
<div class = "container-fluid">
<nav class="navbar navbar-inverse navbar-lower">
<div class="container-fluid">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
<ul class="nav navbar-nav col-lg-12">
<li class="col-md-2">Android Phone</li>
<li class="col-md-2">iPhone</li>
<li class="col-md-2">Windows Phone</li>
<li class="col-md-2">Smart Watch</li>
<li class="col-md-2">Computers</li>
<li class="dropdown col-md-2">Popular Brands <span class="caret"></span>
<ul class="dropdown-menu col-xs-2">
<li>Samsung</li>
<li>Apple</li>
<li>Microsoft</li>
<li>Sony</li>
<li>Asus</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
<!-- Marketechs Third Navigation (with glyphicons) -->
<div class="container">
<ul class="list-unstyled">
<li class = "col-md-3"><span class="glyphicon glyphicon-transfer"></span> 30 days Free Returns</li>
<li class = "col-md-3"><span class="glyphicon glyphicon-plane"></span> Free Delivery Above 999 php</li>
<li class = "col-md-3"><span class="glyphicon glyphicon-usd"></span> Cash on Delivery</li>
<li class = "col-md-3"><span class="glyphicon glyphicon-earphone"></span> Call (049) 557 2681</li>
</ul>
</div>
</div>
<!-- Cart -->
<div class = "container-fluid">
<form method = "GET" action = "deletecartproduct.php">
<div class = "col-md-9">
<table class = "table table-responsive table-hover">
<tr>
<th>Delete</th>
<th>Item Name</th>
<th>Item Price</th>
<th>Image</th>
</tr>
<?php
//connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("marketech_db") or die (mysql_error());
$sql = "SELECT * FROM product_tbl WHERE prod_id='$gadgets'";
$prod_records = mysql_query($sql);
while($products=mysql_fetch_assoc($prod_records)){
echo "<tr>";
echo "<td>" .'<input type="checkbox" name="products[]" value='.$products['prod_id'].'>'."</td>";
echo "<td>".$products['item_name']."</td>";
echo "<td>".$products['item_price']."</td>";
echo "<td>".$products['item_image']."</td>";
echo "</tr>";
}
?>
</table>
<input type ="submit" class = "btn btn-danger" name = "delete_cart_item" value = "Delete"></form>
</div>
</div>
<br><br><br><br><br>
<!-- Footer Navigation -->
<nav class="navbar navbar-default">
<div class="container">
<ul class ="list-unstyled">
<li class = "col-md-2"><h5><b>Call Us: (049) 557 2681</b></h5></li>
<li class = "col-md-2"><h5><b><font color = "black">About Us </b></h5></font></li>
<li class = "col-md-2"><h5><b><font color = "black">Contact Us</b></h5></font></li>
<li class = "col-md-2"><h5><b><font color = "black">Privacy</b></h5></font></li>
<li class = "col-md-2"><h5><b><font color = "black">Terms of Service</b></h5></font></li>
<li class = "col-md-2"><h5><b>Copyright (c) 2016<h5></li>
</ul>
</div>
</nav>
<!--
Members:
Marlon Mendoza
-->
</body>
</html>
You are using primitive session variable: $_SESSION['prod_id'], instead it should be an array.
Initialise it like this:
$_SESSION['prod_ids'] = [];
Then when a product is added/removed to cart, update $_SESSION['prod_ids']
array_push($_SESSION['prod_ids'],/*product_id*/)
You should consider doing the following:
Add a hidden field to your HTML containing the product ID if the current product.
<p>Price: 9,199 Php<br>
<input class="btn btn-success" type="submit" name="action" value="add_to_cart">
<input type="hidden" name="product_id" value="1">
<span class="glyphicon glyphicon-shopping-cart"></span>
</p>
Then you can refactor your PHP to this
<?php
if (session_status() == PHP_SESSION_NONE) {
session_start();
$_SESSION['cart'] = array();
}
$connection = mysql_connect("localhost", "root", "");
if(isset($_GET['add_to_cart'])){
array_push($_SESSION['cart'], $_GET['product_id'];
header("location: ../" . $_GET['product_id'] . ".php");
}
mysql_close($connection);
Notice how you can initialize $_SESSION['cart'] as an array, when creating the session object. Instead of assigning the product ID to $_SESSION['cart'], you can push it into the array.
Refactor the name of the php file which you set in your header, or add an additional hidden field containing the name of the file.
If you have 1000 items to add to cart you can not write 1000 if else statement.Better use loops.I created a sample code for what you want take reference from that and use your own logic to create what you want.happy coding.
check https://jsfiddle.net/qun3ajL9/
I am trying to implement infinity scroll but I am having issues. profile_page.php by default, should display 10 posts, then when the user reaches the bottom of the page, it should display another 10 rows from the database.
The problem is that I know my JQuery is working because the alert() is occuring when I reach the bottom of the page, but new data from the data is not being printed.
Here is my script (which can be found in **profile_page.php**):
<script>
$(document).ready(function(){
var load = 0;
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height()){
load++;
// start AJAX
$.post("inc/ajax.php", {load:load},function (data){
$(".userposts_panel").append(data); // images class
alert ("bottom");
});
}
});
});
</script>
And here is **ajax.php**
$load = htmlentities(strip_tags($_POST['load']))*10;
$query = mysqli_query ($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' ORDER BY id DESC LIMIT ".$load.",10");
while ($row = mysqli_fetch_assoc($query)) {
$thought_id = $row['id'];
$message_content = $row['message'];
$date_of_msg = $row['post_details'];
$thoughts_by = $row['added_by'];
$attachent = $row['attachment'];
echo "<div class='message_wrapper'>
<div class='where_msg_displayed'>
<div class='more_options' style='float: right;'>
<li class='dropdown'>
<a 'href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'> More <span
class='caret'></span></a>
<ul class='dropdown-menu'>
<li><a href>Flag Post
<span id='options' class='glyphicon glyphicon glyphicon-flag' aria-hidden='true'></span> </a></li>";
if ($user == $username){
echo "<li>"; ?> <a href="del_post.php?id=<?php echo $thought_id;?>">Delete <?php
echo "<span id='remove' class='glyphicon glyphicon-remove' aria-hidden='true'></span> </a></li>";
} echo"
</ul>
</li>
</div>
<img src='$profile_pic' height= '68px' style='border: 1px solid #F0F0F0;'/>
<span style='margin-left: 10px;'>$message_content </span> <br/> <br/>";
if ($attachent !=""){
echo "<img src='user_data/attached_images/$attachent' style='width: 230px; height=230px;'/>";
} echo "
</div>
<div class='where_details_displayed'>
<a href='profile_page/$thoughts_by'> <b> $name_of_user </b> </a> - $date_of_msg
<div class='mini_nav' style='float: right;'>
<span class='glyphicon glyphicon-heart-empty' aria-hidden='true' style='padding-right: 5px;' onclick='changeIcon()' ></span> |
<a onclick='return toggle($thought_id);' style='padding-left: 5px;'> View comments ($num_of_comments) </a>
</div>
<div id='toggleComment$thought_id' class='new_comment' style='display:none;'>
<br/> $comment_posted_by said: $comment_body
</div>
</div>
</div>";
}
The echo consists of the divs in which each post is displayed. But when I reach the end of the page, no new content loads. For example, if there are 12 posts by user Alice, 10 will be displayed by default, then when I scroll down, the other two should load, but they dont.