SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use conditional after retrieving database content from url

    Hi everyone,


    I'm retrieving some info from a database and I'd like to display a different coloured background and/or image on my webpage, based on the specific value contained in the url.

    $q = "SELECT name FROM forum WHERE name = '$name'";

    For example, if the url contains the name 'Peter' then the page will load with a blue background; if the url contains the name 'Sarah' a pink background will load. A lame example but thats all I got.

    I have tried the following:

    PHP Code:
    <?php

    if (name == 'Peter') {$bgcolor=='blue';} else {$bgcolor =='pink';}

    print
    '<h1 style="background-color:"' $bgcolor '" >blah blah blah</h1>'

    ?>


    If anyone knows what I can do to get this to work, then please let me know.

    Thank you!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Did't that code work?

  3. #3
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi RedBishop

    Please try with this code:

    if ($name == 'Peter') {$bgcolor='blue';} else {$bgcolor ='pink';}
    print'<h1 style="background-color:'.$bgcolor.';" >blah blah blah</h1><br><br>';
    Younas Aamir
    Alpha Omega Templates
    http://www.alphaomegatemplates.com/

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    If, as you stated, you want the whole page to change color based on the user I would suggest adding a field in the database (for each row) that contains the color.
    Then you retrieve the color field from the record (along with the user name)

    Another way to accomplish this would be to define a CSS stylesheet for each user. Then use code like this:
    Code:
    <link rel="stylesheet" href"path_to_stylesheets/default.css" />
    <link rel="stylesheet" href="path_to_stylesheets/stylefor<? echo $name ?>.css" />
    Leveraging the "Cascading" effect of stylesheets, the default will be used if a valid 'named' stylesheet does not exist.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  5. #5
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guido2004 and others,

    thank you for your input!

    After some "struggle" I got it working with the following code:

    PHP Code:
      if ($name == 'Peter') {$bgcolor='blue';} else {$bgcolor ='red';} 
    Enjoy the day


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
  •