Jump to content

Recommended Posts

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.

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?

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

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);

 

 

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.

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.

 

 

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

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

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.