English 中文(简体)
如何根据正在设置的检查箱进行查询?
原标题:How to append to a search query based on a checkbox being set?
  • 时间:2011-11-22 21:07:35
  •  标签:
  • php
  • sql

I am working on a site which displays products from a database in a listed format, so far so good. However, I would like to add a simple checkbox above which, when ticked, orders the list by price, lowest to highest (and also another one from highest to lowest). I have tried to do this but I m having problems.

First I added a checkbox (see HTML below) labelled "order by lowest price". Then I added a line to my php code just below the main database query (about 10 lines down my PHP code), which tries to append a piece of query language on the end of the database query, which is just above, however it doesn t seem to work.

我只想这样做,这样,当用户对检查箱进行检查时,检查箱的分类成本最低,最昂贵。 我做了哪些错误?

注:购买是指每行的价格数据存放在哪里。

<><><><>>><>>>>><>>>>>>

<form action="spain-holidays.php" method="post">
  <p>Order by lowest price:</p><input name="orderbyprice" type="checkbox" value="orderbyprice" />
</form>

PHP

<?php 

//connect
include ("db.connect.php");

//pagination
$per_page = 20;

$pages_query = mysql_query("SELECT COUNT( id ) FROM `holidaytable` WHERE brandname =  Spain ");
$count = mysql_result($pages_query, 0);
$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET[ page ])) ? (int)$_GET[ page ] : 1;
$start = ($page - 1) * $per_page;

echo  <p>There are  .$count.  holidays<hr></p><br /><br /> ;

//construct query and insert values

$query = mysql_query("SELECT * FROM holidaytable WHERE brandname =  Spain  LIMIT $start, $per_page") or die(mysql_error());


//THIS IS THE LINE I ADDED HERE:

if (isset($_POST[ orderbyprice ])) $query .= " ORDER BY buynow ASC";



while($rows = mysql_fetch_assoc($query)) {

    $name = $rows[ name ];
    $id2 = $rows[ id2 ];
    $hotelname = $rows[ hotelname ];
    $desc = $rows[ desc ];
    $spec = $rows[ spec ];
    $resort = $rows[ resort ];
    $awtrack = $rows[ awtrack ];
    $awthumb = $rows[ awthumb ];
    $awimage = $rows[ awimage ];
    $mthumb = $rows[ mThumb ];
    $mlink = $rows[ mlink ];
    $mimage = $rows[ mimage ];
    $buynow = $rows[ buynow ];
    $awcatid = $rows[ awcatid ];
    $mcat = $rows[ mcat ];
    $brandname = $rows[ brandname ];


echo  "<div class= resultbox >
<div class= resultboxtopbar >
    <div class= hotelname ><h3>$hotelname</h3></div>
    <div class= price ><h3>From <strong>£$buynow</strong> $spec</h3></div>
</div>
<div class= resultboxmain >
    <div class= resultboximage ><a href= $awtrack  target= _blank ><img src= $mimage  /></a></div>

        <h4>$resort, $brandname</h4>
        <p>$name</p>
        <p>$desc...</p>
        <div class= moreinfobutton ><input name= moreinfo  type= button  value= moreinfo  /></div>
</div>";

//more pagination, etc
最佳回答

mysql_query(>>> 不能回航,因此不能附在后面。 我们应该这样做:

$sql = "SELECT * FROM holidaytable WHERE brandname =  Spain  LIMIT $start, $per_page";

if (isset($_POST[ orderbyprice ])) $sql .= " ORDER BY buynow ASC";

$query = mysql_query($sql) or die(mysql_error());
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签