Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: 2 questions, 1 with a failed script. :p

  1. #1
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking 2 questions, 1 with a failed script. :p

    hi im new to php and most of the coding languages.. I tried to fix a script that would choose a stylesheet depending on the usersbrowser.. Unfortionatly it didnt work
    Code:
    Number 1.
    
              <?php $style1
                       $style2
                       $browser = getenv("HTTP_USER_AGENT");
    
                       if $browser = mozilla;
                       <link href="style.css" rel="stylesheet" type="text/css" />;
                       else
                       <link href="style2.css" rel="stylesheet" type="text/css" />;
               ?>
    am I totally outta bounce or is it possible?

    and then I had anotheer question, is it possible to put a "noreload" on a menu, banner, logo and make my index div "like" a fram? http://underground.ninetwozero.com

    peace - kalle

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    the second question isn't related to php, so not sure on that, since php is my area, for the most part.


    As for the code, there are several problems. I suppose that the line break after the if followed by a semicolon might work, but if you need multiple lines, so it like: if (...) {......}, surrounding the next few lines in brackets.
    The main issue is that mozilla is not something standalone in PHP... it needs to be a string. So... 'mozilla', if nothing else. Here's a better way of trying it, though no browser detection is flawless.
    PHP Code:
    $browser strtolower($browser);
    if (
    strpos($browser,'mozilla') !== FALSE) {
      echo 
    '<link href="style.css" rel="stylesheet" type="text/css" />';
      }
    else
      echo 
    '<link href="style2.css" rel="stylesheet" type="text/css" />';
      } 
    PHP Code:
    <?php
    //....
    $browser strtolower($browser); //convert the user agent string to lowercase
    //now MOZILLA = Mozilla = mozilla... etc.
    if (strpos($browser,'mozilla') !== FALSE) { //if 'mozilla' exists within $browser
    //! means 'not', === means exactly equal to*, so !== means is not exactly equal to FALSE
    //*== is equal to, but it sees 0, '' and FALSE as the same, so === seperates by type as well.
      
    echo '<link href="style.css" rel="stylesheet" type="text/css" />'//output style1
      
    //close if
    else { //else with bracket
      
    echo '<link href="style2.css" rel="stylesheet" type="text/css" />'//output style2
      
    //close else
    //....
    ?>
    The text can't just sit there either... it needs to be output (echo function) and be inside quotes. (" is fine, but ' works, since you have double in the string. If you needed to use ", then you'd need to use, for example, href=\"style2.css\".... to escape the quotes in that.)
    Last edited by djr33; 01-06-2007 at 10:50 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    holy sh*t... did you just solve my problem? ima try it out results back in a min.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Note: I just edited the post a bit to make it clearer, with comments and such. Refresh and look at that if you haven't seen it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    hmm it didnt quite work the way i thought but atleast its working.. now we needa know what the browsers called since mozilla seemed to be invalid..

    http://underground.ninetwozero.com/test.php

  6. #6
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i got another idea.. cant we put like if browser is IE then a special style. else = FF etc = other style?

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Well, what I put SHOULD check if 'mozilla' exists anywhere within the string of the user agent. That seems logical to me.

    If you want to see what the user agent string looks like, make a page with just this code:
    <?php
    echo $_SERVER['HTTP_USER_AGENT'];
    ?>

    Also, I'd replace your user agent line in your PHP with:
    $_SERVER['HTTP_USER_AGENT'];

    $_SERVER is what I've used. getenv doesn't seem needed, though it might work as well, but I wouldn't know. It may also be outdated, and be the old method to do it, if you read that somewhere, or something.



    Yes. Just copy/paste the syntax, like this:
    if (....) {
    ...
    }
    else if (....) {
    ...
    }
    else {
    ...
    }

    (duplicate else if as much as needed)

    Also, elseif is valid in php, but using else if is a good habit, since that's valid in other languages as well.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ill try it later, gotta sleep now. thx, ill give u an update tomorrow

    gnite.

  9. #9
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    this is what i get with FF:

    Mozilla/5.0 (Windows; U; Windows NT 5.1; sv-SE; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1

    IE:

    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2)

    so practiclly "MSIE" should be a good "searcher" for the string.. since it aint countin mozilla, jus pickin style2.css when sum1 enters.

  10. #10
    Join Date
    Jan 2007
    Location
    Sweden
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok i tried to modify your script.. without success..
    Code:
    <?php
    
    $browser = strtolower($browser);
    if (strpos($browser,'MSIE') !== FALSE) { 
      echo '<link href="style2.css" rel="stylesheet" type="text/css" />';
      }
    else {
      echo '<link href="style.css" rel="stylesheet" type="text/css" />';
      } 
    
    ?>
    as u can see I changed the string to search from mozilla to MSIE. it didnt work tho. did I do sumthin wrong?
    Last edited by NineTwoZero; 01-07-2007 at 05:30 PM.

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
  •