PHP - Check First 2 Digits
Hi,
I'd like to know what the most efficient code is for checking the first 2 digits of a string. eg a strings first 2 digits must equal '07' thanks, Similar TutorialsHow do I get the number as pairs? (i.e. 05, 20, 01, 70) $query = "SELECT statez, COUNT(statez)FROM distributors GROUP BY statez"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['COUNT(statez)'] ." Distributors in ". $row['statez']; echo "<br />"; } ?> I need a way to find numbers 0000-9999 where no number has more than 2 digits in the same place in common. I know PHP but this is a little beyond me, can anyone help? Below is some PHP code that looks at a DB and outputs a value within the user declared time and date period, via user recall using a web app attached to it. The code is rounding the numbers but I want it to include 2 of the decimal places. The original numbers in the DB have up to 5 decimal digits. This code is making them whole numbers. i would like to include the first 2 digits. I suspect the float should somehow be float(2) . Not sure how to do that. thanks in advance <?php $sql = ";WITH TOTAL_FULL_ENERGY_SUM_BASE_WEEKDAY_SUMMER AS ( SELECT cdate, datepart(hh, cdate) as trans_hour, datepart(mi, cdate) as trans_minute, comm_id, max(cast(total_full_energy_b AS FLOAT)/1000) * meter_multiplier AS totalUsage, meter_multiplier FROM [radiogates].[dbo].[purge_data] LEFT OUTER JOIN [radiogates].[dbo].[ops_invoice] on [radiogates].[dbo].[purge_data].[comm_id] = [radiogates].[dbo].[ops_invoice].[meter_id] where comm_id='$comm_id' and meter_multiplier is not null group by comm_id, cdate, meter_multiplier ) SELECT top 1 *, datepart(weekday, cdate) as trans_date_day, datepart(month, cdate) as trans_date_month, datepart(hour, cdate) as trans_date_hour, DATEPART(minute, cdate) as trans_date_minute FROM TOTAL_FULL_ENERGY_SUM_BASE_WEEKDAY_SUMMER where datepart(weekday, cdate) IN ('2', '3', '4', '5', '6') AND DATEPART(MONTH, cdate) IN ('5','6','7','8','9','10') and cdate between '$base_date $base_start_time' and '$base_date $base_end_time' ORDER BY totalUsage desc"; $query = sqlsrv_query($conn, $sql);if ($query === false){ exit("<pre>".print_r(sqlsrv_errors(), true));}while ($row = sqlsrv_fetch_array($query)){ $sumUsageWKW += $row[totalUsage];}sqlsrv_free_stmt($query);?> Edited by Butterbean, 15 October 2014 - 01:31 PM. I have an integer that I'd like to validate to ensure that it is a valid number between 1 and 99999999999999999999(20 digits). I'm thinking about using the following, but I'm not sure what the max range is on the FILTER_VALIDATE_INT options. return filter_var($_REQUEST['r'], FILTER_VALIDATE_INT, array("options" => array("min_range"=>1, "max_range"=>99999999999999999999))); Can someone advise the edit required to include validation of US phone numbers ($phone_number3) when entered with no spaces between the area code and/or prefix. Note: I have working code (regular expression) that validates phone numbers that enclose the area code with parenthesis or having a space, hyphen, or dot between area code, prefix, or last four digits. Examples: all three shown below are validated based on regular expresion (123) 456-7890 123.456.7890 123 456 7890 However, I want to also include the option for 1234567890 (phone number have NO spaces) as valid. I know the edit will be within ( |-|.) using the PIPE but not sure how to denote the "no space" syntax within the regular expression. See the regular expression: $good_phone = "^\(?[0-9]{3}\)?( |-|.)[0-9]{3}(-|.)[0-9]{4}$"; and if you wan to view the entire php snippet // function which validates an American phone number function validate_phone($phone_number) { $good_phone = "^\(?[0-9]{3}\)?( |-|.)[0-9]{3}(-|.)[0-9]{4}$"; if (preg_match("/$good_phone/", $phone_number)) { echo "$phone_number is valid.<br/>"; } else echo "$phone_number is NOT valid.<br/>"; } $phone_number1 = "123.456.7890"; validate_phone($phone_number1); $phone_number2 = "(123) 456-7890"; validate_phone($phone_number2); $phone_number3 ="1234567890"; validate_phone($phone_number3); hi there - hello dear PHP-Friends, good evening! - i want to extract some data ouf of a large html-file. i have - a very very large amount of data: approx 5000 x the following line-sheme!: Quote 67003 Cato Bontjes Vice Versum House 1 28832 Achim 62042 Cato Bontjes Vice Versum House 2 28832 Achim 41798 Cato Bontjes Vice Versum House 3 37139 Adelebsen 40034 Cato Bontjes Vice Versum House 4 21365 Adendorf 46218 Cato Bontjes Vice Versum House 5 31855 Aerzen 42481Cato Bontjes Vice Versum House 6 21702 Ahlerstedt 49761 Cato Bontjes Vice Versum House 7 26197 Ahlhorn Question: how can i extract the first 5 first digits...!? I have allready some solutions here - i need a very very robuste solution diblertone1 Here's what I'm wanting to do: Number = 100 In a for loop take that number, 100, and break it down into 5 random numbers to equal 100. So the for loop would output something like this: 1. 32 2. 5 3. 18 4. 9 5. 36 Total = 100 Everytime the numbers would be different. I hope that explains it Thanks! ok so i can get this to work so i want to check if there is a video on inside my db if there is no video and nothing happens but if there is i want it to echo video Code: [Select] <?php if($video == ""){ }else{ echo "VIDEO"; } ?> I am unable to execute mysql_num_rows properly in this program. Code: [Select] <?php $link=mysql_connect("localhost","root",""); $db = mysql_select_db("myDB",$link); $result = mysql_query("SELECT * FROM jobs WHERE jobType=abc"); $num = mysql_num_rows($result); echo "Found $num records"; ?> error is mysql_num_rows() expects parameter 1 to be resource Suppose i have a table with around 10 entries in which one column contains the id and the second column contains a checkbox... If a checkbox a selected and the button (below the table) is pressed then i want to delete the columns corresponding the checked checkboxes.... I am not getting how sgould i pass the get variable on the link of the button??? -Pranshu Agrawal pranshu.a.11@gmail.com Hello, I have created a little voting script for my site. The insertion code into the MySQL databse when they vote is very simple: $ipaddress = addslashes($_POST['ipaddress']); $theid = addslashes($_POST['theid']); $gamert = addslashes($_POST['gamert']); $serveron = addslashes($_POST['serveron']); $db = mysql_connect("localhost", "username", "password"); mysql_select_db("thedb",$db); mysql_query ("INSERT INTO voting (theid,ipaddress,gamert,serveron2) VALUES ('$theid','$ipaddress','$gamert','$serveron') "); Obviously I only want them to be able to vote once. What is the best way to do a check of the IPs already in the database and check against theirs before submitting? Thanks. Hello their, I'm wanting to use PHP to check to see if a username and password match a remote RDP server. The reason I ask is because I want to verify that the information the user is giving is correct. I'm looking though LDAP but I have no idea what I'm looking for. To recap, I would like to login to a RDP server and return a result based on if it completed. Thank-you for reading Paul P.S Sorry for the bad English, I seemed to forgot how to spell some words and have had to replace them with longer sentences Hi guys, I am trying to get a admin panel, which when the user is logged in, it will check if there user access is(say for this post) 9... If there access is 9 in the database then direct to admin panel if not return them home. Thanks guys <?php // your email address $youremail = "twitchkuztomz@gmail.com"; // the subject of the email $msg = $_POST['notes']; $subject = $_POST['subject']; $email = $_POST['email']; $name = $_POST['name']; if(empty($youremail)) { die("No email address added. Please go back."); } elseif(empty($name)) { die("No name added. Please go back."); } elseif(empty($msg)) { die("No notes added. Please go back."); } else { if(mail($youremail, $subject, $msg, "From: $email\r\nReply-to: $email\r\n")) { echo "Email sent."; } else { echo "Mail() is not enabled."; } } $ch1 = $_POST['ch1']; $ch2 = $_POST['ch2']; $ch3 = $_POST['ch3']; $ch4 = $_POST['ch4']; $ch5 = $_POST['ch5']; ?> Thats the code i got so far i need it to mail the checked options with the messeges..... Thank you Hello, first of all i'm new here and i kinda like the community... so here i go with my question... When i register and then click on the link to get me to the log in file i have set the link for my member profile to be member.php?p=$_SESSION['id'] .. which works until now.. and lets say for instance my ID is 25... when i type on the browser member.php?p=553524 i will still be on the same page... so what i want to do is to check which id is typed and then throw them to the correct account profile or if the ID does not exist then throw them to a member-not-exist.php file.. I think i can do the how to check the id if it does exist but for the other one? Thanks in advance. does anyone have code for checking to make sure an email address is typed in correct Hi, I am trying to perform a login check and redirect the form to index.php if the username and password are correct. Also the login page check if the fields are entered or not. The issue is that the login check is performing but not redirecting to index.php. Also I'm not getting any error after performing the login check. Let me know if I'm making any errors in below code. the include file only contains the database details. Code: [Select] <?php include("connections.php"); ?> <?php if(isset($_POST['submit'])) { $errors=array(); $required_fields=array('username', 'pass'); foreach($required_fields as $fields) { if(!isset($_POST[$fields]) || empty($_POST[$fields])) { $errors[]=$fields; } } if(empty($errors)) { $username=trim($_POST['username']); $password =trim($_POST['pass']); $query= "SELECT * FROM users WHERE username='{$username}' AND hashed_password='{$password}' "; $result=mysql_query($query); $result_set=mysql_fetch_array($result); if(mysql_num_rows($result_set) == 1) { header('location: index.php'); exit; } else { echo 'Invalid password'; } } } ?> Hey, I'm trying to check if a username is unique, but for some reason none of the methods that I've looked up seem to work. I'm aiming for the code to check if a username is unique, and if get the user to input the name again. $get = "SELECT `i` FROM `m` WHERE `i`=\"$r\""; $checkResults = mysql_query($getRandomness); while(mysql_num_row($checkResults) > 0) { //some code... //check again $get = "SELECT `i` FROM `m` WHERE `i`=$r"; $checkResults = mysql_query($get); } The error comes from the while line, but it comes up during the first check because the record is empty. This is the error: "Fatal error: Call to undefined function mysql_num_row()" Hi, im new to php and got a simple query. i have built a web gallery and i want to add keywords to pictures, ive built the interface and mysql statements so it lists all possible keywords and creates a tick box for the user to use. ive created a table to join the image id's to my keyword id's as its going to be a many to many relationship, ive also built the sql statement below to select the highlighted image, but im not sure how i can join the looping of all keywords to generate the tick boxes and then to loop again to populate the tickboxes that are saved within the DB - if that makes sense?? SELECT * FROM images,img_key,keywords where img_key.img_id=images.id and keywords.id=img_key.key_id and images.id='2' Hello.. I have Two HTML form and one is a Check boxes form that enable users to select their category. Then I need to display second form according to the categories they selected in first form. I use this code in first form to validate form submission.. if ( isset( $_POST['category']) && sizeof( $_POST['category']) <= 3) { $category = $_POST['category']; } else { $errors[] = 'Please select atleast 1, not more than 3 categories'; } If errors array is empty I did this.. if ( empty( $errors )) { // If everything's OK $_SESSION = $category; $url = 'http://localhost/lanka_institute/tutorsignup/select_subjects.php? // Define the URL. ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } Can I know and is this correct? I display category list from mysql category table, it has category name and category id this is my html part from first page echo '<td width="50%"><input type="checkbox" name="category[]" vlaue="' . $info['category_id'] . '" /> ' . $info['category_name'] . '</td>'; any help appreciated. Thanks in advance.. |