PHP generated WHERE statement help

Discussion in 'Web Design and Development' started by Cabbit, Oct 7, 2008.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    I made a wee test thingie for making the dynamic WHERE statement, now all i need to work out is how to only have as many AND statements as needed and only output the data i need. Anyone know how to do this?


    PHP:
    <?php
        
    if (!isset($_GET['subpage'])) {
            echo 
    '<form action="search.php?subpage=upload" method="post" />';
            echo 
    'Title: <input type="text" name="title">';
            echo 
    '<br />';
            echo 
    'Body: <input type="text" name="body">';
            echo 
    '<br />';
            echo 
    'Author: <input type="text" name="author">';
            echo 
    '<br />';
            echo 
    '<input type="checkbox" name="1" value="true">';
            echo 
    '<input type="checkbox" name="2" value="true">';
            echo 
    '<input type="checkbox" name="3" value="true">';
            echo 
    '<input type="submit">';
            echo 
    '</form>';
        }
        if (isset(
    $_GET['subpage']) AND $_GET['subpage'] == "upload") {
            
    $title $_POST['title'];
            
    $body $_POST['body'];
            
    $author $_POST['author'];
            
    $num1 $_POST['1'];
            
    $num2 $_POST['2'];
            
    $num3 $_POST['3'];
            if (isset(
    $_POST['title'])) {
                if (
    $_POST['title'] == "") {
                    
    // nought //
                
    }
                else {
                
    $title_string "
                    story.`title` LIKE'%
    $title%'
                    OR
                    LOWER(story.`title`) LIKE '%
    $title%'
                    OR
                    UPPER(story.`title`) LIKE '%
    $title%'
                "
    ;
                }
            }
            if (isset(
    $_POST['author'])) {
                if (
    $_POST['author'] == "") {
                    
    // nought //
                
    }
                else {
                
    $author_string "
                    story.`author` LIKE'%
    $author%'
                    OR
                    LOWER(story.`author`) LIKE '%
    $author%'
                    OR
                    UPPER(story.`author`) LIKE '%
    $author%'
                "
    ;
                }
            }
            if (isset(
    $_POST['body'])) {
                if (
    $_POST['body'] == "") {
                    
    // nought //
                
    }
                else {
                
    $body_string "
                    story.`body` LIKE'%
    $body%'
                    OR
                    LOWER(story.`body`) LIKE '%
    $body%'
                    OR
                    UPPER(story.`body`) LIKE '%
    $body%'
                "
    ;
                }
            }
            if (isset(
    $_POST['1'])) {
                if (
    $_POST['1'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num1_string "
                    key.`1` = '
    $num1'
                "
    ;
                }
            }
            if (isset(
    $_POST['2'])) {
                if (
    $_POST['2'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num2_string "
                    key.`2` = '
    $num2'
                "
    ;
                }
            }
            if (isset(
    $_POST['3'])) {
                if (
    $_POST['3'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num3_string "
                    key.`3` = '
    $num3'
                "
    ;
                }
            }
            echo 
    'WHERE '.$title_string.' AND '.$author_string.' AND '.$body_string.' AND '.$num1_string.' AND '.$num2_string.' AND '.$num3_string.''
            echo 
    '<br />';
            echo 
    '<a href="search.php">back</a>';
        }
    ?>
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #2
    trying this but the result is empty. anyone know why?

    PHP:
    <?php
        
    if (!isset($_GET['subpage'])) {
            echo 
    '<form action="search.php?subpage=upload" method="post" />';
            echo 
    'Title: <input type="text" name="title">';
            echo 
    '<br />';
            echo 
    'Body: <input type="text" name="body">';
            echo 
    '<br />';
            echo 
    'Author: <input type="text" name="author">';
            echo 
    '<br />';
            echo 
    '<input type="checkbox" name="1" value="true">';
            echo 
    '<input type="checkbox" name="2" value="true">';
            echo 
    '<input type="checkbox" name="3" value="true">';
            echo 
    '<input type="submit">';
            echo 
    '</form>';
        }
        if (isset(
    $_GET['subpage']) AND $_GET['subpage'] == "upload") {
            echo 
    '<a href="search.php">back</a>';
            
    $title $_POST['title'];
            
    $body $_POST['body'];
            
    $author $_POST['author'];
            
    $num1 $_POST['1'];
            
    $num2 $_POST['2'];
            
    $num3 $_POST['3'];
            if (isset(
    $_POST['title'])) {
                if (
    $_POST['title'] == "") {
                    
    // nought //
                
    }
                else {
                
    $title_string "
                    story.`title` LIKE'%
    $title%'
                    OR
                    LOWER(story.`title`) LIKE '%
    $title%'
                    OR
                    UPPER(story.`title`) LIKE '%
    $title%'
                "
    ;
                }
            }
            if (isset(
    $_POST['author'])) {
                if (
    $_POST['author'] == "") {
                    
    // nought //
                
    }
                else {
                
    $author_string "
                    story.`author` LIKE'%
    $author%'
                    OR
                    LOWER(story.`author`) LIKE '%
    $author%'
                    OR
                    UPPER(story.`author`) LIKE '%
    $author%'
                "
    ;
                }
            }
            if (isset(
    $_POST['body'])) {
                if (
    $_POST['body'] == "") {
                    
    // nought //
                
    }
                else {
                
    $body_string "
                    story.`body` LIKE'%
    $body%'
                    OR
                    LOWER(story.`body`) LIKE '%
    $body%'
                    OR
                    UPPER(story.`body`) LIKE '%
    $body%'
                "
    ;
                }
            }
            if (isset(
    $_POST['1'])) {
                if (
    $_POST['1'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num1_string "
                    key.`1` = '
    $num1'
                "
    ;
                }
            }
            if (isset(
    $_POST['2'])) {
                if (
    $_POST['2'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num2_string "
                    key.`2` = '
    $num2'
                "
    ;
                }
            }
            if (isset(
    $_POST['3'])) {
                if (
    $_POST['3'] == "") {
                    
    // nought //
                
    }
                else {
                
    $num3_string "
                    key.`3` = '
    $num3'
                "
    ;
                }
            }
            
    $strArray[] = $title_string
            
    $strArray[] = $author_string
            
    $strArray[] = $body_string
            
    $strArray[] = $num1_string
            
    $strArray[] = $num2_string
            
    $strArray[] = $num3_string
            
    $queryStr 'WHERE '
            
    $andStr ' AND '
            foreach(
    $strArray as $str
            { 
                   if(!
    isempty($str)) 
                   { 
                     
    $queryStr $queryStr $str $andStr
                   } 
            }     
            
    // Remove last $andStr added to str 
            
    $queryStr substr($queryStr0strlen($queryStr)-strlen($andStr)); 
            echo 
    $queryStr;
        }
    ?>
     
  3. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    ah worked it out its if(!empty($str)) not if(!isempty($str)).
     

Share This Page