manvaril Posted April 19, 2011 Share Posted April 19, 2011 I can't figure out how to pull the number from a form name Unfortunately I cant use substr(); because non of the names are the same length... these are the form names cat_sub_id_1 cat_name_1 cat_order_1 cat_sub_id_2 cat_name_2 cat_order_2 cat_sub_id_3 cat_name_3 cat_order_3 this is a list of the numbers that I need to be pulled out. Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/ Share on other sites More sharing options...
Pikachu2000 Posted April 19, 2011 Share Posted April 19, 2011 If it always ends with a single numeric digit: $number = substr($field_name, -1); Or if this is all coming from an HTML form, why not just set the form fields up as arrays? Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203573 Share on other sites More sharing options...
drisate Posted April 19, 2011 Share Posted April 19, 2011 You can also explose de _ character and check each chunk and keep the last numeric number <?php if (isset($number)){unset($number);} // Delete the var if it has been previously created $number_array = explode('_', $field_name); // Explode all the _ foreach($number_array as $value){ // Loop each values if (is_numeric($value)){ // Check if the value is numeric $number = $value; // Keep this value and overight if theres an other one in the string so we keep only the last one } } echo $number; ?> This way you will get the full number what ever the number of characters involved in your string as long as you use the same name field exemple cat_sub_id_1 will return 1 cat_sub_6_id_1 will return 1 cat_sub_id_1_pp will return 1 Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203576 Share on other sites More sharing options...
Zane Posted April 19, 2011 Share Posted April 19, 2011 you could perform a simple regex on the variable and get rid of eveything not INT $yourvariable = "something555somethingblah___!!###!@@3... and another 3"; $yourvariable = preg_replace("#([^a-z_-\.])#i", "$1", $yourvariable); Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203578 Share on other sites More sharing options...
drisate Posted April 19, 2011 Share Posted April 19, 2011 Yeah but if theres 2 numbers in the string he would end up with both together cat_sub_6_id_1 will return 61 instead of only 1 But if thats not gona happend go for that code hehe much smaller then the one i did Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203579 Share on other sites More sharing options...
manvaril Posted April 19, 2011 Author Share Posted April 19, 2011 ok let me clarify a little this form is dynamically created from a mysql database cat_sub_id_1 is a list box cat_name_1 is a input box cat_order_1 is an input box the number on the end will be in the same spot and will increase with the tables ID number and will correspond to the data that the ID number belongs to. Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203581 Share on other sites More sharing options...
silkfire Posted April 19, 2011 Share Posted April 19, 2011 Learn how to use proper regex. You gotta check for numbers at the end of the string, nowhere else: $number = preg_replace('#.*(\d+)$#', '$1', $string); Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203582 Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2011 Share Posted April 19, 2011 As Pikachu2000 already suggested, use an array name for your form fields. You will end up with far less code that executes faster as well. The array index value would be your 1,2,3,... that matches the ID in the database table. Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203583 Share on other sites More sharing options...
manvaril Posted April 19, 2011 Author Share Posted April 19, 2011 I would use it as an array but i don't know how to set it up or even how to pull the data out and insert it into my database. Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203586 Share on other sites More sharing options...
drisate Posted April 19, 2011 Share Posted April 19, 2011 It's very easy actualy <input type="text" name="form[]" size="20"><br> <input type="text" name="form[]" size="20"><br> <input type="text" name="form[]" size="20"><br> <input type="text" name="form[]" size="20"><br> <input type="text" name="form[]" size="20"><br> you just add to your field name [] and repeat it the number of times you need Then to loop them you just do it like this: <?php foreach ($_POST['form'] as $key=>$value){ echo $value."<br>"; } // You can also print out a specific value by doing (knowing the array starts at 0) echo $_POST['form'][2]; // will print out the value of the form field number 3 ?> Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1203592 Share on other sites More sharing options...
manvaril Posted April 25, 2011 Author Share Posted April 25, 2011 Well I finally got this thing to do what I wanted it todo and here is how I did it: <?php //This is what my form looks like //First I get a list of the categories from the database and then throw them in a for loop //safe_sting($var) cleans up the input so it can be displayed correctly ie: trim(), stripslashes(), htmlspecialchars() read_cats($cat); for ($i = 0; $i < $cat["count"]; $i++) { echo "<input type=\"text\" name=\"cat_name_" . $cat[$i]["cat_id"] . "\" size=\"30\" maxlength=\"250\" value=\"" . safe_string($cat[$i]["cat_name"]) . "\">\n"; } //To process the form I call my function that pulls out the categories ID number from the field name function formInt($varname, $setZero = true) { $retval = ''; if (isset($_POST[$varname]) && is_numeric($_POST[$varname])) { $retval = (int) $_POST[$varname]; } return $retval; } //Call all the categories from the database and then for loop them through the $_POST data at the same time putting the new values from the $_POST into the database read_cat_list($cat); for ($i = 0; $i < $cat["count"]; $i++) { $sub_id = formInt('cat_sub_id_'.$cat[$i]["cat_id"]); $name = formInt('cat_name_'.$cat[$i]["cat_id"]); $order = formInt('cat_order_'.$cat[$i]["cat_id"]); echo "Sub:" . $sub_id . " Name:" . $_POST['cat_name_'.$cat[$i]["cat_id"].''] . " Order:" . $order ."<br>"; echo "Sub:" . $cat[$i]["cat_sub_id"] . " Name:" . $cat[$i]["cat_name"] . " Order:" . $cat[$i]["cat_order"] ."<br><br>"; $update_query = "update category"; $update_query .= " set cat_sub_id = '" . $sub_id . "', "; $update_query .= " cat_name = '" . clean_string($vars['cat_name_'.$cat[$i]["cat_id"].'']) . "', "; $update_query .= " cat_order = '" . $order . "'"; $update_query .= " where cat_id in (" . $cat[$i]["cat_id"] . ")"; update_db($update_query); } ?> Although this looks a bit cryptic it does the job fairly well and I get the result that I wanted and that was to submit a form of multiple fields to a database and update the records that are in the database Quote Link to comment https://forums.phpfreaks.com/topic/234170-need-help-pulling-the-number-out-of-a-string/#findComment-1205770 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.