Database Articles

  Home arrow Database Articles arrow Page 2 - Alternating row colors with PHP and my...
DATABASE ARTICLES

Alternating row colors with PHP and mySQL
By: Matt Wade
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 7
    2002-03-02

    Table of Contents:
  • Alternating row colors with PHP and mySQL
  • Let's Do It

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Alternating row colors with PHP and mySQL - Let's Do It


    (Page 2 of 2 )

    First off, let's get some data from our database. Let's assume we have a table called mytable with 3 fields: name, phonenumber, and age.

    <?php
    $db 
    mysql_connect("host","username","password") or die("Problem connecting");
    mysql_select_db("dbname") or die("Problem selecting database");
    $query "SELECT * FROM mytable ORDER BY name";
    $result mysql_query($query) or die ("Query failed");
    //let's get the number of rows in our result so we can use it in a for loop
    $numofrows mysql_num_rows($result);
    ?>

    Ok, we have our data from our database. Now we just need to open up our table and run a for loop for as many rows as we have and display the data. Then, we close the table.

    <?php
    echo "&lt;TABLE BORDER=\"1\"&gt;\n";
    echo 
    "&lt;TR bgcolor=\"lightblue\"&gt;&lt;TD&gt;Name&lt;/TD&gt;&lt;TD&gt;Phone&lt;/TD&gt;&lt;TD&gt;Age&lt;/TD&gt;&lt;/TR&gt;\n";
    for(
    $i 0$i &lt$numofrows$i++) {
        
    $row mysql_fetch_array($result); //get a row from our result set
        
    if($i 2) { //this means if there is a remainder
            
    echo "&lt;TR bgcolor=\"yellow\"&gt;\n";
        } else { 
    //if there isn't a remainder we will do the else
            
    echo "&lt;TR bgcolor=\"white\"&gt;\n";
        }
        echo 
    "&lt;TD&gt;".$row['name']."&lt;/TD&gt;&lt;TD&gt;".$row['phonenumber']."&lt;/TD&gt;&lt;TD&gt;".$row['age']."&lt;/TD&gt;\n";
        echo 
    "&lt;/TR&gt;\n";
    }
    //now let's close the table and be done with it
    echo "&lt;/TABLE&gt;\n";
    ?>

    That's it. Really. That's all there is to it. It's so simple that it is amazing. Now, get out there and fancy your tables up!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual
    - Using Oracle SQL Developer with SQL Statemen...
    - Fixing Errors with Oracle SQL Developer


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap