justlukeyou Posted February 22, 2011 Share Posted February 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/ Share on other sites More sharing options...
KevinM1 Posted February 22, 2011 Share Posted February 22, 2011 $_GET['price'] will be equal to the string '1-300'. Not only that, you use that string value twice in your query. You need to get a hold of the numbers themselves. explode should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178352 Share on other sites More sharing options...
justlukeyou Posted February 22, 2011 Author Share Posted February 22, 2011 Isn't there a function to display everything between two references. For example display everything between Andrew ($name) and John ($name) Cant I use the between function? Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178358 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2011 Share Posted February 22, 2011 If you were to echo the query string right now, you'd see it reads WHERE price BETWEEN '1-300' and '1-300'. Can you see as where that may be an issue? Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178382 Share on other sites More sharing options...
justlukeyou Posted February 22, 2011 Author Share Posted February 22, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178384 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 No, each needs to be a distinct value. Either explode the 1-300 string on the hyphen, or have two form fields, one for lower limit and one for upper limit. WHERE price BETWEEN $lower_limit AND $upper_limit Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178387 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178389 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 Hi, I am also looking to do different range. For example 301-600 etc so is it best to use explode()? Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178391 Share on other sites More sharing options...
.josh Posted February 23, 2011 Share Posted February 23, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178395 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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>£ $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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178400 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 What about it 'didn't work'? Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178402 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178407 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178410 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178412 Share on other sites More sharing options...
.josh Posted February 23, 2011 Share Posted February 23, 2011 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']); Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178413 Share on other sites More sharing options...
jcbones Posted February 23, 2011 Share Posted February 23, 2011 You should try copying CV's code a little better. This: $price = explode($_GET['price']); Should be: $price = explode('-',$_GET['price']); Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178414 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178419 Share on other sites More sharing options...
jcbones Posted February 23, 2011 Share Posted February 23, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178424 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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']; Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178804 Share on other sites More sharing options...
Pikachu2000 Posted February 23, 2011 Share Posted February 23, 2011 If you're getting a blank screen, you have a fatal parse error and error_reporting isn't properly enabled in your php.ini file, possibly due to a typo or syntax error, OR there's nothing to display. Probably the first option, though. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178808 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178814 Share on other sites More sharing options...
PaulRyan Posted February 23, 2011 Share Posted February 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178819 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 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 *** Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178844 Share on other sites More sharing options...
PaulRyan Posted February 23, 2011 Share Posted February 23, 2011 It was most likely working, but you wern't echong out any data after the query had run, as you said you had no errors. But I'll never know, as you are the beholder of the full source code Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178847 Share on other sites More sharing options...
justlukeyou Posted February 23, 2011 Author Share Posted February 23, 2011 Well I merged the two together straight away. I just swapped my previous echo with the you had and it worked straight away so at least I know the echo works. Quote Link to comment https://forums.phpfreaks.com/topic/228532-between-function/#findComment-1178848 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.