SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Css & Php

  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Css & Php

    Hey Folks,

    Using an external CSS file, how do I display the data outputted in the following colours:?

    Name (RED)
    Species (Blue)
    Sex (Green)

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
    }
    ?>

  2. #2
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,478
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the HTML code output look like? (CSS doens't care one iota about PHP.)

  3. #3
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Schulz View Post
    What does the HTML code output look like? (CSS doens't care one iota about PHP.)
    What do you mean by that?
    The output layout-out is like the following:

    Name
    Species
    Sex

    Name
    Species
    Sex

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","peter","abc123");
    if (!
    $con)
    {
      die(
    'Could not connect: ' mysql_error());
    }

    mysql_select_db("WB"$con);

    $result mysql_query("SELECT * FROM Birds");

    if(
    mysql_error() != || mysql_num_rows($result) == 0)
    {
      die(
    "No database, or no data");
    }

    // Wrap each in a CSS selectable element like a <div> or <span>.
    // <div> effects a line break so we don't need the <br> tags..
    while($row mysql_fetch_object($result))
    {
      echo 
    '
    <div class="name">' 
    $row->Name '</div>
    <div class="species">' 
    $row->Species '</div>
    <div class="sex">' 
    $row->Sex '<div>';
    }
    ?>
    In your css file:

    Code:
    .name {
        color: red;
    }
    .species {
        color: blue;
    }
    .species {
        color: green;
    }

  5. #5
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Trent Reimer,

    How do I link to an external CSS file, in the code?

  6. #6
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,478
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salchester View Post
    What do you mean by that?
    The output layout-out is like the following:

    Name
    Species
    Sex

    Name
    Species
    Sex
    What I meant was that CSS doesn't deal with PHP because it sends the HTML to the browser, meaning that CSS only sees the HTML code.

    In other words, what does the HTML code that your PHP script produces look like? Is it a table, a list, a series of DIVs?

    As for attaching the stylesheet, in the HEAD section of your Web page (directly below the TITLE element), just link to it, like so:
    Code:
    <link rel="stylesheet" type="text/css" href="screen.css" media="screen">
    (Add a space and a forward slash after media="screen" if using XHTML.)

  7. #7
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salchester View Post
    How do I link to an external CSS file, in the code?
    That goes in your HTML output. You can place that in the <head> section. For example, if your css file was named "site.css":

    Code:
    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="site.css">
        </head>
       <body>
        etc.
    Last edited by Trent Reimer; Jul 6, 2007 at 12:13. Reason: error

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I don't mean to sound like a prude or rude but you should really learn HTML and CSS before trying to tackle PHP.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSS & Php

    No. Not at all.

    I know HTML & CSS like the back of my hand, though when it comes to joining 3 different languages together, I'm sometimes get unsure on how all 3 interact with one another.

    I now understand that doing stuff with CSS & PHP, is done in the same way as doing stuff with CSS and HTML etc.

    Cheers for the help.

  10. #10
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Css & Php

    How do I add css to the following PHP code?

    Code:
    echo "<a href=\"./Display.php?id=" . ($start - $perpage) . "\">Previous Screen</a>";

  11. #11
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salchester View Post
    I'm sometimes get unsure on how all 3 interact with one another.

    I now understand that doing stuff with CSS & PHP, is done in the same way as doing stuff with CSS and HTML etc.
    Exactly, PHP is just used to output the HTML.*


    *It can also be used to output in other text or binary formats if you need it but the vast majority of the time it is used to output HTML.

    Quote Originally Posted by Salchester View Post
    How do I add css to the following PHP code?
    Exactly the same way you would add it to normal HTML. Whatever markup is printed or echoed from your PHP script can be used with CSS like any other HTML.

  12. #12
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Trent Reimer View Post
    Exactly, PHP is just used to output the HTML.*


    *It can also be used to output in other text or binary formats if you need it but the vast majority of the time it is used to output HTML.



    Exactly the same way you would add it to normal HTML. Whatever markup is printed or echoed from your PHP script can be used with CSS like any other HTML.
    Yes, but its different as it's got loads of other crap this time. Can you give me an example, so I have something to go by?

  13. #13
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    [q]How do I add css to the following PHP code?[/q]
    The same way you would if you were writing static html. Just remember to comment out the "

    PHP Code:
    echo "<a class=\"css class name\" href=\"./Display.php?id=" . ($start $perpage) . "\">Previous Screen</a>"
    Or

    PHP Code:
    echo "<a style=\"css syntax here\" href=\"./Display.php?id=" . ($start $perpage) . "\">Previous Screen</a>"
    Now its time to take a look over html and css though, as the information above is something of the first you should know when handling html and css


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
  •