After sending a search query page become blank - php

I am working on a little search engine in PHP & MySQL. Yesterday, I restyled (I did changes only at the HTML part, no php code was touched) the result page, after that I was unable to search for a term.
New code:
<?php
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
require_once("lucene.php");
?>
<?php
$row1 = [];
$row2 = [];
$search_word = false;
if(isset($_POST["submit"])) {
//edit this with your credentials
$con = mysqli_connect("localhost", "root", "pass", "db");
if(mysqli_connect_error()) echo "Connection Fail";
else {
$search_word = true;
$input = $_POST["s_input"];
// tokenize input
$tokens = tokenize($input);
//compute weight of every token
$token_weight = compute_weight($tokens, $con);
$sql1 = "SELECT *, match(description) against('". $input ."') as score FROM web where match(description) against('".$input."') order by score desc";
$sql2 = "SELECT *, match(description) against('". $input ."') as score FROM web where match(description) against('".$input."') order by score desc";
$result1 = mysqli_query($con, $sql1);
$result2 = mysqli_query($con, $sql2);
$sql3 = "SELECT * FROM web";
$numDocs = (mysqli_num_rows(mysqli_query($con, $sql3)));
$maxOverlap = sizeof($tokens);
$ctr2 = 0;
while($ctr2 != $maxOverlap){
//compute inverse_document_frequency of term
$sql3 = "SELECT *, match(description) against('".$tokens[$ctr2]."') FROM web where match(description) against('".$tokens[$ctr2]."')";
$docFreq = (mysqli_num_rows(mysqli_query($con, $sql3)));
$idf[$tokens[$ctr2]] = idf($numDocs, $docFreq);
$ctr2++;
}
$ctr = 0;
while($r1 = mysqli_fetch_array($result1)){
$row1[$ctr]['ID'] = $r1['ID'];
$row1[$ctr]['title'] = $r1['title'];
$row1[$ctr]['description'] = highlight_words($r1['description'], $tokens);
$row1[$ctr]['credit'] = $r1['credit'];
$row1[$ctr]['url'] = $r1['url'];
$document = strtolower($row1[$ctr]['description']);
$ctr2 = 0;
$summation = 0;
while ($ctr2 != sizeof($tokens)) {
//compute term_frequency(term in document)
$term_frequency = tf($tokens[$ctr2], $document);
//get idf of term
$inverse_document_frequency = $idf[$tokens[$ctr2]];
//compute norm
$norm = norm($document);
$ctr2++;
$summation += $term_frequency*$inverse_document_frequency*$inverse_document_frequency*$norm;
}
$queryNorm = queryNorm($idf, $tokens);
$coord = coord($tokens, $document, $token_weight);
$lucene_score = $summation * $coord * $queryNorm;
$row1[$ctr]['score'] = $lucene_score;
$ctr++;
}
$row1 = orderBy($row1);
/*
desc score = number of word ocurrences * weight + match_against weight
+ 2(if exact words occur)
*/
$ctr = 0;
while($r2 = mysqli_fetch_array($result2)){
$row2[$ctr]['ID'] = $r2['ID'];
$row2[$ctr]['title'] = $r2['title'];
$row2[$ctr]['description'] = highlight_words($r2['description'], $tokens);
$row2[$ctr]['credit'] = $r2['credit'];
$row2[$ctr]['url'] = $r2['url'];
$desc = strtolower($row2[$ctr]['description']);
$ctr2 = 0;
$total_weight = 0;
$flag = 0;
while ($ctr2 != sizeof($tokens)) {
$weight = substr_count($desc, $tokens[$ctr2]) * $token_weight[$tokens[$ctr2]];
if ($weight == 0)
$flag++;
$total_weight += $weight;
$ctr2++;
}
//occurence of exact word * weight * 2
$exact_word_weight = substr_count($desc, $input) * $total_weight * 5;
$total_weight += $flag;
$row2[$ctr]['score'] = $r2['score'] + $total_weight + $exact_word_weight;
$ctr++;
}
$row2 = orderBy($row2);
}
mysqli_close($con);
}
?>
<!DOCTYPE>
<html>
<head>
<TITLE>Search Engine</TITLE>
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/search.css">
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Solve Simplex JavaScript -->
<script src="js/validation.js"></script>
</head>
<body>
<div id="wrapper">
<nav>
<form name="search_form" id="search_form" validate method="post">
<div class="left">
<img src="images/logo.png" attr="google logo">
<input type="text" class="textbox" id="obj_function" name="s_input">
<button type="submit" id="search_button" name="submit">
</form>
</div>
<div class="right">
<img src="images/button.png">
Sign In
</div>
</nav>
<div class="nav2">
<ul>
<li class="active">Web</li>
<li>Images </li>
<li>Videos</li>
<li>More <span><img src="images/dropdown.png"></span></li>
<li>Search Tools</li>
</ul>
</div>
<div class="search_results">
<ol>
<li>
<?php
$ctr = 0;
while ($ctr != count($row2)){
echo "<h1><a href=''#'> ".$row2[$ctr]['title']."</a></h1>";
echo "<div class='grey_green_links'>";
echo "<p class='green'>".$row2[$ctr]['url']."<span><img src='images/search_result_link_dropdown.png'></span></p>";
echo "<tr class = 'active'><td>"."<b>Description: </b>".($row2[$ctr]['description']."</td><td></td></tr>");
echo "<tr><td><br></br></td><td></td></tr>";
$ctr++;
}
?>
</li>
</ol>
<div class="page_nav">
<img src="images/pagination_logo.png" alt="pagination page logo">
<ol>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li id="next">10</li>
<li>Next</li>
</ol>
</div>
<footer>
<div class="top">
<p>
<img src="images/your_location_dot.png">
<span class="light_grey">Mars, TX - From your Internet address -</span> Use precise lcoation - Learn more
</p>
</div>
<div class="bottom">
<ul>
<li>Help</li>
<li>Send feedback</li>
<li>Privacy</li>
<li>Terms</li>
</ul>
</div>
</footer>
</div>
</body>
</html>
I personally thinks that bug is somewhere in this section of code, but I can't figure out where it should be. Can someone look at it, and maybe lend me a hand ?
<form name="search_form" id="search_form" validate method="post">
<div class="left">
<img src="images/logo.png" attr="google logo">
<input type="text" class="textbox" id="obj_function" name="s_input">
<button type="submit" id="search_button" name="submit">
</form>

I rewrited it a little bit and it's working, thanks to everyone for help.

Related

Nothing fetching content with server

Hosting company updated servers or added new patchs can't get a straight answer.
I updated code looked online and stuck. Tried to have hosting check with server and company keeps coming back and saying its the code.
Don't know what else to do and help would be appreciated.
<?
include_once("mysql_nitrousgarage.inc");
$clean_url = substr($PHP_SELF,1);
$clean_url = substr($clean_url,0,-5);
$q = "SELECT * from NG_product where active='Y' and
clean_url='$clean_url'";
$results = mysql_query($q);
$row = mysql_fetch_array($results);
$product_id = $row["product_id"];
$product_name = $row["product_name"];
$sku = $row["sku"];
$description = $row["description"];
$meta_title = $row["meta_title"];
$meta_description = $row["meta_description"];
$magiczoomplus = "Y";
include_once("topnav.inc")
?>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<meta name="viewport" content="width=400, user-scalable=no">
<script type="text/javascript"
src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-
52f96c9761c7b0cb" async="async"></script>
<div class="box-border">
Home ›
<?
if ($saved_category_id || !$saved_brand_id)
{
if ($saved_category_id)
$q = "SELECT * from NG_category where
category_id='$saved_category_id'";
else
$q = "SELECT * from NG_product_category as pc, NG_category
as c where pc.product_id='$product_id' and
pc.category_id=c.category_id order by c.order_placement limit 1";
$results = mysql_nitrousgarage.inc($q);
$row = mysql_fetch_array($results);
$category_id = $row["category_id"];
$category = $row["category"];
$category_clean_url = $row["clean_url"];
echo "<a href='$category_clean_url'>$category Wheels</a>";
}
else
{
$q = "SELECT * from NG_brand where brand_id='$saved_brand_id'";
$results = mysql_query($q);
$row = mysql_fetch_array($results);
$brand_id = $row["brand_id"];
$brand = $row["brand"];
$brand_clean_url = $row["clean_url"];
echo "<a href='$brand_clean_url'>$brand Wheels</a>";
}
?>
› <?echo $product_name?>
<div class="page-numbers">
<a href="<?echo $category_clean_url?>">‹ Back to <?echo
$category?> Wheels</a>
</div>
</div>
<div class="space20"></div>
<?
$image_id_arr = array();
$q1 = "SELECT * from NG_finish as f, NG_product_finish as pf, NG_finish_image as fi where f.active='Y' and f.finish_id=pf.finish_id and pf.active='Y' and pf.product_id='$product_id' and pf.product_finish_id=fi.product_finish_id and fi.active='Y' order by f.order_placement,fi.order_placement";
$r1 = mysql_query($q1);
while ($row1 = mysql_fetch_array($r1))
{
$image_id = $row1["image_id"];
$product_finish_name = $row1["product_finish_name"];
if (!$main_image)
{
$main_image = "finish-$image_id.jpg";
$selected_finish = $product_finish_name;
}
$image_id_arr[] = $image_id;
$product_finish_name_arr[] = $product_finish_name;
}
?>
<div class="simpletable">
<div class="row">
<div class="simplecell details-cll1">
<div id="m-container">
<img class="maxwidth" id="m-img" src="productphotos/<?echo $main_image?>" width=488>
</div>
</div>
<div class="simplecell" style="width: 3%"></div>
<div class="simplecell details-cll2">
<!--<div class="details-share">
<img src="images/facebook.jpg">
<br class="nomobile">
<img src="images/twitter.jpg">
<br class="nomobile">
<img src="images/mail.jpg">
<br class="nomobile">
<img src="images/plus.jpg">
</div>-->
<span class="details-title"><?echo $product_name?></span>
<div class="style-number">Style: <?echo $sku?></div>
<div class="selected-finish">Selected Finish: <span id=finish_layer>
<?echo $selected_finish?></span>
</div>
<div class="space30"></div>
<br>
<div class="simpleinline">
<div class="like-wheel">Like this Wheel?</div>
<div class="more-info-via">GET MORE INFO VIA</div>
</div>
<div class="simpleinline">
<a class="like-buttons" href="wheel-inquiry.php?product_id=<?echo $product_id?>&contact=C">CALL/TEXT</a> <a class="like-buttons" href="wheel-inquiry.php?product_id=<?echo $product_id?>&contact=E">EMAIL</a>
</div>
<div class="space50 nomobile"></div>
<div class="space30"></div>
<?
while(list($k,$v) = each($image_id_arr))
{
$product_finish_name = $product_finish_name_arr[$k];
echo "<img class='maxwidth' src='productphotos/finish-$v.jpg' width=145>";
}
?>
</div>
</div>
</div>
<div class="space20"></div>
<div class="box-border">
<div class="simpletable">
<div class="row">
<div class="simplecell product-desc">
<?echo $description?>
</div>
<!--<div class="simplecell price-notshow">
<b>Why are prices not shown?</b>
<br><br>
Many of our customers frequently ask us why are the prices not show.
</div>-->
</div>
</div>
</div>
<div class="space20"></div>
<?
$q = "SELECT * FROM NG_gallery WHERE active='y' and product_id='$product_id' ORDER BY featured desc, orderplacement limit 3";
$results = mysql_query($q);
if (mysql_num_rows($results) > 0) { ?>
<b><?echo $product_name?></b> Vehicle Gallery
<div class="page-numbers">
CLICK IMAGE TO <b>ENLARGE | VIEW MORE</b>
</div>
<?
while ($row = mysql_fetch_array($results))
{
$gallery_id = $row["gallery_id"];
$gallery_title = $row["gallery_title"];
$gallery_pics .= "<a href='productphotos/gallery_$gallery_id-l.jpg' class='MagicZoomPlus' id='Zoomer$gallery_id' rel='zoom-position: inner; zoom-width:400px;zoom-height:500px;' title='$gallery_title'><img class='maxwidth' src='productphotos/gallery_$gallery_id-l.jpg' width=380 border=0></a>";
}
?>
<div class="details-img-container">
<nobr>
<?echo $gallery_pics?>
</nobr>
</div>
<div class="details-img-container2">
<?echo $gallery_pics?>
</div>
<?}?>
<? include_once("footer.inc") ?>
Before updates of server page was working fine.

PHP code not working correctly. Everything works except the qty of the first product in cart

Like written in the title my code works okay except for the quantity for the first product when there are multiple products, where you can remove by clicking update but cant change the quantity.
<?php
include("functions/functions.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<img src="images/logo.png" />
<div id="navbar">
<ul>
<li>
Home
</li>
<li>
Products
</li>
<li>
My Account
</li>
<li>
Sign Up
</li>
<li>
Shopping Card
</li>
<li>
Contact Us
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<i class="fas fa-shopping-cart"> | </i>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box-cart">
<form action="" method="post" enctype="multipart/form-data">
<table>
<thead>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
<th>Price</th>
</thead>
<tbody>
<?php
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$pro_qty = $row_pro_price['qty'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'") or die(mysqli_error($con));
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_title = $row_pro_price2['product_title'];
$product_image = $row_pro_price2['product_image'];
$single_price = $row_pro_price2['product_price'];
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
?>
<tr>
<td>
<h2><?php echo $pro_title ?></h2>
<img src="admin/product_images/<?php echo $product_image;?>">
</td>
<td>
<input type="text" name="qty[]" value = "<?php echo $pro_qty;?>">
</td>
<td>
<input type="checkbox" name="remove[]" value="<?php echo $pro_id ?>" />
</td>
<td>
<?php echo "$" . $single_price; ?>
</td>
</tr>
</tbody>
<?php }} ?>
</table>
<p>
<b> Total Value: </b> <?php echo "$" . $total;?>
</p>
<div id="check-buttons">
<input type="submit" name="update_cart" value="Update Cart" />
<input type="submit" name="continue" value="Continue Shopping" />
<input type="button" value="Checkout" />
</div>
</form>
<?php
//pressing update cart ->
if(isset($_POST['update_cart'])) {
//removing the products
if(isset($_POST['remove'])) {
foreach($_POST['remove'] as $remove_id) {
$delete_product = "DELETE FROM cart WHERE p_id = '$remove_id' AND ip_add = '$ip'";
$run_delete = mysqli_query($con,$delete_product);
if($run_delete) {
echo "<script>window.open('cart.php','_self')</script>";
}
}
}
//quantity of product
if(isset($_POST['qty'])) {
foreach($_POST['qty'] as $qty_id) {
$update_qty = "UPDATE cart SET qty = '$qty_id' WHERE p_id = '$pro_id' AND ip_add = '$ip'";
$run_qty = mysqli_query($con, $update_qty) or die(mysqli_error($con));
}
}
}
elseif(isset($_POST['continue'])) {
echo "<script>window.open('index.php','_self')</script>";
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by </p>
</div> <!-- END footer -->
</body>
</html>
This is my cart.php page where the code executes. The $pro_id variable is from GET method for adding to cart. Therefore the $pro_id = $_GET[add_cart] which is in index.php. I will also include the index.php file bellow so if needed, one can take a look.
Quick answer would mean a lot. Thanks
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.12/css/all.css" integrity="sha384-G0fIWCsCzJIMAVNQPfjH08cyYaUtMwjJwqiRKxxE/rx96Uroj1BtIQ6MLJuheaO9" crossorigin="anonymous">
<link type="text/css" rel="stylesheet" href="styles/style.css" />
<title>Prodavnica+</title>
</head>
<body>
<div id="header" class="cf">
<img src="images/logo.png" />
<div id="navbar">
<ul>
<li>
Home
</li>
<li>
Products
</li>
<li>
My Account
</li>
<li>
Sign Up
</li>
<li>
Shopping Card
</li>
<li>
Contact Us
</li>
</ul>
</div> <!-- END navbar -->
<div id="search">
<form method="get" action="results.php" enctype="multipart/form-data">
<input type="text" name="search_query" placeholder="Search Product" />
<input type="submit" name="search_button" value="Search" />
</form>
</div>
</div> <!-- END header -->
<?php cart(); ?>
<div id="shop-bar">
<p>
Total items: <?php totalItems() ?>
</p>
<p>
Total price: <?php totalPrice()?>
</p>
<i class="fas fa-shopping-cart"> | </i>
<span> Welcome Guest! </span>
</div> <!-- END shop-bar -->
<div id="container">
<div id="main">
<div id="product-box">
<?php
if(isset($_GET['cat'])) {
getCatPro();
}
elseif(isset($_GET['brand'])) {
getBrandPro();
}
else {
getPro();
}
?>
</div> <!-- END product box -->
</div> <!-- END main -->
<div id="side">
<div id="side-category">
<h2>Categories</h2>
<hr />
<table id="mw">
<tr>
<?php
getBrands();
?>
</tr>
</table>
<ul>
<?php
getCats();
?>
</ul>
</div><!-- END side-category-->
</div> <!-- END side -->
</div> <!--END container -->
<div id="footer">
<p>© 2018 by Djordje Stamenkovic</p>
</div> <!-- END footer -->
</body>
</html>
functions.php
<?php
//database connection
$con = mysqli_connect("localhost","root","","eprodavnica");
// function for getting ip address of the client
/*In this PHP function, first attempt is to get the direct IP address of client’s machine, if not available then try for forwarded for IP address using HTTP_X_FORWARDED_FOR. And if this is also not available, then finally get the IP address using REMOTE_ADDR.*/
function getIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//adding a single product to cart
function cart() {
if(isset($_GET['add_cart'])) {
global $con;
$ip = getIp();
$pro_id = $_GET['add_cart'];
$run_check = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip' AND p_id = '$pro_id'") or die(mysqli_error($con));
if(mysqli_num_rows($run_check)>0) {
echo "";
} else {
$run_pro = mysqli_query($con, "INSERT INTO cart (p_id, ip_add, qty) values ('$pro_id','$ip',1)") or die(mysqli_error($con));
echo "<script>window.open('index.php','_self')</script>";
}
}
}
//getting the total added items
function totalItems() {
if(isset($_GET['add_cart'])) {
global $con;
$ip = getIp();
$run_items = mysqli_query($con, "SELECT * FROM cart WHERE ip_add='$ip'");
$count_items = mysqli_num_rows($run_items);
} else {
global $con;
$ip = getIp();
$run_items = mysqli_query($con, "SELECT * FROM cart WHERE ip_add='$ip'") or die(mysqli_error($con));
$count_items = mysqli_num_rows($run_items);
}
echo $count_items;
}
//getting the total Price of items in the cart
function totalPrice() {
$total = 0;
global $con;
$ip = getIp();
$run_price = mysqli_query($con,"SELECT * FROM cart WHERE ip_add = '$ip'");
while($row_pro_price = mysqli_fetch_array($run_price)) {
$pro_id = $row_pro_price['p_id'];
$run_pro_price2 = mysqli_query($con,"SELECT * FROM products WHERE product_id = '$pro_id'");
while($row_pro_price2 = mysqli_fetch_array($run_pro_price2)) {
$pro_price = array($row_pro_price2['product_price']);
$pro_price_values = array_sum($pro_price);
$total += $pro_price_values;
}
}
echo "$" . $total;
}
//getting the categories
function getCats() {
global $con;
$run_cats = mysqli_query($con,"SELECT * FROM categories");
while($row_cats = mysqli_fetch_array($run_cats)) {
$cat_id = $row_cats['cat_id'];
$cat_title = $row_cats['cat_title'];
echo "<li>
<a href='index.php?cat=$cat_id'>$cat_title</a>
</li>";
}
}
//getting the brands
function getBrands() {
global $con;
$run_brands = mysqli_query($con,"SELECT * FROM brands");
while($row_brands = mysqli_fetch_array($run_brands)) {
$brand_id = $row_brands['brand_id'];
$brand_title = $row_brands['brand_title'];
echo "<td>
<a href='index.php?brand=$brand_id'>$brand_title</a>
</td>";
}
}
//getting products on main page
function getPro() {
if(!isset($_GET['cat'])){
if(!isset($_GET['brands'])) {
global $con;
$run_pro = mysqli_query($con,"SELECT * FROM products ORDER BY RAND() LIMIT 0,9");
while($row_pro = mysqli_fetch_array($run_pro)) {
$pro_id = $row_pro['product_id'];
$pro_cat = $row_pro['product_cat'];
$pro_brand = $row_pro['product_brand'];
$pro_title = $row_pro['product_title'];
$pro_price = $row_pro['product_price'];
$pro_image = $row_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
//getting the category products
function getCatPro() {
if(isset($_GET['cat'])){
$cat_id = $_GET['cat'];
global $con;
$run_cat_pro = mysqli_query($con,"SELECT * FROM products WHERE product_cat = '$cat_id'");
$count_cats = mysqli_num_rows($run_cat_pro);
if($count_cats == 0) {
echo "<div class='no-cat'>
<h1> We're sorry! There are currently no products with that category. :(</h1>
</div>";
} else {
while($row_cat_pro = mysqli_fetch_array($run_cat_pro)) {
$pro_id = $row_cat_pro['product_id'];
$pro_cat = $row_cat_pro['product_cat'];
$pro_brand = $row_cat_pro['product_brand'];
$pro_title = $row_cat_pro['product_title'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
//getting the brand products (in this case sex)
function getBrandPro() {
if(isset($_GET['brand'])){
$brand_id = $_GET['brand'];
global $con;
$run_brand_pro = mysqli_query($con,"SELECT * FROM products WHERE product_brand = '$brand_id'") or die(mysqli_error($con));
$count_brands = mysqli_num_rows($run_brand_pro);
if($count_brands == 0) {
echo "<div class='no-cat'>
<h1> We're sorry! There are currently no products for that sex. :(</h1>
</div>";
} else {
while($row_brand_pro = mysqli_fetch_array($run_brand_pro)) {
$pro_id = $row_brand_pro['product_id'];
$pro_cat = $row_brand_pro['product_cat'];
$pro_brand = $row_brand_pro['product_brand'];
$pro_title = $row_brand_pro['product_title'];
$pro_price = $row_brand_pro['product_price'];
$pro_image = $row_brand_pro['product_image'];
echo "
<div class='single-product cf'>
<h4><a href='#'>$pro_title</a></h4>
<a href='details.php?pro_id=$pro_id'><img src='admin/product_images/$pro_image' /></a>
<p>
Price: $ $pro_price
</p>
<a href='index.php?add_cart=$pro_id'><button>Add To Cart</button></a>
</div>
";
}
}
}
}
NOTE: It works fine when one product is in the cart.

php pagination has some error

I'm trying to materialize pagination. I want to fetch like
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
and i used while paragraph to show memo and ripple (ripple database has parent field that what number of memo and it's order num primary field) but I think it seems like typed wrong code what should i do? It's image that I want make
Expected result:
Error:
<?php
session_start();
// echo "<a href='memo.php?page=$i'> $i </a>";
$scale=5; // page per writing
// start recored number newest writing is up
// $page 값에 다른 시작 레코드 넘버값, 가장 높은 글번호(최신글 부터 밑으로)
$start = ($page - 1) * $scale; //0,5,10 ...
if(!isset($_REQUEST["page"]))
{
$page = 1; // initialize seeing page
}else{
$page = $_REQUEST["page"];
}
require_once '../lib/dbconn.php';
$pdo = db_connect();
if(isset($_SESSION["userid"])){
$userid = $_SESSION["userid"];
}else{
$userid = "";
}
// start recored number newest writing is up
try{
//$sql = "select * from phptest.memo order by num desc";
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
$stmh = $pdo->query($sql2);
} catch (PDOException $ex) {`enter code here`
print "오류: ".$ex->getMessage();
}
$total_record = $stmh->rowCount();
$number = $total_record - $start;
// 전체 페이지 수 계산..
if ($total_record % $scale == 0)
$total_page = floor($total_record/$scale);
else
$total_page = floor($total_record/$scale) + 1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<link href="../css/common.css" rel="stylesheet" type="text/css" media="all">
<link href="../css/memo.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div id="wrap">
<div id="header">
<?php include "../lib/top_login2.php"; ?>
</div> <!-- end of header -->
<div id="menu">
<?php include "../lib/top_menu2.php"; ?>
</div> <!-- end of menu -->
<div id="content">
<div id="col1">
<div id="left_menu">
<?php
include "../lib/left_menu.php";
?>
</div>
</div>
<div id="col2">
<div id="title">
<img src="../img/title_memo.gif">
</div>
<?php
// if(isset($_SESSION["userid"])){
?>
<div id="memo_row1">
<form name="memo_form" method="post" action="insert.php">
<?php if(isset($_SESSION['nick'])){ ?>
<div id="memo_writer"><span >▷ <?=$_SESSION['nick'] ?> </span></div>
<?php }?>
<div id="memo1"><textarea rows="6" cols="95" name="content"></textarea></div>
<div id="memo2"><input type="image" src="../img/memo_button.gif"></div>
</form>
</div> <!-- end of memo_row1 -->
<?php // }
while ($row = $stmh->fetch(PDO::FETCH_ASSOC))
{
// $stmh = $pdo->query($sql2);
//$row[$i] = $row;
// $row = $stmh2->fetch(PDO::FETCH_ASSOC);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
$memo_content = str_replace("\n", "<br>", $row['content']);
$memo_content = str_replace(" ", " ", $memo_content);
?>
<div id="memo_writer_title">
<ul>
<li id="writer_title1"><?= $memo_num ?></li> <!--article number -->
<li id="writer_title2"><?= $memo_nick ?></li>
<li id="writer_title3"><?= $memo_date ?></li>
<li id="writer_title4">
<?php
if($userid=="admin" || $userid==$memo_id)
echo "<a href='delete.php?num=$memo_num'>[삭제]</a>";
?>
</li>
</ul>
</div>
<div id="memo_content"><?= $memo_content ?>
</div>
<div id="ripple">
<div id="ripple1">덧글</div>
<div id="ripple2">
<?php
$sql3 = "select * from phptest.memo_ripple where parent=$memo_num";
$stmh3 = $pdo->query($sql3);
while ($row_ripple = $stmh3->fetch(PDO::FETCH_ASSOC))
{
$ripple_num = $row_ripple["num"];
$ripple_id = $row_ripple["id"];
$ripple_nick = $row_ripple["nick"];
$ripple_content = str_replace("\n", "<br>", $row_ripple["content"]);
$ripple_content = str_replace(" ", " ", $ripple_content);
$ripple_date = $row_ripple["regist_day"];
?>
<div id="ripple_title">
<ul>
<li><?= $ripple_nick ?> <?= $ripple_date ?></li>
<li id="mdi_del">
<?php
if($userid=="admin" || $userid==$ripple_id)
echo "<a href='delete_ripple.php?num=$ripple_num'>삭제</a>";
?>
</li>
</ul>
</div>
<div id="ripple_content"> <?= $ripple_content ?></div>
<?php
}
if(isset($_SESSION["userid"])){
?>
<form name="ripple_form" method="post" action="insert_ripple.php">
<input type="hidden" name="num" value="<?= $memo_num ?>">
<div id="ripple_insert">
<div id="ripple_textarea">
<textarea rows="3" cols="80" name="ripple_content"></textarea>
</div>
<div id="ripple_button"><input type="image" src="../img/memo_ripple_button.png"></div>
</div>
</form>
<?php } ?>
</div> <!-- end of ripple2 -->
<div class="clear"></div>
<div class="linespace_10"></div>
<?php
//$number--;
}
?>
<div id="page_num"> ◀ 이전
<?php
// page link num
// 게시판 목록 하단에 페이지 링크 번호 출력
for ($i=1; $i<=$total_page; $i++)
{
if ($page == $i) // ���� ������ ��ȣ ��ũ ����
{
echo "<b> $i </b>";
}
else
{
echo "<a href='memo.php?page=$i'> $i </a>";
}
}
?>
다음 ▶</div>
</div> <!-- end of ripple -->
</div> <!-- end of col2 -->
</div> <!-- end of content -->
</div> <!-- end of wrap -->
</body>
</html>
The SQL doesn't like doing maths in the limit clause. Do it first in PHP:
$sql2 = "select * from phptest.memo order by num desc limit $start," . ($start+$scale);

PHP Pagination removes search term and reverts back to displaying all results, fix?

After searching for a specific course the results are displayed correctly but once I change page it reverts back to displaying all of the available results. How do I keep the search term results?
Here's the code
<?php
$con = mysqli_connect("localhost", "root", "root", "courses");
// $total = mysqli_query($con, "SELECT count(title) as total")->fetch()['total']);
if(isset($_GET['searchword'])){
$searchword = $_GET['searchword'];
}
else {
$searchword = "";
}
$result = mysqli_query($con, "SELECT count(title) FROM course WHERE title LIKE '$searchword%'");
$row = mysqli_fetch_row($result);
// Total rows.
$rowstotal = $row[0];
//pages
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 100 ? (int)$_GET['per-page'] : 10;
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$pages = $rowstotal / $perPage;
$sql = "SELECT * FROM course WHERE title LIKE '%$searchword%' ORDER BY title ASC LIMIT {$start},{$perPage}";
$query = mysqli_query($con, $sql);
$resultsText = "$rowstotal results";
$resultsList = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$courseId = $row["id"];
$courseAward = $row["award"];
$courseName = $row["title"];
$courseDetails = $row["summary"];
$resultsList .= ''.$courseAward.' '.$courseName.' <br>'.$courseDetails.'<br/>';
}
mysqli_close($con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Courses</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<nav class="navbar navbar-default navbar-static-top navbar-inverse">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
<img alt="Napier" src="">
</a>
</div>
</div>
</nav>
<div class="container">
<!-- Search -->
<form method="GET">
<input type="text" class="form-control" name="searchword" placeholder="Search" value="<?php echo $searchword ?>" style="width:30%;"><br>
</form>
<!-- Results -->
<div class="results">
<p></p>
<p>Showing <?php echo $perPage; ?> of <?php echo $resultsText; ?></p>
<p id="content"><?php echo $resultsList; ?></p>
</div>
<div class="pagination">
<?php for($x = 1; $x <= $pages; $x++): ?>
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>
<?php endfor; ?>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
</body>
</html>
Your page links don't preserve the searchword parameter. Change this:
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>
to this:
<a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>&searchword=<?php echo $searchword; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>

Is there a way i can implement a "load more" code into my original code?

Well i'm trying to make a website as you can see, and i'm having difficulty with the "LOAD MORE" button, what i in-vision is that whenever someone clicks this button it would go through preferably a php code that tells the database to load in 5 more posts.
show_posts.php
<?php
$query = $con->query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT 5");
if( isset( $_POST['load'] ) ){
//code goes here
}
while($result = mysqli_fetch_assoc($query)){
// if video is empty then echo this line
if($result["video"] == ""){
$account_assoc = $result["account_assoc"];
$result2 = mysqli_fetch_assoc($con->query("SELECT * FROM `Accounts` WHERE username='$account_assoc' OR email='$account_assoc'"));
if($result2["username"] == ""){
$identifier = $result2["firstname"];
}else{
$identifier = $result2["username"];
}
if($result2['image'] == ""){
$image = "http://jnvbaghmara.nic.in/images/staff/Blank.png";
}else{
$image = 'data:image/jpeg;base64,'.base64_encode($result2['image']);
}
if($result['dislikes'] > 1){
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislikes";
}else{
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislike";
}
if($result['likes'] > 1){
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> likes";
}else{
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> like";
}
echo '<li>
<h4><img src="'.$image.'"/> <label>'.$identifier.'</label></h4>
<div class="all-content">
<label style="color: #777;"> '.$like.' </label><label style="color: #777;"> | '.$dislike.'</label><label style="color: #777;"> | 2 comment</label>
<p>'.$result["text"].'</p>
</div></li>';
}
// if video is not empty then echo this line
elseif($result["video"] != ""){
$account_assoc = $result["account_assoc"];
$result2 = mysqli_fetch_assoc($con->query("SELECT * FROM `Accounts` WHERE username='$account_assoc' OR email='$account_assoc'"));
if($result2["username"] == ""){
$identifier = $result2["firstname"];
}else{
$identifier = $result2["username"];
}
if($result2['image'] == ""){
$image = "http://jnvbaghmara.nic.in/images/staff/Blank.png";
}else{
$image = 'data:image/jpeg;base64,'.base64_encode($result2['image']);
}
if($result['dislikes'] > 1){
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislikes";
}else{
$dislike = "<label style='color: red;'>".format_num($result['dislikes'])."</label> Dislike";
}
if($result['likes'] > 1){
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> likes";
}else{
$like = "<label style='color: #0096f3;'>".format_num($result['likes'])."</label> like";
}
echo '<li>
<h4><img src="'.$image.'"/> <label>'.$identifier.'</label></h4>
<div class="all-content">
<iframe width="90%" height="90%" src="https://www.youtube.com/embed/'.$result["video"].'" frameborder="0" allowfullscreen></iframe>
<p>'.$result["text"].'</p>
<label style="color: #777;"> '.$like.' </label><label style="color: #777;"> | '.$dislike.'</label><label style="color: #777;"> | 2 comment</label>
</div>
</li>';
}
}
echo '<li>
<div class="all-content">
<form method="post" action="#">
<input type="submit" name="load" value="LOAD MORE..." class="load_more_button"/>
</form>
</div>
</li>';
?>
profile.php
<?php include('include/header.php'); ?>
<?php
$error = "";
if(isset($_POST['post'])){
$video = substr($_POST["video"], 17);
$text = $_POST['paragraph'];
$account_assoc = "";
if($_SESSION['username'] != ""){
$account_assoc = $_SESSION['username'];
}elseif($_SESSION['email'] != ""){
$account_assoc = $_SESSION['email'];
}
$con->query("INSERT INTO `posts` (`id`, `image`, `video`, `text`, `account_assoc`, `likes`, `dislikes`) VALUES ('', '$image', '$video', '$text', '$account_assoc', '0', '0')");
}
?>
<div class="content">
<div class="actual_content">
<div class="right-content">
<div style="padding: 10px;">
<div id="post">
<h3>Latest News</h3>
<ul class="inner-post">
<?php
$sql = $con->query("SELECT * FROM `news` ORDER BY `id` DESC LIMIT 5");
function myTruncate($string, $limit, $break=" ", $pad="..."){
if(strlen($string) <= $limit){
return $string;
}
$string = substr($string, 0, $limit);
if(false !== ($breakpoint = strrpos($string, $break))) {
$string = substr($string, 0, $breakpoint);
}
return $string . $pad;
}
while($result = mysqli_fetch_array($sql)){
$header = $result['header'];
$statement = $result['statement'];
$image = $result['image'];
$shortdesc = myTruncate($statement, 300);
echo '<li><h5><img src="#" /><label>'.$header.'</label></h5><p>'.$shortdesc.'</p></li>';
}
?>
</ul>
</div>
</div>
</div>
<div class="left-content">
<div class="post-holder" width="70%" style="padding: 10px;">
<form class="post" method="post" action="#">
<textarea name="paragraph" placeholder="Hey there! Share a game highlight with a photo or video... <?php echo $error; ?>" maxlength="250"></textarea>
<input type="submit" name="post" class="button_post" value="POST" />
<label class="video"><input type="text" name="video" placeholder="Use a YouTube link in order to post video..."/></label><br />
</form>
</div>
<div class="new-posts" style="padding: 10px;">
<ul>
<?php include('show_posts.php')?>
</ul>
</div>
</div>
</div>
</div>
<div class="footer">
</div>
</div>
</body>
</html>
Also if there is anyway to make my code simpler, then i'm open to suggestions.
*I want the code to be php because i understand php more than i do js, jquery etc.
$postsNum = $_GET['num'];
$query = $con->query("SELECT * FROM `posts` ORDER BY `id` DESC LIMIT $postsNum");
The LOAD MORE button with a link: YOURURL?num=n
n can change by your php code.

Categories