thara Posted November 2, 2012 Share Posted November 2, 2012 I am using a form to take a four digits length number from user..also users can enter more than one number by separating by commas in form's text field. My problem is if it is one number in text field its no problem to me and I can get it to php. but if there are more numbers I cant get those in to php. so anybody can help me to do this? thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/ Share on other sites More sharing options...
Beeeeney Posted November 2, 2012 Share Posted November 2, 2012 Your code here. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389583 Share on other sites More sharing options...
floridaflatlander Posted November 2, 2012 Share Posted November 2, 2012 I would think you could use the explode function using the comma as a seperator. I can't see people inputing info in a form right each and every time. I think you'll also need something to remove spaces and a way to deal with numbers that are more or less than four charactors. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389585 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 this is my form... <form class="search_code" method="post" action=""> <input type="radio" name="searchCode" class="searchRadio" value="1" <?php if (isset( $searchCode) && $searchCode == 'TutorCode' ) echo "checked"; ?> /> Tutor (LITI) <input type="radio" name="searchCode" class="searchRadio" value="2" <?php if (isset( $searchCode) && $searchCode == 'InstituteCode' ) echo "checked"; ?> /> Center (LICI) <input type="text" name="searchText" class="searchText" value="<?php if (isset( $searchText)) echo $searchText; ?>" /> <input type="submit" value="Go" class="searchGo" /> <input type="hidden" value="TRUE" name="code_search_submitted" /> <p>Enter Member Code <span style="color:red;">(s)</span> separated by comma</p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389587 Share on other sites More sharing options...
cyberRobot Posted November 2, 2012 Share Posted November 2, 2012 (edited) Do you have any code for processing the form? As floridaflatlander suggested, you should be able to break apart the member code(s) with explode(): http://php.net/manua...ion.explode.php The array returned by the explode() function can then be processed with a foreach loop. Within the loop, you'll want to remove any leading/trailing space with trim(): http://us3.php.net/m...nction.trim.php ...and check to make sure the member codes are numbers and are four digits or less. If any member code doesn't meet the criteria, send them back to the form and have the user fix the error(s). Edited November 2, 2012 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389596 Share on other sites More sharing options...
Christian F. Posted November 2, 2012 Share Posted November 2, 2012 Or, via a RegExp to avoid having to trim every element: $RegExp = '/(\\d+)[,\s]*/'; preg_match_all ($RegExp, $data, $matches); $numbers = $matches[1]; Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389602 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 this is the code for form processing that I have been trying so far.. //------------ Check code search bar form submission ---------------// if ( isset( $_POST['code_search_submitted'])) { if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) { $searchCode = $_POST['searchCode']; $searchText = $_POST['searchText']; if (is_numeric( $searchText ) && (strlen($searchText) == 4)) { if ( $searchCode == 1 ) { $_SESSION['searchCode'] = 'TutorCode'; } elseif ( $searchCode == 2 ) { $_SESSION['searchCode'] = 'InstituteCode'; } $_SESSION['searchText'] = $searchText; $url = BASE_URL . 'search/searching.php'; // Define the URL: ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } else { $searchingError= 'Enter four digits length number!'; echo '<script type="text/javascript">'; echo "alert('" . $searchingError . "')"; echo "</script>"; } } else { $searchingError= 'Enter Tutor or Institute Code!'; echo '<script type="text/javascript">'; echo "alert('" . $searchingError . "')"; echo "</script>"; } } //---- Check code search bar form submission ----// Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389612 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 I get a solution to this using 'explode()' something like this.. $numbers = explode(",", $searchText); foreach($numbers as $number) { $numbers = trim($number); } I think now I need to check and make sure user given member codes are numbers and are four digits or less and there are more member codes etc.. so far I tried like this.. [/font][/color] [color=#282828][font=helvetica, arial, sans-serif] if (is_numeric( $searchText ) && (strlen($searchText) == 4)) {[/font][/color] [color=#282828][font=helvetica, arial, sans-serif] } it is ok when one member code given... but I want to check with one or more member codes... any idea? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389628 Share on other sites More sharing options...
cyberRobot Posted November 2, 2012 Share Posted November 2, 2012 (edited) Note that is_numeric() will accept values like "1e4" and "9.1". My current preference for checking numbers is ctype_digit(): http://www.php.net/m...ctype-digit.php Does the number have to be four digits? Could "0322" be entered as "322"? Edited November 2, 2012 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389632 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 I modified the code for form processing something like this... if ( isset( $_POST['code_search_submitted'])) { if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) { $searchCode = $_POST['searchCode']; $searchText = $_POST['searchText']; [/font][/color] [color=#282828][font=helvetica, arial, sans-serif] echo $searchCode; echo '<br />'; echo $searchText; //if (is_numeric( $searchText ) && (strlen($searchText) == 4)) { $numbers = explode(",", $searchText); foreach($numbers as $number) { $numbers = trim($number); if (ctype_digit($numbers) && (strlen($numbers) == 4)) { echo '<br />' . $numbers ; } else { echo 'Can not cantain string and more than 4 digists number'; } } } } I tested it now and working.. but error message acting differently if user enter code in text field something like this... Eg: 1234,03443,23432,2344 Its output like this: 1234 Can not cantain string and more than 4 digists number Can not cantain string and more than 4 digists number 2344 can anybody tell me what I need to do here? Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389657 Share on other sites More sharing options...
cyberRobot Posted November 2, 2012 Share Posted November 2, 2012 I tested it now and working.. but error message acting differently if user enter code in text field something like this... Eg: 1234,03443,23432,2344 Its output like this: 1234 Can not cantain string and more than 4 digists number Can not cantain string and more than 4 digists number 2344 can anybody tell me what I need to do here? Isn't that what you want? "03443" and "23432" are both 5-digit numbers which don't meet your criteria. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389708 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 yes... just think a user enter some values like this '34534,4535,34243,eew3,3243 etc... I need to display one error message.. telling strings are not accepting and more than four digits numbers also not accepting.. so how to check this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389714 Share on other sites More sharing options...
Jessica Posted November 2, 2012 Share Posted November 2, 2012 Rather than echoing, turn that section of code into a function. When you get to an error, return the string. Then echo that one string. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389715 Share on other sites More sharing options...
cyberRobot Posted November 2, 2012 Share Posted November 2, 2012 If an error is found, is there any reason to continue processing the numbers? If not, you could end the foreach loop with break: http://php.net/manua...tures.break.php The break would go into the else portion with the error message. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389722 Share on other sites More sharing options...
thara Posted November 2, 2012 Author Share Posted November 2, 2012 ok thanks.. I echoed $numbers for test.. Now I modified it like this.. if (ctype_digit($numbers) && (strlen($numbers) == 4)) { $code[] = $numbers ; $_SESSION['code'] = $code; } else { echo 'Can not cantain string and more than 4 digists number'; } Now I need to attach this $code value to a query. in WHERE clause. this is my query..: SELECT tcs.tutor_id AS tid, t.qualification, GROUP_CONCAT( DISTINCT o.option_name SEPARATOR ', ') AS tutor_option, timg.image_name AS img, city_name AS city, d.district_name AS district FROM tutor_category_subject as tcs INNER JOIN subject AS s ON tcs.subject_id = s.subject_id INNER JOIN tutors AS t ON tcs.tutor_id = t.tutor_id INNER JOIN address ON address.address_id = t.address_id INNER JOIN city ON city.city_id = address.city_id INNER JOIN district AS d ON d.district_id = city.district_id LEFT JOIN tutor_images AS timg ON timg.tutor_id = tcs.tutor_id AND timg.image_type = 'profile' WHERE t.tutor_code = ------------ GROUP BY tcs.tutor_id; so please can you tell me how can I do this? Thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/270195-getting-more-numbers-into-a-variable/#findComment-1389729 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.