Jump to content

[HELP] If $POST = Variable


Solar

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

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.