• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

php mysql strip value

Hi all,
I have a php script that gets values from my mysql database.  I have an add page which the form sends the data to the add2 page.  On the add page, I have over 100 values from checkboxes.  What I would like to do on the add2 page (before the data is added to the database) is check the names of the values for some character (ie. #) and if a value has a # in it, then strip everything before the # (including the #) for the value.  ie. add passes 3 names to the add2 page; name1, something#name2 and somethingelse#name3.  The add2 page then strips the names to become; name1, name2, name3 and then runs the rest of the page (adds data to database).  I can do this value by value, but because there are so many, I was hoping that I can just have a small snipplet before the data is added.

thanks in advance.
0
newbe101
Asked:
newbe101
  • 9
  • 9
  • 3
1 Solution
 
ScottMcintyreCommented:
The easiest solution would be to foreach loop the $_POST for the form and then to explode # for the name,

Check out,

http://www.php.net/foreach
http://www.php.net/explode

Small example (obviously you'd need to `for` the explode array).

foreach ($_POST as $key => $value) {
$names = explode("#",$names);
$name1 = $names[0];
$$key = addslashes(trim($name1));
}
0
 
MasonWolfCommented:
I don't think the first solution would actually work, but I see what he's getting at, and his idea makes sense. Try this one instead.

foreach($_POST as $key => $value)
{
   if(strstr($value,'#'))
       $_POST[$key] = array_pop(explode('#',$value));
}

the result would be a $_POST array that only has the values you want
0
 
ScottMcintyreCommented:
MasonWolf: It wouldn't work neither would the solution you provided since they are only examples.

We don't have enough info to gauge what he wants to do with the information or what is provided.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
MasonWolfCommented:
ScottMcintyre: I'm not trying to insult you, but you did accidentally put "$names" as a variable where you meant "$value", and newbe101 wants the data AFTER the #, not before - which is what your solution offers. I still think you hit on the right method in general - you just needed help with the specifics. Please don't take it personal. It was not meant that way.
0
 
newbe101Author Commented:
here is an example of what is being passed (checkbox):
<input type="checkbox" id="Brakes#roptselect1" name="Brakes#roptselect1" value="33.00" onclick="this.form.total.value=calculateTotal(this); SingleSelect('Brakes#roptselect',this);" />
this goes on for multiple checkboxes.... some of which may or may not have the # in them.  What I need is the name to become roptslecet1 before I add it to the database...

I will play with your examples and see what I can come up with.
0
 
ScottMcintyreCommented:
MasonWolf: I don't take anything personal,  I was simply highlighting it was only an example.  There more than just the $names being in the explode,  the whole thing doesn't do what he wants because if it did what would be the point in asking advice?  He'd be asking for the script instead.

Also just to touch upon after the #, he did not state that (his message says to get everything and then split the text).

The same answers still apply,

http://www.php.net/foreach
http://www.php.net/explode

Anyway just thought i'd clarify.
0
 
MasonWolfCommented:
OH! You want the name changed, not the value. Ok...

foreach($_POST as $key => $value)
{
   if(strstr($key,'#'))
       $key = array_pop(explode('#',$value));
   $$key = $value;
}
0
 
MasonWolfCommented:
(Sorry - I realized the mistake as it was submitting)

foreach($_POST as $key => $value)
{
   if(strstr($key,'#'))
       $key = array_pop(explode('#',$key));
   $$key = $value;
}
0
 
newbe101Author Commented:
MasonWolf,
it's not working... here is what I have for the add2.php file:
<?
include("../connect.php");

foreach($_POST as $key => $value)
{
   if(strstr($key,'#'))
       $key = array_pop(explode('#',$value));
   $$key = $value;
}


     $leads_id=$_POST['leads_id'];
     $leads_date=$_POST['leads_date'];       
     $leads_location=$_POST['leads_location'];
...
       $roptselect1=$_POST['roptselect1'];             
       $roptname1=$_POST['roptname1'];
       $ropt1=$_POST['ropt1'];
       $dopt1=$_POST['dopt1'];
       $copt1=$_POST['copt1'];             
       $roptselect2=$_POST['roptselect2'];             
       $roptname2=$_POST['roptname2'];
       $ropt2=$_POST['ropt2'];
       $dopt2=$_POST['dopt2'];
       $copt2=$_POST['copt2'];
...
     $query = "INSERT INTO leads VALUES ('','$leads_date','$leads_location','$leads_name',
...
'$roptselect1','$roptname1','$ropt1','$dopt1','$copt1','$roptselect2','$roptname2','$ropt2','$dopt2','$copt2'
...
)";
     mysql_query($query);
      
mysql_close($link);
0
 
newbe101Author Commented:
OK, what if I just do this the long way... How would I create an if statement that trims everything before the #.  ie.
if (Brakes#roptselect1=$_POST['Brakes#roptselect1']) { $roptselect1 = "$_POST['Brakes#roptselect1']"; } else { $roptselect1 = "$_POST['roptselect1']"; }
I tried this but I think the # is messing up the code.
0
 
MasonWolfCommented:
In what way does it not work?
0
 
newbe101Author Commented:
Does it look like it should?  I am using dreamweaver for editing purposes and the # looks like it is a comment or something... or do you mean the last results... if so, It is not getting the proper name for $roptselect1=$_POST['roptselect1'];
0
 
newbe101Author Commented:
$roptionselect1 name is being passed as Brakes#roptionselect1.  I tried echo "$roptselect1"; right after your code, and I didn't get anything.
0
 
MasonWolfCommented:
oh - ok

$roptselect was set to the value contained in $_POST['Brakes#roptselect'] but then your code overwrote that value a little later. Remove all the declarations past the bit that I gave you, and it should work fine.
0
 
newbe101Author Commented:
still didn't work... here is what I have now:

<?
include("../connect.php");

foreach($_POST as $key => $value)
{
   if(strstr($key,'#'))
       $key = array_pop(explode('#',$value));
   $$key = $value;
}
     @mysql_select_db($db) or die( "Unable to select database");
$query = "INSERT INTO leads VALUES ('','$leads_date','$leads_location','$leads_name',
...
$roptselect1','$roptname1','$ropt1','$dopt1','$copt1','$roptselect2','$roptname2','$ropt2','$dopt2','$copt2','$roptselect3','$roptname3','$ropt3','$dopt3','$copt3','$roptselect4'
...
)";
     mysql_query($query);
      
mysql_close($link);

       echo "ROPTIONSELECT<br>$roptselect1<br>";
       echo "$roptselect2<br>";
       echo "$roptselect3<br><br>";
?>
it is still adding everything else, just not the items with the # in it.
0
 
newbe101Author Commented:
actually, now it is not getting any checked values... just name, address, etc.
0
 
MasonWolfCommented:
please add this line:

var_dump($_POST);

Show me what prints out, and I'll use that as test data. I'll see if I can get it working on my end, and then I'll send you back the script I end up with.
0
 
newbe101Author Commented:
yeah, it's not getting the variables with the # (ie. roptselect 1 - 3):

array(96) { ["leads_date"]=> string(10) "07/24/2007" ["leads_make"]=> string(9) "Speedster" ["leads_model"]=> string(3) "GTR" ["leads_status"]=> string(1) "1" ["leads_location"]=> string(6) "Malibu" ["leads_name"]=> string(4) "fsdf" ["leads_phone"]=> string(5) "fdsaf" ["leads_address1"]=> string(0) "" ["leads_email"]=> string(5) "fasdf" ["leads_city"]=> string(0) "" ["leads_state"]=> string(0) "" ["leads_zip"]=> string(0) "" ["roptselect4"]=> string(4) "3000" ["roptname4"]=> string(6) "1915cc" ["ropt4"]=> string(4) "3000" ["dopt4"]=> string(4) "2000" ["copt4"]=> string(4) "1000" ["roptselect5"]=> string(3) "234" ["roptname5"]=> string(4) "asdf" ["ropt5"]=> string(3) "234" ["dopt5"]=> string(2) "34" ["copt5"]=> string(2) "23" ["roptselect12"]=> string(2) "30" ["roptname12"]=> string(14) "Steering Wheel" ["ropt12"]=> string(2) "30" ["dopt12"]=> string(2) "20" ["copt12"]=> string(2) "10" ["roptname13"]=> string(4) "ross" ["ropt13"]=> string(2) "30" ["dopt13"]=> string(2) "20" ["copt13"]=> string(2) "10" ["roptname14"]=> string(4) "test" ["ropt14"]=> string(1) "3" ["dopt14"]=> string(1) "2" ["copt14"]=> string(1) "1" ["roptname15"]=> string(4) "Blue" ["ropt15"]=> string(4) "2000" ["dopt15"]=> string(4) "1500" ["copt15"]=> string(3) "900" ["roptname16"]=> string(10) "Flat Black" ["ropt16"]=> string(1) "0" ["dopt16"]=> string(1) "0" ["copt16"]=> string(1) "0" ["roptname17"]=> string(6) "Silver" ["ropt17"]=> string(1) "0" ["dopt17"]=> string(1) "0" ["copt17"]=> string(1) "0" ["Brakes#roptselect1"]=> string(5) "33.00" ["roptname1"]=> string(11) "Disk Brakes" ["ropt1"]=> string(5) "33.00" ["dopt1"]=> string(5) "22.00" ["copt1"]=> string(5) "12.00" ["roptname2"]=> string(11) "Drum Brakes" ["ropt2"]=> string(7) "5000.00" ["dopt2"]=> string(7) "4000.00" ["copt2"]=> string(7) "3000.00" ["roptname3"]=> string(13) "5 Bolt Brakes" ["ropt3"]=> string(2) "40" ["dopt3"]=> string(2) "30" ["copt3"]=> string(2) "20" ["roptselect6"]=> string(2) "10" ["roptname6"]=> string(4) "fdas" ["ropt6"]=> string(3) "std" ["dopt6"]=> string(3) "std" ["copt6"]=> string(3) "std" ["Tires#roptselect7"]=> string(1) "3" ["roptname7"]=> string(4) "asdf" ["ropt7"]=> string(1) "3" ["dopt7"]=> string(1) "2" ["copt7"]=> string(1) "1" ["roptname8"]=> string(4) "asdf" ["ropt8"]=> string(1) "3" ["dopt8"]=> string(1) "2" ["copt8"]=> string(1) "1" ["roptname9"]=> string(11) "White Walls" ["ropt9"]=> string(6) "395.00" ["dopt9"]=> string(6) "300.00" ["copt9"]=> string(6) "199.00" ["roptselect10"]=> string(3) "225" ["roptname10"]=> string(13) "Leather Seats" ["ropt10"]=> string(3) "std" ["dopt10"]=> string(3) "std" ["copt10"]=> string(3) "std" ["roptname11"]=> string(4) "test" ["ropt11"]=> string(1) "6" ["dopt11"]=> string(1) "5" ["copt11"]=> string(1) "4" ["calculatedTotal"]=> string(5) "33795" ["previouslySelectedRadioButton"]=> string(1) "0" ["leads_msrp"]=> string(5) "29995" ["leads_dealer"]=> string(5) "22000" ["leads_cost"]=> string(5) "19000" ["leads_shipping"]=> string(3) "500" ["optionstotal"]=> string(4) "3300" ["total"]=> string(10) "$33,795.00" ["Submit"]=> string(13) "Request Build" }
0
 
MasonWolfCommented:
whoops - :)
07.24.2007 at 12:19PM PDT, ID: 19559113
"MasonWolf:
(Sorry - I realized the mistake as it was submitting)

foreach($_POST as $key => $value)
{
   if(strstr($key,'#'))
       $key = array_pop(explode('#',$key));
   $$key = $value;
}"

You used the one I put up by mistake just a few seconds earlier. I had accidentally written "$value" in a spot where I had meant to put "$key"
0
 
newbe101Author Commented:
Your are awsome!  THank you!  it works!  The javascript issue is having one last minor issue, I left some notes on it.

THANK YOU FOR ALL OF YOUR HELP!!!
0
 
MasonWolfCommented:
:D Glad I can help. I'm looking over your javascript issue right now.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now