Database Articles
  Home arrow Database Articles arrow Page 2 - Alternating row colors with PHP and my...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
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 / 5
    2002-03-02

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · at least if you use stylesheets (all people should do so).in this case, you can...
       · hehehe...matts idea is even better.coz u see,ive been using this code to...
       · There are many ways of performing this simple - yet, always questioned...
       · the way i did it lookes something like this$x = 0;loophere {$x +=...
       · Save a little more coding...I create a variable called $f and $F....
       · Try this:While iterating through your WHILE loopto build the table, place this...
       · bagus thanks skali yaaa,... (ga ngerti indo ya.. kacian dehh yee hakwakwakwa!!)
       · You don't have to use abs-function.Why don't you do like this?$line=2;...
     

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT