SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with conditional phrase

    I generated a variable by means of a select statement in a form.
    HTML Code:
        <label for="outcome">How do you want to move the needle?</label>
        <select class="medium" name="outcome" size="1" id="outcome">
            <option value="revenue" selected="selected">Increased revenue</option>
            <option value="cost">Reduce costs</option>
            <option value="customer">Customer satisfaction</option>
            <option value="other">Other  Details below</option>
        </select>
    In the action script, I captured the variable in
    PHP Code:
      $outcome $_POST['outcome']; 
    Now I want to display various information depending on the value of 'outcome'. I have tried
    PHP Code:
    if ($outcome=="revenue"
        {
        
    ?>
            <p>Information about revenue.</p>
        } 
    which doesn't work. What am I missing?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Generally, it's easier to stay in PHP and echo, rather than coming out of PHP:
    PHP Code:
    <?php
    switch(strtolower($outcome)){ //the StrToLower is there to compare the string case-insensitively
        
    case 'revenue':
            echo 
    '<p>Information about Revenue</p>';
        break;
        case 
    'blah':
            echo 
    '<p>Blahhhhhhh</p>';
        break;
    }
    However, if you're always going to have the same style of title for every outcome:
    PHP Code:
    printf('<p>Information about %s</p>'$outcome); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The following code is not working correctly.
    PHP Code:
    echo "<h3>You said <em> $outcome</em></h3>";
    switch (
    $outcome){
        case 
    $outcome "Increase revenues";
            echo 
    "<p>Revenue blah, blah, blah.</p>";
            break;
        case 
    $outcome "Reduce costs":
            echo 
    "<p>Reduce costs blah, blah, blah</p>";
            break;    
        case 
    $outcome "Improve customer satisfaction":
            echo 
    "<p>Customer satisfaction blah, blah, blah</p>";
            break;    
        case 
    $outcome "Other":
            echo 
    "<p>Other blah, blah, blah</p>";
            break;    

    When $outcome is set to "Reduce costs", this part works correctly
    PHP Code:
    echo "<h3>You said <em> $outcome</em></h3>" 
    i.e. $outcome displays "Reduce costs".

    However the case part displays
    HTML Code:
    <p>Revenue blah, blah, blah.</p>
    , which should be
    HTML Code:
    <p>Reduce costs blah, blah, blah</p>
    Please advise.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a semicolon at the end of that line instead of a colon, iti isn't necessary to have the $outcome = .

    PHP Code:
        case "Increase revenues"

  5. #5
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paulcj2 View Post
    The following code is not working correctly.
    PHP Code:
    echo "<h3>You said <em> $outcome</em></h3>";
    switch (
    $outcome){
        case 
    $outcome "Increase revenues";
            echo 
    "<p>Revenue blah, blah, blah.</p>";
            break;
        case 
    $outcome "Reduce costs":
            echo 
    "<p>Reduce costs blah, blah, blah</p>";
            break;    
        case 
    $outcome "Improve customer satisfaction":
            echo 
    "<p>Customer satisfaction blah, blah, blah</p>";
            break;    
        case 
    $outcome "Other":
            echo 
    "<p>Other blah, blah, blah</p>";
            break;    

    you need to set the case value so

    PHP Code:
    echo "<h3>You said <em> $outcome</em></h3>";
    switch (
    $outcome){
        case 
    "Increase revenues";
            echo 
    "<p>Revenue blah, blah, blah.</p>";
            break;
        case 
    "Reduce costs":
            echo 
    "<p>Reduce costs blah, blah, blah</p>";
            break;    
        case  
    "Improve customer satisfaction":
            echo 
    "<p>Customer satisfaction blah, blah, blah</p>";
            break;    
        case 
    "Other":
            echo 
    "<p>Other blah, blah, blah</p>";
            break;    


    Would Work, you might also want to add in at the end generally

    [PHP] default:
    echo "<p>Invalid input selection</p>"; break;

  6. #6
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I went with the first option.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  7. #7
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,381
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. It's working great now.
    Paul C.
    ClickBasics
    http://www.clickbasics.com


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
  •