Jump to content

Getting More Numbers Into A Variable


thara

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

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 ----//

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

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.