Sort a PHP generated form by price - php

I'm trying to sort a PHP generated form by price. I get an error message 'Notice: Undefined index: PriceDesc/Asc'. I understand the error message but can't find any relevant info through searching so thought I'd ask. I'm new to PHP and so far this is the code I have:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--[if IE]>
<link href="blueprint/ie.css" type="text/css" rel="stylesheet">
<![endif]-->
<link href="blueprint/screen.css" type="text/css" rel="stylesheet">
<link href="style.css" type="text/css" rel="stylesheet">
<title>Enygma Peripherals</title>
</head>
<body>
<div id="wrapper" class="container">
<div id="top" class="span-24">
<div id="logo" class="span-5">
<img src="images/logo.png" alt="Enygma">
</div>
<div id="nav" class="span-11 last">
<ul>
<li>HOME</li>
<li>PRODUCTS</li>
<li>ABOUT</li>
<li>CONTACT</li>
</ul>
</div>
</div>
<div id="mainContent" class="span-24">
<h2>Products</h2>
<div id="left" class="span-8">
</div>
<div id="right" class="span-15 last">
<form method="get" name="sort">
<select name="sort" id="sort">
<option value="">--Select--</option>
<option value='PriceAsc'>Price: Highest First</option>
<option value='PriceDesc'>Price: Lowest First</option>
</select>
<input type="submit" value="Sort"/>
</form>
<?php
include("connection.php");
$data = mysql_query("SELECT * FROM products");
IF ($_GET['PriceAsc']){
$orderby=" ORDER BY price ASC";
}
IF ($_GET['PriceDesc']){
$orderby=" ORDER BY price DESC";
}
Print "<table border cellpadding=10>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Product Number:</th> <td>".$info['product_no'] . "</td> ";
Print "<th>Product:</th> <td>".$info['product_name'] . "</td> ";
Print "<th>Type:</th> <td>".$info['type'] . "</td> ";
Print "<th>Price:</th> <td>".$info['price'] . "</td> ";
Print "<th>Availability:</th> <td>".$info['availability'] . " </td></tr>";
}
Print "</table>";
?>
</div>
</div>
<div id="footer" class="span-24">
Acknowledgments
</div>
</div>
For the record, I'm using PhpMyAdmin for my backend d/b and the initial tabled data works fine.

Replace
$data = mysql_query("SELECT * FROM products");
IF ($_GET['PriceAsc']){
$orderby =" ORDER BY price ASC";
}
IF ($_GET['PriceDesc']){
$orderby =" ORDER BY price DESC";
}
with
if ( !empty($_GET['sort']) && $_GET['sort'] == 'PriceAsc' ) {
$orderby=" ORDER BY price ASC";
}
if ( !empty($_GET['sort']) && $_GET['sort'] == 'PriceDesc' ) {
$orderby=" ORDER BY price DESC";
}
$data = mysql_query("SELECT * FROM products".$orderby);
Note the changes, you need to use empty() to check if form submitted and check the value of it. And do the query once Order by appended to SQL.

Yes append the $sort to the $data as above
and you'll need those query strings to be coming in correctly with the url
http://www/mypage.php?PriceAsc=true
and you'll need to compare it correctly
if ($_GET['PriceAsc']=='true'){

You never attached the $orderby variable to the query. Try this:
$data1 ="SELECT * FROM products";
IF ($_GET['PriceAsc']){
$data1 .=" ORDER BY price ASC";
}
IF ($_GET['PriceDesc']){
$data1 .=" ORDER BY price DESC";
$data = mysql_query($data1);
The .= appends the string to the end of $data
And reason you getting the undefined index error is because theres no PriceAsc available! Youre trying to $_GET something that doesnt exist.
What you need to do is :
IF ($_GET['sort'] == 'Price: Highest First'){
$data1 .=" ORDER BY price ASC";
} else {
$data1 .=" ORDER BY price DESC";
}

Related

How to let the pagination keep the search parameters

Recently I am writing a products catalog for my company. I use the dropdown list to provide a filter to a grid views product list that has pagination. The filter is work fine if products not more than 1 page. When more then one page of search result, the browser will show all products same as no filter applied. Below is my code of the index.php. Please help because I already stuck in here for a week. Thank in advance.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php session_start();
if (!isset($_SESSION['user_name']))
{
echo "<script>window.open('../login.php','_self')</script>";
}
$user_name=isset($_SESSION['user_name'])?$_SESSION['user_name']:'';
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/gallery.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-3.2.1.min.js"></script>
<meta name="theme-color" content="#444" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php
include_once('../database/db_conection.php');//Include the database connection
?>
<div class="container">
<div class="col-md-2"> <br/>
<i class="fa fa-home" style="font-size:28px"></i>Home <br/>
<br/>
<br/>
<form action="buckle.php'" method="post" >
<div class="cf_header5">MATERIAL<br/>
(e.g.Brass, Alloy...)</div>
<select name="material" style="width:150px; height:25px;font-size:16px">
<option value="">Please select ....</option>
';
<?php
$sql_ml = "SELECT distinct material FROM products where material !='' order by material";
$records_ml=mysqli_query($dbcon,$sql_ml);
while ($row_ml=mysqli_fetch_array($records_ml)){
echo '<option value="'.$row_ml['material'].'">'.$row_ml['material'].'</option>';
}
?>
</select>
<br/>
<br/>
<div class="cf_header5">DESCRIPTION</div>
<select name="description" style="width:150px; height:25px;font-size:16px">
<option value="">Please select ....</option>
';
<?php
$sql_dn = "SELECT distinct catagory FROM products where catagory !='' order by catagory";
$records_dn=mysqli_query($dbcon,$sql_dn);
while ($row_dn=mysqli_fetch_array($records_dn)){
echo '<option value="'.$row_dn['catagory'].'">'.$row_dn['catagory'].'</option>';
}
?>
</select>
<br/>
<br/>
<div class="cf_header5">SIZE</div>
<select name="size" style="width:150px; height:25px;font-size:16px">
<option value="">Please select ....</option>
<?php
$sql_se = "SELECT distinct size FROM products where size !='' order by size";
$records_se=mysqli_query($dbcon,$sql_se);
while ($row_se=mysqli_fetch_array($records_se)){
echo '<option value="'.$row_se['size'].'">'.$row_se['size'].'</option>';
}
?>
</select>
<br/>
<br/>
<input type="submit" name="submit" value="Search" />
</form>
<?php //get the value from dropdown list and stored it to a variable
$material=isset($_POST['material']) ? $_POST['material'] : "";
$size=isset($_POST['size']) ? $_POST['size'] : "";
$description=isset($_POST['description']) ? $_POST['description'] : "";
?>
</div>
<div class="col-md-10">
<div id="main">
<header>
<div class="logo-outer">
<div class="logo"> <img src="../images/logo4.png" /> </div>
<?php
//////SET UP THE TOTAL images PER PAGE & CALCULATIONS:
$per_page = 12;// Number of images per page, change for a different number of images per page
// Get the page and offset value:
if (isset($_GET['page'])) {
$page = $_GET['page'] - 1;
$offset = $page * $per_page;
}
else {
$page = 0;
$offset = 0;
}
//Display no of items in cart
echo '<a href="viewcart.php?page=';echo $page+1;echo '&type=buckle';echo'">';
echo "You have";
$t_sql="select count(product_id) as nocart from cart where user_name='$user_name'";
$t_run=mysqli_query($dbcon,$t_sql);
$t_row=mysqli_fetch_array($t_run);
$cart_count=$t_row['nocart'];
echo '&nbsp';
echo '<span class="badge" >';echo $cart_count; echo '</span>';
echo ' items on list</a>';
//end of display number of items in cart
?>
</div>
</div>
</header>
<div class="margin">
<div id="pagination"><!-- #pagination start -->
<?php
$images_sql="select count(id) from products where product_type='buckle'";
if(isset($_POST['material'])and $material!='')
{$images_sql=$images_sql." and material='$material'";}
if(isset($_POST['description'])and $description!='')
{$images_sql=$images_sql." and catagory='$description'";}
if(isset($_POST['size'])and $size!='')
{$images_sql=$images_sql." and size='$size'";}
$images_sql=$images_sql." ORDER by id ASC";
$result_images = mysqli_query($dbcon, $images_sql);
$row_images = mysqli_fetch_array($result_images);
$total_images = $row_images[0];
// Calculate the number of pages:
if ($total_images > $per_page) {//If there is more than one page
$pages_total = ceil($total_images / $per_page);
$page_up = $page + 2;
$page_down = $page;
$display ='';//leave the display variable empty so it doesn't hide anything
}
else {//Else if there is only one page
$pages = 1;
$pages_total = 1;
$display = ' class="display-none"';//class to hide page count and buttons if only one page
}
////// DISPLAY THE PAGE COUNT AND BUTTONS:
echo '<h2'.$display.'>Page '; echo $page + 1 .' of '.$pages_total;//Page out of total pages
$i = 1;//Set the $i counting variable to 1
echo '<div id="pageNav"'.$display.'>';// $display variable will do nothing if more than one page
// Show the page buttons:
if ($page) {
echo '<button><<</button>';//Button for first page [<<]
echo '<button><</button>';//Button for previous page [<]
}
for ($i=1;$i<=$pages_total;$i++) {
if(($i==$page+1)) {
echo '<button class="active">'.$i.'</button>';//Button for active page, underlined using 'active' class
}
//In this next if statement, calculate how many buttonso show.
if(($i!=$page+1)&&($i<=$page+3)&&($i>=$page-1)) {//This is set for two below and two above the current page
echo '<button>'.$i.'</button>'; }
}
if (($page + 1) != $pages_total) {
echo '<button>></button>';//Button for next page [>]
echo '<button>>></button>';//Button for last page [>>]
}
echo '</div>';// #pageNav end
?>
</div>
<div id="gallery"><!-- Gallery start-->
<?php
// DISPLAY THE images:
//Define the SQL statement based on the dropdown list
if ($total_images>0)
{
$img_sql="select * from products where product_type='buckle'";
if(isset($_POST['material'])and $material!='')
{$img_sql=$img_sql." and material='$material'";}
if(isset($_POST['description'])and $description!='')
{$img_sql=$img_sql." and catagory='$description'";}
if(isset($_POST['size'])and $size!='')
{$img_sql=$img_sql." and size='$size'";}
$img_sql=$img_sql." ORDER by id ASC LIMIT $offset, $per_page";
$result = mysqli_query($dbcon, $img_sql);
while($row = mysqli_fetch_array($result)) {//Open the while array loop
//Define the image variable:
$image=$row['images'];
$product_id=$row['product_id'];
$product_type=$row['product_type'];
$size=$row['size'];
$material=$row['material'];
$description=$row['catagory'];
echo '<div class="img-container">';
echo '<div class="img">';
echo '<img src="images/'.$image.'">';
echo '</a>';
echo '</div>';
echo $product_id;echo '&nbsp &nbsp ';
echo 'Add to List';
echo '<br/>Size:';echo $size;
echo '&nbsp &nbsp Material: '; echo $material ;
echo '<br/>Description: ';echo $description;
echo '</div>';// .img-container end
}//Close the while array loop
}
else
{
echo'<br/><br>';
echo '<h1>Sorry! No result is matched, please search again.</h1>';
echo'<br/><br>';
}
?>
</div>
<!-- Gallery end-->
<div class="clearfix"></div>
</div>
</div>
<!-- #main end -->
</div>
</body>
</html>
Your logic seems OK but your problem is when you navigate trough pages you make a new call of the document, which doesn't include variables like filters but the current page ($_GET['page']).
Either you send filters as variables trough URL as well, which isn't the cleanest way to do table pagination.
Or you gather results in a javascript array, and then on click on a pagination button display the previous/next rows. By doing this you also avoid refreshing the whole document every time, which isn't necessary.

php - for loop takes only the first value from mysql result

In this code I'm trying to create as many modal boxes as the rows of my query result. This works! Then I want to fill these modal boxes with as many checkboxes as the number $row['seats_no'] from database. Here we have a problem..in every modal box it creates the same amount of checkboxes as the first one. What I mean is that the $row['seats_no'] has the number from the first row in every row.
<?php
session_start();
if(!isset($_SESSION['name']) && !isset($_SESSION['email']) && !isset($_SESSION['id']) && !isset($_SESSION['cash'])){
header("location: Start.php");
}
$name=trim($_SESSION['name']);
$email=trim($_SESSION['email']);
$dbc = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($dbc,"SET NAMES 'utf8'");
mysqli_query($dbc,"SET CHARACTER SET 'utf8'");
if (isset($_POST['buy']) && isset($_POST['consert_id'])){
$q = "SELECT ticket_id, section, cost, seats_no FROM tickets INNER JOIN concert ON tickets.consert_id=concert.concert_id WHERE concert.concert_id=".$_POST['consert_id'];
//ticket_id, concert_id, section, cost, seats_no, concert_id,concert_name, date, place, category, description
$r = mysqli_query($dbc,$q);
$num= mysqli_num_rows($r);
$q1 = "SELECT * FROM concert WHERE concert_id=".$_POST['consert_id'];
$r1 = mysqli_query($dbc,$q1);
$first = mysqli_fetch_assoc($r1)
?>
<!DOCTYPE html>
<html>
<title>Seats</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link type="text/css" rel="stylesheet" href="seats_style.css">
<body>
<div style="z-index:1;position:fixed;">
<ul>
<li>E-Viva</li>
<li>Ανανεωση υπολοιπου</li>
</ul>
</div>
<div class="background">
<div class="transbox">
<div class="w3-container">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/199011/concert.png">
<h1><?=$first['concert_name']?></h1>
<div style="left:0;"><?=$first['date']?></div>
<div style="right:0;"><?=$first['place']?></div>
<p><?=$first['description']?></p>
<?php
while ($row = mysqli_fetch_array($r)) {
?>
<div style="margin-left:5em;padding:5px;"><?=$row['section']?></div>
<div style="margin-left:5em;padding:5px;">Τιμή <?=$row['cost']?>€</div>
<div style="margin-left:5em;padding:5px;"><button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Κράτηση</button></div>
<div id="id01" class="w3-modal" background="black">
<div class="w3-modal-content">
<div class="w3-container">
<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-display-topright">×</span>
<p><h2>Διαλεξε τις θεσεις που θες</h2></p>
<form action="buy.php" method="post">
<table>
<?php
$num=0;
for($j=1; $j<=10; $j++){
echo "<tr>";
for($i=1; $i<= $row['seats_no']/10; $i++){
$num++;
echo "<td><input class='seat' type=\"checkbox\" name=\"s[]\" id=\"".$num."\" value=\"".$num."\"><label for=\"".$num."\">".$num."</label><input type='hidden' name='ticket_id' value='".$row['ticket_id']."'><input type='hidden' name='cost' value='".$row['cost']."'></td>";
}
echo "</tr>";
}
echo "</table><input class=\"ok\" type=\"submit\" name=\"ok\" value=\"Κράτηση\"></form></div></div></div><br>";
}
}
mysqli_free_result($r1);
mysqli_free_result($r);
mysqli_close($dbc);
?>
</div>
</div>
</div>
</body>
</html>
here are the results i get from executing my query at phpMyAdmin
Here are the buttons I create to open the modals
And here is the modal from the last button i clicked that in the database shows it should be 60 checkboxes but it shows 180
Κατερίνα,
Try to change the while ($row = mysqli_fetch_array($r)) {...} with a foreach loop,
like as foreach( mysqli_fetch_array($r) as $row ) {....}
Just a thought.
Also, you close one bracket too soon, remove one before the first mysqli_free_result($r1) and close it after the closing html tag </html> at the end of the file.
Also, as #tadman said, do some research and use parameterized queries. Your code it's very prone to MySQL Injections and you will face several problems. As far as i can see that page has to do with payments, or pre-payment.
Anyways, i hope i helped you.
Καλή επιτυχία ;)

php MYSQL query "ORDER BY" not working

Forewarning: I'm sure my code is clunky and inefficient, please feel free to call me out on it. I'm fairly new at this.
I'm attempting to order my query results by a column, indicated as a result of a select input. The select input exists on feed.php which features include: 'feedphp2.php.
The select input auto-submits onchange with action="feedSort.php". feedSort.php takes the submitted value and redirects to feed.php?sort=submittedValue. Then the included feedphp2.php within feed.php takes the $_GET['sort'] value and pulls a query sorted by that column.
//feed.php
<?php
session_start();
if(!isset($_SESSION['userID'])) {
header('Location: login.html');
}
?>
<html>
<head>
<title>Title</title>
<link href="style.css" type="text/css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css
family=Josefin+Sans:400,100,300,600,700' rel='stylesheet' type='text/css'>
</head>
<body style="background-color:rgba(0,212,242,0.1)">
<div class="headerSmall">
<div class="container">
<a href="index.php"><img class="logoImgSmall"
src="images/logosmall2.png"></a>
<div class="navFeed">
<?php include 'usernameGet.php';?>,Log Out
List a Meal
Dashboard
</div>
</div>
</div>
<div class="container">
<div class="sort">
<form action="feedSort.php" method="POST">
<select name="sortForm" onchange="this.form.submit()">
<option value="">Sort By...</option>
<option value="user">By User</option>
<option value="dateAdded">By Date Added</option>
</select>
</form>
</div>
<?php include 'feedphp2.php'?>
</div>
</body>
</html>
space
//feedphp2.php
<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);
if (isset($_GET['sort'])) {
$sortBy = $_GET['sort'];
echo $sortBy;
$resultQuery = "SELECT * FROM meals
ORDER BY '$sortBy'";
} else {
echo 'sortBy is not set.';
$resultQuery = "SELECT * FROM meals
ORDER BY user";
}
$result = mysqli_query($conn, $resultQuery);
//Write to divs
while ($rows = mysqli_fetch_assoc($result)) {
$imgSrc = $rows['image'];
echo '<a href="meal.php?'.'mealID=';
echo $rows['ID'];
echo '">';
echo '<div class="entry">';
echo '<div class="foodPic">';
echo '<img src="';
echo $imgSrc;
echo '">';
echo '</div>';
echo '<p class="entryName">'.$rows['mealName'].'</p>';
echo '<p class="entryServ">'.$rows['mealQuan'].'</p>';
echo '<p
class="entryPrice">$'.$rows['mealDollars'].'.'.$rows['mealCents'].'</p>';
echo '<img class="servingsIco" src="images/servings.png">';
echo '<p class="user">'.$rows['user'].'</p>';
echo '</div>';
echo '</a>';
}
?>
space
//feedSort.php:
<?php
session_start();
$sortBy = $_POST['sortForm'];
header ("Location: feed.php?sort=".$sortBy."");
?>
Columns within my table exist for both user and dateAdded and sort perfectly when explicitly provided within the query ie: SELECT * FROM meals
ORDER BY user. While provided through a PHP variable however:
Where $sortBy = user returns the exact same sort order as $sortBy = dateAdded.
I appreciate any help you might provide.
you using $_POST method to pass the value and using $_GET method to retrieve it . and change the $sortby without quotes. edit these lines in your feedphp2.php
<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);
if (isset($_POST['sort'])) {
$sortBy = $_POST['sort'];
echo $sortBy;
$resultQuery = "SELECT * FROM meals
ORDER BY $sortBy";
} else {
or use $_REQUEST[],Method which will retrieve both get and post method
<?php
include 'MysqlConnect.php';
mysqli_select_db($conn,$dbname);
if (isset($_REQUEST['sort'])) {//request method
$sortBy = $_REQUEST['sort'];//
echo $sortBy;
$resultQuery = "SELECT * FROM meals
ORDER BY $sortBy";
} else {

Php Variable Data Allocation

I wonder why the 2nd output $devNO gives me bool(false) which is empty. i have no idea about it. Am I wrong at the mysql_query() part?
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
var_dump($developer);
echo "<br>";
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
Here's my output:
Here I will show my full code for "games.php":
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<html>
<head lang="en">
<meta charset="utf-8" />
<title>Game List</title>
<link rel="stylesheet" href="css.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<style>
legend {font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif}
</style>
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Dandy's Game Library</h1>
</header>
<nav id="top_menu">
<ul>
<li><strong></stron>Home</strong></li>
<li><strong>Game List</strong></li>
</ul>
</nav>
<div id="game_wrapper">
<section id="filter">
<form action="games.php" method="post" name="search_form">
<fieldset>
<legend><h3><strong>Search</strong></h3></legend>
<strong>Developer</strong><br>
<select name="dev">
<option value="">--Select--</option>
<?php
include("dbConnection.php");
mysql_connect("localhost","root","");
mysql_select_db("games");
$sql = mysql_query("SELECT Publisher FROM publisher");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Publisher']."'>" .$row[Publisher]. "</option>";
}
?>
</select>
<br/><br/><strong>Game Platform</strong><br>
<select name="plat">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Platform FROM platform");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Platform']."'>" .$row[Platform]. "</option>";
}
?>
</select>
<br/><br/><strong>Genre</strong><br>
<select name="gen">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Genre FROM genre");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Genre']."'>" .$row[Genre]. "</option>";
}
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
?>
</select>
<br><br><input type="submit" name="search" value="Search"></input>
</fieldset>
</form>
</section>
<aside id="items">
<fieldset>
<legend><h3><strong>Game List</strong></h3></legend>
<?php
var_dump($developer);
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>
<div class="row">
<div class="cell"><?php echo $row[$game_title]?></div>
<div class="cell"><?php echo "Year : ".$row[$game_year]?></div>
<div class="cell"><?php echo "Language : ".$row[$game_lan]?></div>
<div class="cell"><?php echo "Price : RM".$row[$game_price]?></div>
</div>
<?php
}
?>
</div>
</fieldset>
</aside>
</div>
</div>
<div id="btm_wrapper">
<footer id="foot">
<strong></strong>
</footer>
</div>
</body>
</html>
The full output will be like this after i search from dropdown:
The method mysql_query() , if successful, returns a resultset, which has to be made an array even if only a single value is being returned from the query.
Ideally there should be a for-each iteration on this array to get result, however if you're sure you'll get at least one value, the first row with desired index('No' in your case).
$dataset= mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$row = mysql_fetch_assoc($dataset);
$devNO = $row['No'];
Also note the quote I've used for varchar. It was correctly pointed in the other answer. Try Now.
If $developer is a string use quotes. You need to first fetch results from result set and than use it in code:
<?php
$devNOResult = mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$platNOResult = mysql_query("SELECT No FROM platform WHERE Platform = '$platform'");
$genNOResult = mysql_query("SELECT No FROM genre WHERE Genre = '$genre'");
$devNO = mysql_fetch_row($devNOResult);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>

My php form will not submit through jQuery-Mobile

I have a fully working piece of PHP, HTML code but when I place it into my website that is styled using jQuery Mobile the submit button will not display the query. Does anybody know how to fix this or even why it is happening
SCRIPT
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>The School of Computing and Mathematics</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/project1.css" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile.structure-1.3.0.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
</head>
PHP
<body>
<div data-role="page">
<?php
// Create connection
$con=mysqli_connect('localhost', '', '', 'timetabledb');
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$course_dropdown ="";
$query_course = "SELECT * FROM course";
$result_course = mysqli_query($con,$query_course) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_course))
{
$course_dropdown .= "<option value='{$row['CourseName']}'>{$row['CourseName']}</option>";
}
$module_dropdown ="";
$query_module = "SELECT * FROM module";
$result_module = mysqli_query($con,$query_module) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_module))
{
$module_dropdown .= "<option value='{$row['ModuleName']}'>{$row['ModuleName']}</option>";
}
$day_dropdown ="";
$query_day = "SELECT * FROM days ";
$result_day = mysqli_query($con,$query_day) or die(mysqli_error());
while($row = mysqli_fetch_assoc($result_day))
{
$day_dropdown .= "<option value='{$row['Day']}'>{$row['Day']}</option>";
}
echo "<table border='1'>
<tr>
<th>Course Name</th>
<th>Module Name</th>
<th>Type of Class</th>
<th>Lecturer</th>
<th>Time</th>
<th>Day</th>
<th>Room</th>
</tr>";
if (isset($_POST['button']) && $_POST['button'] == 'Submit') {
$course = mysqli_real_escape_string($con, $_POST['Course']);
$module = mysqli_real_escape_string($con, $_POST['Module']);
$day = mysqli_real_escape_string($con, $_POST['Day']);
$query = "SELECT * FROM course_module WHERE CourseName = '$course' AND ModuleName = '$module' AND Day = '$day'";
$result = mysqli_query($con,$query);
while($row1 = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row1['CourseName'] . "</td>";
echo "<td>" . $row1['ModuleName'] . "</td>";
echo "<td>" . $row1['ClassType'] . "</td>";
echo "<td>" . $row1['Lecturer'] . "</td>";
echo "<td>" . $row1['Time'] . "</td>";
echo "<td>" . $row1['Day'] . "</td>";
echo "<td>" . $row1['Room'] . "</td>";
echo "</tr>";
}
}
?>
HTML
<h1>School of Computing and Mathematics</h1>
<h2>Mobile website<h2>
<h2>Current students</h2>
<div data-role="collapsible-set" data-theme="c" data-content-theme="d">
<div data-role="collapsible">
<h3>Section 1</h3>
<p>I'm the collapsible content for section 1</p>
</div>
<div data-role="collapsible">
<h3>Section 2</h3>
<p>I'm the collapsible content for section 2</p>
</div>
<div data-role="collapsible">
<h3>Timetabling</h3>
<p>Select your Course</p>
<form action="current.php" method="post" data-ajax="false">
<select name="Course">
<option>Select Course</option>
<?php echo $course_dropdown; ?>
</select>
<select name="Module">
<option>Select Module</option>
<?php echo $module_dropdown; ?>
</select>
<select name="Day">
<option>Select Day</option>
<?php echo $day_dropdown; ?>
</select>
<input id ="button_timetable" name="button" value="Submit" type="submit">
</form>
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Home</li>
<li>News</li>
<li class="ui-btn-active ui-state-persist">Current students</li>
<li>Prospective students</li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
</div>
</body>
</html>
Please keep in mind that this script is fully implemented on a plain HTML site. I am only having issues with the jQuery mobile library.
Thank You in advance
Let me give you an answer, I found everything going through your code.
Inside a index.php:
data-ajax="false" should be added as a form attribute, it prevents jQuery Mobile from using ajax to submit data.
Incorrect jQuery Mobile css was initialized in a HEAD
table tag was not closed
But, through and trough this is well built page.

Categories