Solar Posted May 21, 2012 Share Posted May 21, 2012 What is the most effect approach to doing this? <?php $catid = $_POST['catid']; if($catid = "Automotive"){ $catid = '3'; } if($catid = "Business"){ $catid = '4'; } if($catid = "Careers"){ $catid = '5'; } if($catid = "Education"){ $catid = '6'; } if($catid = "Financial"){ $catid = '7'; } if($catid = "Government"){ $catid = '8'; } if($catid = "Health"){ $catid = '9'; } if($catid = "Mobile"){ $catid = '10'; } if($catid = "Organization"){ $catid = '11'; } if($catid = "Programming"){ $catid = '12'; } if($catid = "Software"){ $catid = '13'; } if($catid = "Travel"){ $catid = '14'; } if($catid = "Web"){ $catid = '15'; } if($catid = "Other"){ $catid = '16'; } if($catid = "Gaming"){ $catid = '17'; } ?> I tried this and it works; <?php $catid = "Automotive"; if($catid = "Automotive"){ $catid = '3'; } echo $catid; ?> But... When I add another if statement... <?php $catid = "Automotive"; if($catid = "Automotive"){ $catid = '3'; } if($catid = "Gaming"){ $catid = '17'; } echo $catid; ?> It echos $catid as 17 and not 3 which is defined as a variable. I hope there is a simple solution for this! Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/ Share on other sites More sharing options...
smerny Posted May 21, 2012 Share Posted May 21, 2012 1) you should be using "elseif" for situations like this (when there can only be one answer) 2) the error is because you are using a single = sign, rather than a double ==, the double == is used for comparison, the single is used for setting... your code explained: <?php $catid = "Automotive"; // set catid to "Automotive" if($catid = "Automotive"){ // set catid to "Automotive", if set is successful... $catid = '3'; // ...set catid to "3" } if($catid = "Gaming"){ //set catid to "Gaming", if set is successful... $catid = '17'; // ...set catid to "17" } echo $catid; ?> Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/#findComment-1347176 Share on other sites More sharing options...
.josh Posted May 21, 2012 Share Posted May 21, 2012 First off, in your conditions, = is the assignment operator, whereas == is the equality operator...you should be using == in your conditions. But anyways, you can cut all those conditions out by using an array: $catIDs = array( 3 => "Automotive", 4 => "Business", // etc... ); $catid = array_search(trim($_POST['catid']), $catIDs); echo $catid; array_search will search the array and return the key (like 3,4, etc..) or if value not found, it will return boolean false. Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/#findComment-1347177 Share on other sites More sharing options...
Solar Posted May 21, 2012 Author Share Posted May 21, 2012 Thanks for the fast replies! I figured it out using the manual way. No matter how many times I try doing arrays... They never work for me. My form selection is using Automotive, Business, Careers, etc... So I want them to transfer to numbers so when inserted with mysql, they display correctly in the blog id categories. <?php if($_POST['catid']=="Automotive"){ $catid = '3'; } if($_POST['catid']=="Business"){ $catid = '4'; } if($_POST['catid']=="Careers"){ $catid = '5'; } if($_POST['catid']=="Education"){ $catid = '6'; } if($_POST['catid']=="Financial"){ $catid = '7'; } if($_POST['catid']=="Government"){ $catid = '8'; } if($_POST['catid']=="Health"){ $catid = '9'; } if($_POST['catid']=="Mobile"){ $catid = '10'; } if($_POST['catid']=="Organization"){ $catid = '11'; } if($_POST['catid']=="Programming"){ $catid = '12'; } if($_POST['catid']=="Software"){ $catid = '13'; } if($_POST['catid']=="Travel"){ $catid = '14'; } if($_POST['catid']=="Web"){ $catid = '15'; } if($_POST['catid']=="Other"){ $catid = '16'; } if($_POST['catid']=="Gaming"){ $catid = '17'; } ?> If I were to do the array.... Would I want that backwards? "Automotive" => 3, "Business" => 4, etc... Or the way you have it is fine? I get an error. Notice: Undefined index: catid Warning: array_search() expects at least 2 parameters, 1 given Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/#findComment-1347180 Share on other sites More sharing options...
.josh Posted May 21, 2012 Share Posted May 21, 2012 array_search() searches the array values and returns the key an array is array(key => value). As far as the Notice...maybe you snagged the code before I edited it..I forgot to add the 2nd param when I first posted it. Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/#findComment-1347181 Share on other sites More sharing options...
Solar Posted May 21, 2012 Author Share Posted May 21, 2012 I snagged before you had edit your post. $catIDs = array( 3 => "Automotive", 4 => "Business", 5 => "Careers", 6 => "Education", 7 => "Financial", 8 => "Government", 9 => "Health", 10 => "Mobile", 11 => "Organization", 12 => "Programming", 13 => "Software", 14 => "Travel", 15 => "Web", 16 => "Other", 17 => "Gaming" ); $catid = array_search(trim($_POST['catid']), $catIDs); This works WAY better. Cleaner, and nicer. This will open up more solutions around my website! Thankyou very much! Quote Link to comment https://forums.phpfreaks.com/topic/262847-help-if-post-variable/#findComment-1347183 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.