PHP
  Home arrow PHP arrow Mastering WHILE Loops for PHP and MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Mastering WHILE Loops for PHP and MySQL
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-10-27


    Table of Contents:
  • Mastering WHILE Loops for PHP and MySQL
  • Using WHILE Loops as Counters
  • Generating Unique Random Numbers Using WHILE Loops
  • Constructing Trigonometric Tables Using WHILE Loops
  • Retrieving All Rows in a MySQL Table Using WHILE Loops

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Mastering WHILE Loops for PHP and MySQL
    ( Page 1 of 5 )

    Do you want to learn how to handle PHP WHILE loops? WHILE loops are one of the most powerful features as well as the easiest loop available to any PHP/MySQL developer. They enable us to shorten repetitive tasks for a highly useful application. This tutorial gives examples of WHILE loops in PHP/MySQL that beginner and novice developers can use as a quick reference for building similar loops in their applications.

    The early part of this article will focus on both basic and advanced WHILE looping techniques entirely written for PHP. The last part will be devoted to the use of WHILE Loop in displaying MySQL queries.

    There are different types of loops in PHP that suit different applications. Seeing actual examples and real applications will enable a developer to use this information to determine which loop script is similar to his intended application; thus, he can use it to write derivative programs. This marks the first step towards mastery in writing loop statements in PHP/MySQL.

    If you are interested, keep reading.

    Basics: Loop to output increasing or decreasing sequence of numbers

    This is basic; a lot of applications depend on outputting an increasing or decreasing sequence of numbers. See the examples below:

    Example #1: Output numbers from 1 to 10 in HTML

    <?php

    $i=1;

    while ($i<=10) {

    echo $i++;

    echo '<br />';

    }

    ?>

    What if you need to produce numbers starting from 51 and continuing to 202? All you need to do is customize using the format (bold):

    <?php

    $i=51;

    while ($i<=202) {

    echo $i++;

    echo '<br />';

    }

    ?>

    Example #2: Output a decreasing sequence of numbers: 10 to 1 in HTML

    A variation of this can be formulated but using $i—

    <?php

    $i=10;

    while ($i>=1) {

    echo $i--;

    echo '<br />';

    }

    ?>

    The situation is somewhat reversed from what is shown in Example 1.

    Example #3: Output even numbers in HTML:

    We even use $i++ to produce even numbers; see below. The code will produce an even number of 2, 4, 6, 8, 10.Instead of using 10 as the last number, 8 is used, since the loop will add twice.

    <?php

    $i=0;

    while ($i<=8) {

    $i=($i++)+2;

    echo $i;

    echo '<br />';

    }

    ?>

    The same principle applies to outputting odd numbers or any type of number sequences.



     
     
    >>> More PHP Articles          >>> More By Codex-M
     

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek