SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: if statements

  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if statements

    Hello, I am trying to create an if statement that will either produce one if statement or anouther. This is for checking form fields to see if they are empty for certain forms. The code is below. I have no idea how to do this task.

    PHP Code:
    if($form == 'hosting') {
    if (!
    $name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone || !$domain || !$user3 || !$pass3 || !$tos || (strlen($user3)< OR strlen($user3) > 8) || (strlen($pass3)< OR strlen($pass3) > 8)) {
    } else {
    if (!
    $name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone || !$tos) {



    I know that is a really long if statement and it is very confusing. Any ideas how to do this?
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could always throw it in a variable and then run it through eval. That'll be dangerous if you aren't checking for data before you run it though (which you probably aren't since this seems to the data checking).

    I would probably create an array:
    PHP Code:
    if($form == 'hosting')
    {
    $check_fields = array('name3','street'); //and all the other fields for the first sample
    }
    else
    {
    $check_fields = array('name3'); //add others.

    And then to check if they were filled, you could use a foreach loop:

    PHP Code:
    foreach($check_fields as $field)
    {
    if(!
    $_POST[$field]) { //do your error stuff here }

    That should work out assuming you are collecting your data from a form through the POST method.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still having a slight problem with things. I tried the arrays but that did not prove to be what I need I believe. The whole code that I am working with is located at:
    http://www.wlhosting.com/temp/order/order.txt

    the demo is located at:
    http://www.wlhosting.com/temp/order/

    The issue for me is trying to remove the required domain, user, and pass from the really long top if statement for the scripts part. Because if you try to fill in the scripts part it will not work all the way because it is still trying to fill in the domain, user, and pass. If you try the hosting page it will run to the last screen which is what it is supposed to do. Any suggestions?
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  4. #4
    public static void brain Gybbyl's Avatar
    Join Date
    Jun 2002
    Location
    Montana, USA
    Posts
    647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should try something like this:
    PHP Code:
    foreach($_POST as $key => $value)
    {
        if(
    $value == "")
        {
            print(
    "You left ".$key." blank in your form!");
        }

    This will check all of the form elements that were submitted and make sure that they contained a value. That seems to be what your problem is, correct? Just validating a form?

    I'm not really sure what you mean by 'having an if statement generate another if statement.' Could you be a little more specific? I know it's probably in your code, but that looks like a nightmare imo (no offense).
    Ryan

  5. #5
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    In a house in the USA
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am currently using one file called order.php and then having a variable choose what items to choose. If I have the hosting form it works fine because of the if statement at the top. But when I move to the scripts page the if statement at the top has 6 variables that do not need to be there in it.

    Here is the hosting check:
    PHP Code:
    if (!$name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone || !$domain || !$user3 || !$pass3 || !$tos || (strlen($user3)< OR strlen($user3) > 8) || (strlen($pass3)< OR strlen($pass3) > 8)) { 


    I am wanting the check if not hosting to be:
    PHP Code:
    if (!$name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone) { 


    Is there a statement that would check if it was hosting and then choose which if statement to choose from? Any suggestions?
    Daniel
    http://www.wlscripting.com - PHP Tutorials and code snippets
    Notepad++ Function List plugin tip - for PHP developers

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't like the foreach statement, you could simply nest the if statements.

    PHP Code:
    if($form == 'hosting')
    {
     if (!
    $name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone || !$domain || !$user3 || !$pass3 || !$tos || (strlen($user3)< OR strlen($user3) > 8) || (strlen($pass3)< OR strlen($pass3) > 8))
     {
      
    //do your stuff
     
    }
    }
    else
    {
     if (!
    $name3 || !$street || !$city || !$sp || !$zpc || !$country || !$mailCheck || !$phone || !$tos)
     {
      
    //do your stuff
     
    }


  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2003
    Location
    Singapore
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would prefer isset() or empty() instead of checking for failure.

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. biggulp does present avalid point. Check for true or false means that if a zero was entered in one of your feilds, the script would still fail. It's best to use one of his two listed methods to ensure you know what you are getting.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •