Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dynamic Table with Columns

    I am generating a dynamic table and everything works fine except I have the columns hard coded. I need to somehow determine if a column exists in the sense that there is a value in it other than NULL and then change the code below to generate the columns dynamically.

    Here is my current code:
    PHP Code:
    <table border=0 align=center cellpadding=5>
    <tr>
    <td><b>Land</b></td>
    <td><b>Acres</b></td>
    <td><b>$/Acre</b></td>
    <td><b>$/Mile</b></td>
    <td><b>Survey</b></td>
    <td><b>Township</b></td>
    <td><b>Range</b></td>
    <td><b>Section</b></td>
    <td><b>Block</b></td>
    </tr>
    <tr>
    <td colspan=11>
    <hr>
    </td>
    </tr>
    <tr>
    <td>
    <?
    foreach($land_category as $item)

    {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
      }
    ?>
    </td>
    <td>
    <?
      
    foreach($acres as $item)
      {
          if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($price_per_acre as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($price_per_linear_mile as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($survey as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($township as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($range as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($section as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    <td>
    <?
      
    foreach($block as $item)
       {
        if (
    $item != NULL)
          {
          print 
    $item.'<br>';
          }
       }
    ?>
    </td>
    </tr>
    The problem again, is that I don't want to display the column title (e.g., Acres) if no value is in the database. Can anyone offer some advice?

  • #2
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Thumbs up

    Not mine got it from here:
    http://www.codewalkers.com/c/a/Datab...repeat-region/
    Hope this helps you.
    PHP Code:
    <?php
    // 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
    ?>
    Thanks for you support!

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much.

    I am studying how this works, and I am curious about the

    PHP Code:
     $num_cols 3// the number of columns 
    This code may be exactly what I need, but I don't think I quite understand. The table I am working with has maybe 15 columns. If I SELECT * FROM tbl
    but then am only using a maximum of 8 of those columns, how can I somehow figure out to use the column. I suppose the test is if a value exists in one of the rows of the column.

    Please comment if you can on this.

  • #4
    Regular Coder westmatrix99's Avatar
    Join Date
    Dec 2006
    Location
    South Africa
    Posts
    307
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Maybe it's the amount of columns, I tried it and it did not work for my horizontal loop that I needed.
    Change the number to 15 and see what happens.

    Cheers
    West
    Thanks for you support!

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for replying. I will try that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •