PHP - Searching A Number Range Using Between
I have read that i can search for a number within a mysql database using BETWEEN
i.e: SELECT * FROM `table_name` WHERE `field_name` BETWEEN 99 AND 200 How would i do this with the code below? <?php $salary = explode(":", $_GET['salary_to']); } $types[] = (isset($_GET['salary_to']) && strlen(trim($_GET['salary_to'])) > 0) ? "`salary_from` LIKE '%". mysql_real_escape_string(trim($salary[0])) ."%' AND `salary_to` LIKE '%". mysql_real_escape_string(trim($salary[1])) ."%'" : ''; ?> Thanks for any help Similar TutorialsI think I've coded myself into a corner. I have numerous items entered with measurement extremes - highest and lowest. Now I want somebody to be able to search for an item that could match a specific length. So I could have several items that are entered into a database like this: Code: [Select] item_id | length | high_low_param -----------+-----------+---------------------- 1234 | 12 cm | high -----------+-----------+--------------------- 1234 | 6 cm | low If somebody searches for an item that is 8 cm long, I want the results to be item 1234 and any other items that span that range. I'm not sure how to do this? array_fill ? I don't know. I don't want to slow down the database too much, and I don't want to have to put an entry for every single length each item could be. Any suggestions? i'm stuck with searching on age query i let users register their date of birth (they give in DAY, MONTH, YEAR) --> column 3 Name CalcAge Timestamp Timestamp calculated for query Person 1 21 659948401 642754801 Person 2 39 91954801 83746801 Person 3 15 849337201 848991601 Person 4 33 281257201 281170801 Person 5 23 596876401 600850801 Person 6 87 -1422809999 -1422723599 I came up with the following query to search between a minimum and maximum age (using AJAX): Code: [Select] //user enters a minimum and/ or max age - getting the querystring from URL (ajax) $minAge = $_GET['minAge']; $maxAge = $_GET['maxAge']; //age to unix timestamp function agetostamp($age){ $gebjaar = date(Y) - $age; $timestamp = mktime (0,0,1,0,0,$gebjaar); return $timestamp; } //making getvariables timestamp in order to compare with database if (isset ($minAge)){ $minAge = agetostamp($minAge); } if (isset ($maxAge)){ $maxAge = agetostamp($maxAge); } //building query query = "SELECT * FROM respondenten WHERE geslacht = '$sex'"; if(is_numeric($minAge) && $minAge > 0) $query .= " AND geboortedatum <= '$minAge'"; else if (is_numeric($minAge)) $query .= " AND geboortedatum >= '$minAge'"; if(is_numeric($maxAge) && $maxAge > 0) $query .= " AND geboortedatum <= '$maxAge'"; else if (is_numeric($minAge)) $query .= " AND geboortedatum >= '$maxAge'"; I encouter a lot of troubles working like this: 1) if i leave out the part of maxAge (so i only allow to set a minimum age in the search box) it works except for the fact that if i set the minimum age to 23, person 5 will not be included since 596876401 < 600850801 even though she is 23 and should be included. 2) if i add the part of also searching on a max age, i don't get any results I know that remark 1 probably has to do with the fact that im using date(Y) - $age and that people that are already born this year will have a timestamp higher than the one calculated in the query and visa versa. conclusion: i don't think i'm going to get out of this so i'm hoping sombody can help me, telling me how to build a query for selecting on age range Hi.. I need help in getting the 3 Months Name from my table field FromMonth and ToMonth. Here is the scenario. First, I select FromMonth and ToMonth. Ex. FromMonth = 5 ToMonth = 7 So it means FromMonth is May ToMonth is July. Now I save the MonthNumber to my database: table- so_month FromMonth = 5 ToMonth = 7 Now I need to get the between Months Name from FromMonth to ToMonth which are (May, June, July). How does it possible? Thank you so much. I'm getting the dreaded " Invalid parameter number: number of bound variables does not match number of tokens" error and I've looked at this for days. Here is what my table looks like:
| id | int(4) | NO | PRI | NULL | auto_increment | | user_id | int(4) | NO | | NULL | | | recipient | varchar(30) | NO | | NULL | | | subject | varchar(25) | YES | | NULL | | | cc_email | varchar(30) | YES | | NULL | | | reply | varchar(20) | YES | | NULL | | | location | varchar(50) | YES | | NULL | | | stationery | varchar(40) | YES | | NULL | | | ink_color | varchar(12) | YES | | NULL | | | fontchosen | varchar(30) | YES | | NULL | | | message | varchar(500) | NO | | NULL | | | attachment | varchar(40) | YES | | NULL | | | messageDate | datetime | YES | | NULL |Here are my params: $params = array( ':user_id' => $userid, ':recipient' => $this->message_vars['recipient'], ':subject' => $this->message_vars['subject'], ':cc_email' => $this->message_vars['cc_email'], ':reply' => $this->message_vars['reply'], ':location' => $this->message_vars['location'], ':stationery' => $this->message_vars['stationery'], ':ink_color' => $this->message_vars['ink_color'], ':fontchosen' => $this->message_vars['fontchosen'], ':message' => $messageInput, ':attachment' => $this->message_vars['attachment'], ':messageDate' => $date );Here is my sql: $sql = "INSERT INTO messages (user_id,recipient, subject, cc_email, reply, location,stationery, ink_color, fontchosen, message,attachment) VALUES( $userid, :recipient, :subject, :cc_email, :reply, :location, :stationery, :ink_color, :fontchosen, $messageInput, :attachment, $date);"; And lastly, here is how I am calling it: $dbh = parent::$dbh; $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); if (empty($dbh)) return false; $stmt = $dbh->prepare($sql); $stmt->execute($params) or die(print_r($stmt->errorInfo(), true)); if (!$stmt) { print_r($dbh->errorInfo()); }I know my userid is valid and and the date is set above (I've echo'd these out to make sure). Since the id is auto_increment, I do not put that in my sql (though I've tried that too), nor in my params (tried that too). What am I missing? I feel certain it is something small, but I have spent days checking commas, semi-colons and spelling. Can anyone see what I'm doing wrong? Hello,
I have problem durring binding update query. I can't find what is causing problem.
public function Update(Entry $e) { try { $query = "update entry set string = $e->string,delimiter=$e->delimiter where entryid= $e->id"; $stmt = $this->db->mysqli->prepare($query); $stmt->bind_param('ssi',$e->string,$e->delimiter,$e->id); $stmt->close(); } catch(Exception $ex) { print 'Error: ' .$ex->getMessage(); } }When I run function update I'm getting next error:Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement Can you help me to solve this problem ? Edited by danchi, 17 October 2014 - 10:25 AM. I need to display a number(the number is retrieved from the db) in the form input field such that only the last 4 digits is visbile, the remaining can be masked as * or X or whatever is applicable. I know the last 4 can be obtained as follows: Code: [Select] $number=substr($number,-4,4); But when i hit the submit button the form validates the input field and checks if the number is a valid number of a specific format. Therefore when I click on the submit button then I should still be able to unmask the masked numbers or do something similar that would help me validate the whole number. Code: [Select] <input type="text" name="no" value="<?php if(!empty($number)){ echo $number;} ?>"> Why is this not working? Code: [Select] $result = mysql_query("SELECT * FROM contacts WHERE type = 'consumer' AND sent BETWEEN '".date("Y-m-d")."' AND '".date("Y-m-d", strtotime("-7 days"))."'") or die(mysql_error()); $numrows = mysql_num_rows($result); if($numrows < 0 ){ echo "No records found"; } The record that is stored in the sent column is like this "2011-11-03 14:42:12", so there is a record in there, but nothing is showing up. Can anyone see why? thanks in advance This works if say I do 192.168.0.1 - 192.168.1.254 it will work. However doing 192.168.0.1 - 192.168.1.10 will only generate up to .10 on both IP schemes. I see why, but I cannot figure out a way around this... Thanks! $start = explode('.', '192.168.10.1'); $end = explode('.', '192.168.11.10'); /** * Final output... */ $a = 0; $b = 0; $c = 0; $d = 0; // First segment for ($a = $start[0]; $a < 1 + $end[0]; $a++) { // Second segment for ($b = $start[1]; $b < 1 + $end[1]; $b++) { // Third segment for ($c = $start[2]; $c < 1 + $end[2]; $c++) { // Fourth segment for ($d = $start[3]; $d < 1 + $end[3]; $d++) { echo "{$a}.{$b}.{$c}.{$d}<br />"; } } } } I have range of categories from 1 to 999 With this is piece of code: foreach (range(96,204) as $number) if (Tools::getValue('id_category') == $number) return; $this->filterResults(); return $this->display( __FILE__, 'block-center-filter.tpl'); iw managed to avoid display of 'block-center-filter.tpl' in that range but it shows in every other categories. What i need is opposite... i would like to dont show except in that range... how to do that? I am currently working on a date range script and wondering if anybody think of an easier way to do this, doing up to next 8 weeks? This one starts on a Sunday. I also need to do one starting on a Monday (will figure that one later) $thisweek = date("Y-m-d"); $week = date('w', strtotime($thisweek)); $date = new DateTime($thisweek); $firstWeek = $date->modify("-".$week." day")->format("M d"); $endWeek = $date->modify("+6 day")->format("M d"); echo $firstWeek." - "; echo $endWeek; $nextweek = date(("Y-m-d"), strtotime("+7 Days")); $week1 = date('w', strtotime($nextweek)); $date1 = new DateTime($nextweek); $firstWeek1 = $date1->modify("-".$week1." day")->format("M d"); $endWeek1 = $date1->modify("+6 day")->format("M d"); echo $firstWeek1." - "; echo $endWeek1;
Hey I have a string that looks like the following: Quote top-php-tutorials-2.html I have a script that cycles through each page. The 2 in the quote above is the page number. How can I extract the number between the - and the .html and replace it with another number? I've tried Code: [Select] substr($engine->selectedcaturl, 0,-6).$v.".html"But then I realised this only works for numbers that are 1 digit long Any input would be appreciated Hello, I'm very disapointed because I don't know how do it this. I'm explain. I have a database with one input date_arrival and second date_departure. Two dates are saved in SQL format : 2014-03-02. After my SQL request to select all dates in database I'd like to add dates between date_arrival and date_departure and for each line. For example I have date_arrival date_departure line 1 : 2014-02-01 2014-02-05 line 2 : 2014-02-10 2014-02-15 In fact at the end, I have to send the result like this (with JSON) : ["2014-02-01","2014-02-02","2014-02-03","2014-02-04","2014-02-05","2014-02-10","2014-02-11","2014-02-12","2014-02-13","2014-02-14","2014-02-15"] Can you give me some help to add intermediates values in an array ? Thx Ok, my subject line may not be exactly what I am looking for and I am not even sure what the subject should be, so let me explain what I am trying to do. My application is as such that you enter in a zip code and it returns information, however my hard copy has a range of zip codes, in this case, equaling a zone, I.E. 90076-90210 is zone A, now instead of having the following in the database: Code: [Select] ID ZIP ZONE 1 90076 a 2 90077 a 3 90078 a ... ... 134 90210 a Could I make it: Code: [Select] ID SZIP EZIP ZONE 1 90076 90210 a and though my PHP/mySQL callout compare the variable to, and find that is ZIP is between SZIP and EZIP to output a? As there are a couple od thousand zip codes I am trying to minimize my database to be a bit smaller, but am not sure how to code it. Normally I would make the database have all 99000 entries and pull from the database where ZIP = ZIP, but again I am trying to save my self some time... Could I make it somehow that if ZIP <= SZIP and >= EZIP output ZONE? Does this make any sense? My PHP Coding is unfortunately lacking, I have only been programming it heavily for the last 5 months and it has been basic stuff. Any help would be appreciated. i have the below code which i wrote this monring, for some reason when i assign $date to fetch data from my database i just get the echo of "You had an Error" yet if i manually assign $date as the following it works: Code: [Select] $date = strtotime("2011-09-20"); i have it set exactly the same in the database cus if i echo $date i get: Code: [Select] 2011-09-20 whats going on? date in php is a cruel function to play around with.. here is my code: Code: [Select] <?php // Query the DB to fetch all data where member_id matches that set in session $reminder = mysql_query("SELECT * FROM `reminders` WHERE member_id=$_SESSION[SESS_MEMBER_ID]") or die(mysql_error()); //While Loop the results while($fetch = mysql_fetch_array( $reminder )) { // Start date (TODAY!) $s_date = strtotime("now"); // End Date (+7 days from today) $e_date = strtotime("+1 week"); // Fetches Date stamp from "reminderDate" row in DB $date = $fetch['reminderDate']; // Checks if the date is between 2 ranges set above if($date > $s_date && $date < $e_date) //Echo The Results { echo "<div class='notification error png_bg'><a href='#' class='close'><img src='img/cross_grey_small.png' title='Close this notification' alt='close' /></a><div>Custom Error Notification BOX!</div></div>"; } else { echo "You had an Error"; //echo $date; // TEST that date outputs datestamp "2011-09-02" } //END LOOP } ?> Good Evening, I've created a function to get the Range of an array (Highest Value - Lowest Value). Now I'm trying to use this to find the Range of the Upper Quartile(Q3) and the Lower Quartile(Q1) but am not sure as to what is the best/most efficient way of doing this? Should I find the middle value of the array, and then create 2 new arrays, the top half and bottom half, then use the Range function on each of them to get Q3 and Q1 or is there a better way to go about doing this? $x = array(); sort($x); // calculate median (middle number/value) $count = count($x); $middleval = floor(($count-1)/2); if($count % 2) { $median = $x[$middleval]; } else { $low = $x[$middleval]; $high = $x[$middleval+1]; $median = (($low+$high)/2); } // calculate range $min = min($x); $max = max($x); return $range = $max - $min; any help/suggestions are appreciated! Thank you. Kind Regards, Ace Hi i have two arrays, activity and feed.. contains 100 arrays, $activity = array(0,1,2....,100); contains 5 arrays $feed = array(100,50,25,12,75); I have 7 ranges, (0 -15),(15-30),(30-45),...(90-105) now i need to compare feed array with activity array to find in which range it was... Example, $feed[0] = 100, then it is in (90-105) range $feed[3] = 12, then it is in (0-15) range.. ... how to find this range?? please help me.. I can not get the below to work: if(time() < strtotime("11/21/2011 3:30AM EST") && time() > strtotime("11/21/2011 2:30PM EST")) It should be evaluating to true between 3:30 AM and 2:30 PM EST for today. Now that it's past 2:30PM it should be evaluating to false, but it's coming up as true. Edit: Nevermind. Cache/Cookies Issue. Okay we are using a javascript, but the javascript that the site comes from has bans all irans ips... and it loads slow for those people, how would I place a if statement saying dont load this javascript if in Iran, thanks... I know how to do it, but I can't find a real set number, and I would use just 1 ip but the guy has dynamic... thanks ahead of time Hi i have the following code Quote
$sql= "SELECT * FROM income WHERE month(date) between '04' and '12' and year(date) between 2020 and 2020"; This obviously selects all the information from April 2020 to December 2020
How do i change this to add a date as well for example if i wanted to display all the information from April 6th 2020 to December 5th 2020 ?
I've written the code to generate letter and number ranges: public function generateRange($type = 'numbers', $start = 0, $end = 10) { $rangelist = array(); switch($type) { case 'upper letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } } case 'lower letters': { foreach(range($start, $end) as $letter) { $rangelist[] = $letter; } } case 'numbers': { foreach(range($start, $end) as $number) { $rangelist[] = $number; } } default: { return false; } return $rangelist; } } It current doesn't seem to return the array result correctly currently. If I change the line "$rangelist[] = $number;" to "echo $number;" it does print the correct data. Any ideas on why the array return line isn't working correctly? |