SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: if else Trouble

  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if else Trouble

    HI all,

    I have this snippet of code:
    PHP Code:
    <P>Visible:<BR>
    <input type=radio name="visible" value="Y" <?php if ($visible == Y) {
     echo(
    "CHECKED");

    } else {
     echo(
    "");
    }
    ?>>Yes&nbsp;<input type=radio name="visible" value="N" <?php if ($visible == N) {

     echo(
    "CHECKED");

    } else {

     echo(
    "");

    }
    ?>>No</P>
    This is part of a larger script that connects to the database and a certain table. Then, I have the above code. You can probably figure out what it's supposed to do.

    I'm not getting errors, but it is not printing CHECKED at all. I'm positive that the database has this registered (as opposed to being an empty field.)

    Any help is greatly appreciated.

    -Corbb
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    UK
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe quotes on the Y and N values? I'm no expert...

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TechSited,

    Thanks for your help, but still the same problem.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2000
    Location
    Bangkok,Thailand
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Try this

    PHP Code:
    <?php
    //pull it from your table column (I called it visible in this example) 

    $visible=$myrow["visible"];

    //use switch to determine what value is in the cell

    switch ($visible) {
    //if yes mark the yes as checked
    case"Y":
        
    $visibleY=("checked=\"checked\"");
        break;
    //if no mark no as checked
    case "N":
        
    $visibleN=("checked=\"checked\"");
        break;
    }
    //then spit it out
    ?>

    <input type="radio" value="Y" name="visible" <? echo $visibleY ?> />Yes Please<br />
    <input type="radio" value="N" name="visible" <? echo $visibleN ?> />No thanks<br />

    I've trimmed this down from a chuck of code where there were at elast a dozen choices - it worked fine, it should work with just your two - though there is probably a more efficient way to do it than by using switch

    HTH
    Stuart
    http://www.travelfish.org

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Stuart,

    Thanks for your help. However, I'm still gettting the same problem. Here's my code:
    PHP Code:
    <P>Visible:<BR>
    <?php

    //use switch to determine what value is in the cell

    switch ($visible) {
    //if yes mark the yes as checked
    case"Y":
        
    $visibleY=("checked=\"checked\"");
        break;

    //if no mark no as checked
    case "N":
        
    $visibleN=("checked=\"checked\"");
        break;
    }

    ?>
    <input type="radio" value="Y" name="visible" <? echo $visibleY ?> />Yes
    <input type="radio" value="N" name="visible" <? echo $visibleN ?> />No</P>
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  6. #6
    code addict Abstraction's Avatar
    Join Date
    Apr 2001
    Location
    Des Moines, IA
    Posts
    346
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo the contents of $visible out somewhere that you can see what it's value is.

  7. #7
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Asking a dumb question just in case, there's no chance you have a another checked radio button later on the page, with the the name "visible"?

    The last button checked in the group overides all previous.

  8. #8
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everybody for your help.

    I got it fixed.

    I had accidentally named my variable $isvisible instead of $visible.

    Thanks again for all of your help,
    -Corbb
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!


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
  •