+ Post New Thread
Results 1 to 13 of 13
Web Development Thread, If Else in Coding and Web Development; I have one of these that works PHP Code: <?php   if ( $lib_opened [ 'active' ]== '1' ) { echo  "<img src='web/img/full.png' alt='status'>" ;}  elseif ( $lib_num ...
  1. #1

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351

    If Else

    I have one of these that works

    PHP Code:
    <?php  if ($lib_opened['active']=='1') { echo "<img src='web/img/full.png' alt='status'>";}  elseif ($lib_num<'22') {echo "<img src='web/img/tick.png' alt='status'>"; } elseif ($lib_num=='28') { echo "<img src='web/img/full.png' alt='status'>"; } else  {echo "<img src='web/img/warning.png' alt='status'>";
    Tried changing it for another part of my page but can't see where i'm going wrong

    PHP Code:
    if ($lib_opened['active']=='1') { echo "<img src='web/img/full.png' alt='status'>"}  else { echo $lib_num ." of 28 <br />
      PCs used" 
    .; } 
    I just get the error
    PHP Parse error: syntax error, unexpected '}', expecting ',' or ';'
    i've been coding all day and my eyes hurt now!

  2. #2
    penfold_99's Avatar
    Join Date
    Feb 2008
    Location
    East Sussex
    Posts
    1,003
    Thank Post
    60
    Thanked 167 Times in 119 Posts
    Rep Power
    69
    This should work. You had a missing ; on the first echo line and the second one you had an extra .

    PHP Code:
    <?php  
    if ($lib_opened['active']=='1') { 
        echo 
    "<img src='web/img/full.png' alt='status'>";
    } else { 
        echo 
    $lib_num ." of 28 <br /> PCs used"
    }

  3. #3

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    I did that the ; on the first line but in the wrong place and it kicked up a fuss the first time!

    Time for me to sleep and then look at it again tomorrow

    thanks its working now!

  4. #4

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Why are you evaluating $lib_opened as a string?? Is it supposed to be a boolean value? Don't evaluate "if 1", evaluate "if true":

    PHP Code:
    if ($lib_opened['active'] == true
    which protects you against rogue values. (For reference, 0 == false and !0 == true.)

    Then you can take a short cut, and use your variable's sensible name to make it easier to read:

    PHP Code:
    if ($lib_opened['active']) 

  5. #5

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Quote Originally Posted by powdarrmonkey View Post
    Why are you evaluating $lib_opened as a string?? Is it supposed to be a boolean value? Don't evaluate "if 1", evaluate "if true":
    because there are also values for 2 and 3 its not simply a value of 0 and 1

    0 is open, 1, is closed, 2 is closed due to vandalism, 3 is closed for an event
    its simpler to have 0,1,2,3 then random words in the database.

    Its for the Display of computers not being used i have made to slot into Webmans Login Tracker.

    On another part it echo's text rather then an image, but i had that part working.

  6. #6

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by glennda View Post
    because there are also values for 2 and 3 its not simply a value of 0 and 1
    That's fair enough, but you still shouldn't be evaluating it as a string:

    PHP Code:
    if ($lib_opened['active'] == 1

  7. #7

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    How should i do it then?

  8. #8

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    Quote Originally Posted by glennda View Post
    How should i do it then?
    as above:

    Quote Originally Posted by powdarrmonkey View Post
    PHP Code:
    if ($lib_opened['active'] == 1

  9. #9

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Thats how I have it above

    PHP Code:
    if ($lib_opened['active']=='1') {  
        echo 
    "<img src='web/img/full.png' alt='status'>"
    } else {  
        echo 
    $lib_num ." of 28 <br /> PCs used";  

    Apart from no spaces

  10. #10

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,867
    Thank Post
    412
    Thanked 778 Times in 651 Posts
    Rep Power
    182
    No, you don't. You have wrapped the number in single quotes, so it is cast to a string before evaluation. This is a BAD thing and it can lead to hard-to-find bugs.

  11. #11

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Quote Originally Posted by powdarrmonkey View Post
    No, you don't. You have wrapped the number in single quotes, so it is cast to a string before evaluation. This is a BAD thing and it can lead to hard-to-find bugs.
    Ahh yes i see now! both me and the other tech missed that!

    I will change it


    Toby

  12. #12

    sparkeh's Avatar
    Join Date
    May 2007
    Posts
    7,231
    Thank Post
    1,446
    Thanked 1,859 Times in 1,252 Posts
    Blog Entries
    22
    Rep Power
    559
    Quote Originally Posted by glennda View Post
    Thats how I have it above
    No, you don't.
    You have single quotes around the one when evaluating the result meaning you are comparing a the result against a string, which you do not want to do.

    Powdarrmonkey's does not, meaning his compares the result to a value, which you do want to do.

    edit: Too late

  13. #13

    glennda's Avatar
    Join Date
    Jun 2009
    Location
    Sussex
    Posts
    7,821
    Thank Post
    272
    Thanked 1,140 Times in 1,036 Posts
    Rep Power
    351
    Quote Originally Posted by sparkeh View Post
    No, you don't.
    You have single quotes around the one when evaluating the result meaning you are comparing a the result against a string, which you do not want to do.

    Powdarrmonkey's does not, meaning his compares the result to a value, which you do want to do.

    edit: Too late
    i'm already halfway down the corridor with my coat!!!



SHARE:
+ Post New Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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