Database Code

  Home arrow Database Code arrow Nested repeat region

Nested repeat region
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1

    Table of Contents:



    Presenting data into rows or columns is a standard action realized with a simple loop through your record set. Often the data have to be presented in a different order. This example shows a table where the results are provided into columns and rows. Each row holds a number of records from your database; if the number of columns is reached a new row will be used. If there are no more records empty cells will be created and the dynamic creation of cells and rows is ended after the last row has reached the maximum number of columns. Nested repeat example page

    By : olaf

    // this is an example query from the link page on my website, use your own data and don't forget to change the names of the results inside the table below
    $query = "SELECT title, descr, link FROM linksite ORDER BY vote DESC";
    $result = mysql_query($query);
    $total_records = mysql_num_rows($result); // the number of records in your result set

    $num_cols = 3; // the number of columns
    $num_rows = ceil($total_records / $num_cols); // the number of rows
    $num = 0; // don't change this value, this is the first number of each record inside a record set

    echo "<table>\n";
    // next the loop for the table rows
    for ($rows = 0; $rows < $num_rows; $rows++) {
    echo "<tr>\n";
    // this is the loop for the table columns
    for ($cols = 0; $cols < $num_cols; $cols++) {
    if ($num < $total_records) { // show records if available (reduce by one because the first record is no. "0" (zero)
    // first create variables with the values of the current record
    $titel = mysql_result($result, $num, "title"); // you have to chenge the names here to fit your own sql statement
    $description = mysql_result($result, $num, "descr");
    $url = mysql_result($result, $num, "link");
    echo "<td><b>".$titel."</b><br>".$description."<i><a href=\"".$url."\">Visit here!</a></i><td>\n";
    } else { // show an empty cell
    echo "<td>&nbsp;</td>\n";
    $num++; // raise the number by one for the next record
    echo "</tr>\n"; // there are no more cols in this row, close the table row tag
    echo "</table>\n"; // end of the region = closing tag for the table element
    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.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus


    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser

    Developer Shed Affiliates


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