Jump to content

Between Function


justlukeyou

Recommended Posts

I am trying to search between two range of numbers. So it displays everything from 1 and 300.

 

phppricerange.php?price=1-300

 

I have this code so far.  I found it based on date example which shows everything between two dates.  However, I dont seem to be able to get it to work for my code.

 

<?php
if( isset($_GET['price'])) {
$price = $_GET['price'];
$query = "SELECT * FROM productfeed WHERE price between '$price' and '$price' LIMIT 0, 10";
$fetchdata = mysql_query($query) ;
while($row = mysql_fetch_array($fetchdata)) {
$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

 

Can anyone advise please.  The strange thing is it isn't coming up with any errors.  Even if I use:

 

price=1&300 or price=1-300

Link to comment
Share on other sites

I see, so its using the query twice.

 

I tried it with the following but this came back with the same response.  Nothing.

 

$query = "SELECT * FROM productfeed WHERE price between '$price' LIMIT 0, 10";

 

If I use the following and search for an individual price it does display an individual price but I can find the code to display a range.

 

$query = "SELECT * FROM productfeed WHERE price like '%$price%' LIMIT 0, 10";

Link to comment
Share on other sites

Thanks,

 

I tried using that.

 

Do I need to set values to $lower_limit AND $upper_limit ?

 

if( isset($_GET['price'])) {
$price = $_GET['price'];
$query = "SELECT * FROM productfeed WHERE price BETWEEN $lower_limit AND $upper_limit LIMIT 0, 10";
$fetchdata = mysql_query($query) ;
while($row = mysql_fetch_array($fetchdata)) {
$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

Link to comment
Share on other sites

geez man, no offense, but you need to make a better effort here... if you are seriously struggling with something as basic as this...I mean again, no offense, but maybe coding just isn't your thing?

 

you are passing your price range as a single "1-300" string.  It's like saying "please give me thisandthis" instead of "please give me this and this".

 

You need to split it up into individual variables.

 

for example:

$price = explode('-',$_GET['price']);
echo $price[0];
echo "<br/>";
echo $price[1];

 

Do you see how those are two separate things now? Now you can do this:

 

if( isset($_GET['price'])) {
$price = explode('-',$_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price[0]' and '$price[1]' LIMIT 0, 10";

 

Link to comment
Share on other sites

Hi,

 

I tried this but I couldn't get it to work.  Could it be because I am using "$price =" twice?

 

<?php

$price = explode($_GET['price']);
echo $price[0];
echo "<br/>";
echo $price[1];

if( isset($_GET['price'])) {
$price = explode($_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price[0]' and '$price[1]' LIMIT 0, 10";
$fetchdata = mysql_query($query) ;
while($row = mysql_fetch_array($fetchdata)) {
$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

echo "<div class='productdisplayshell'>
<div class='productdisplayoutline'>
<div class='productborder'><center>
<a href='$link' target='_blank'><img src='$image' width=\"95%\" /></a>
</center> </div></div>
<div class='productdescriptionoutline'>
<div class='productdescriptionbox'>
<a href='$link' target='_blank' >$description</a>
</div>
<div class='productfulldescriptionbox'>$fulldescription</div>
</div>
<div class='productpriceoutline'>
<div class='productpricebox'>
<center>&#163; $price</center>
</div>
<div class='productbuybutton'>
<center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center>
</div>
</div>
</div>";
} echo 
'Product is not available.  Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>';
}
?>

Link to comment
Share on other sites

I dont know.

 

So this code slits the query "php?price=1-300" is split into two.  1 splits into $price[0] and 300 splits into $price[1];

 

It isn't displaying anything, just a blank screen.

 

Is there anyway I can test it.  I know I have a value of 99 in the database I can use 'like' to display it.

Link to comment
Share on other sites

You really aren't doing yourself any favors by not having error reporting on while you're developing. Either enable it in your php.ini file with

error_reporting = -1
display_errors = On

 

or on a per script basis by pasting this code in at the top:

ini_set('display_errors', 1);
error_reporting(-1);

Link to comment
Share on other sites

Thanks Pikachu2000,

 

I do have the php.ini file set as you suggest but it doesn't return anything.  But now I have added the display errors to the code it shows a full error messages.

 

Big thanks.

 

It tells me I have "Wrong parameter count for explode()"

 

What Im puzzled about is what the line break does and why this is needed.

Link to comment
Share on other sites

In the latest code you posted...

 

a) you have the following added in there, which was intended as an example to illustrate a concept. You need to remove it:

$price = explode($_GET['price']);
echo $price[0];
echo "<br/>";
echo $price[1];

 

b) You somehow managed to lose the delimiter for the explode.  This line:

 

$price = explode($_GET['price']);

 

should be

 

$price = explode('-',$_GET['price']);

Link to comment
Share on other sites

Thanks,

 

I removed the code I shouldn't have added but it came back with a blank screen, I also removed the number tags and that also came back with a blank screen without any error messages.  Would that be because its not telling it echo properly?

 

ini_set('display_errors', 1);error_reporting(-1);

if( isset($_GET['price'])) {
$price = explode('-',$_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price' and '$price' LIMIT 0, 10";
$fetchdata = mysql_query($query) ;
while($row = mysql_fetch_array($fetchdata)) {
$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

 

Link to comment
Share on other sites

This:

$price = explode('-',$_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price' and '$price' LIMIT 0, 10";

 

Should be

$price = explode('-',$_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price[0]' and '$price[1]' LIMIT 0, 10";

Link to comment
Share on other sites

Hi,

 

I tried adding the [0] and [1] tags but it came up with a blank screen.  I have also tried a few other things such as renaming all the terms as I was concern I was using 'price' twice but that has not affect. I have also tried doing things capitals but that has had no affect.  Can anyone shed any more light please.

 

ini_set('display_errors', 1);error_reporting(-1);

if( isset($_GET['price'])) {
$price = explode('-',$_GET['price']);
$query = "SELECT * FROM productfeed WHERE price between '$price[0]' and '$price[1]' LIMIT 0, 10";
$fetchdata = mysql_query($query) ;
while($row = mysql_fetch_array($fetchdata)) {
$id = $row['id'];
$image = $row['awImage'];
$link = $row['link'];
$description = $row['description'];
$fulldescription = $row['fulldescription'];
$price = $row['price'];

Link to comment
Share on other sites

Ah I was making such a 'newbie' mistake.  I had old code in such as printing the description in title which I no longer have the description query on this page.

 

I also moved this file into another folder and displayed the full errors.

 

I have stripped all of the old code and I still have a blank screen.

Link to comment
Share on other sites

At times like this you wish you had a brick or something... this had turned from a simple situation to a bloody nightmare...surely it can't be that hard to copy code?

 

<?PHP

  ini_set('display_errors', 1);
  error_reporting(-1);

  if(isSet($_GET['price'])) {
    $price = explode('-',$_GET['price']);
    $low   = (int)$price[0];
    $high  = (int)$price[1];

    $myQuery = "SELECT * FROM productfeed WHERE price BETWEEN $low AND $high LIMIT 0,10");
    if($doQuery = mysql_query($myQuery)) {

      while($row = mysql_fetch_assoc($doQuery)) {
        echo '<pre>';
        print_r($row);
        echo '</pre><br>';
      }

    } else {
      echo 'This Query failed: '.$myQuery.' <br> MySQL Error: '.mysql_error();
    }
  } else {
    echo 'Price not set.';
  }
?>

 

Try the above code? That should work...if not then check to make sure data exists in the DB for the query you are doing, make sure you are actually connected to MySQL also.

 

Regards, PaulRyan.

Link to comment
Share on other sites

Brilliant many thanks, this works.

 

Was there anything in pirticularly wrong with what I was doing before?

 

I would like to try and merge the two together.  I shall give it a go!

 

***  I am learning PHP & MySQL.  I just whipped up a page in twenty minutes which would have taken a week bank in January.  So many thanks  ***

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.