SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to write this simple code

    I'm starting a fairly large script that will involve pulling a lot of data from mysql. I have a simple function started just to pull the categories down as an example. I would like to know the best way to write this code so I don't go and write a dozen similar functions and find out I should have done it another way.

    Here is what I have:
    PHP Code:
    $Catalog = new Catalog;
    class 
    Catalog
    {
    //**************************************************************************************
    //** Retrieves the fixed categories to be displayed on each page 
    function categoriesFixed()
    {
         global 
    $DB;
         
    // Query the Database
         
    $query " SELECT * 
         FROM ez_categories 
         WHERE parent_id = 0
         ORDER BY category ASC
         "
    ;  
         
    $result $DB->query($query);
         
    $num_rows mysql_num_rows($result);
         
    // Display the Results
         
    for ( $i 0$i $num_rows$i++ )
         {
              
    $row mysql_fetch_object($result);
              print 
    "<tr>\n";
              print 
    "<td width='10'><img src='assets/images/icon_folder.gif' alt='' width='16' height='16' /></td>\n";
              print 
    "<td width='150'><a href='index.php?category="$row>category_id ."'>"$row->category ."</a></td>\n";
              print 
    "</tr>\n";
         }
         } 
    I read that you shouldn't print your results through the function but rather use "return" and print it from the html page. But I'm not sure if this is true or how to do this.

    This is what I have in my main page to display the results:
    HTML Code:
    <table width="160"  border="0" cellpadding="0" cellspacing="0" class="box">
    <tr>
    <th>Categories</th>
    </tr>
    <?php echo $Catalog->categoriesFixed();?>
    </table>
    Can you advise me on how to make this code any better? Thanks!

  2. #2
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would consider creating a class that returns the database result in an array. Then iterate over the result for display purposes.

    For example:
    PHP Code:
    <?php
    class CategoryFinder {
        var 
    $connection;

        function 
    CategoryFinder(&$connection) {
            
    $this->connection =& $connection;
        }

        function &
    findAll() {
            
    $sql "SELECT * FROM category ORDER BY name";
            
    $result =& $this->connection->query($sql);

            if (
    DB::isError($result)) {
                die(
    $result->getMessage());
            }

            
    $resultArray = array();

            
    // put the database result into an array
            
    while ($row =& $result->fetchRow(DB_FETCHMODE_ASSOC)) {
                
    $resultArray[] = $row;
            }

            
    $result->free();

            return 
    $resultArray;
        }
    }

    $finder =& new CategoryFinder($connection);
    $rows =& $finder->findAll();
    ?>
    <table>
    <?php foreach ($rows as $row): ?>
      <tr>
        <td><?php echo $row['name']; ?></td>
      </tr>
    <?php endforeach; ?>
    </table>
    Thanks,

    JT

  3. #3
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That makes sense. I'm trying to work that method into my own code. This is what I have so far.
    PHP Code:
    function &categoriesFixed()
     {
      global 
    $DB;
      
    $sql" SELECT * FROM ez_categories WHERE parent_id = 0 ORDER BY Category ASC";
      
    $result $DB->query($sql);

      
    $resultArray = array();
      while (
    $row mysql_fetch_object($result)) 
      { 
           
    $resultArray[] = $row
      } 
      return 
    $resultArray;

    PHP Code:
    <?php
    $rows 
    =& $Catalog->categoriesFixed();
    ?>
    <?php 
    foreach ($rows as $row): ?> 
         <tr>
              <td width="10"><img src="assets/images/icon_folder.gif" alt="" width="16" height="16" /></td>
              <td width="150%"><?php echo $row['category']; ?></td>
         </tr>
    <?php endforeach; ?>
    I'm too tired to mess with it more tonight, but I thought I would post what I have to see if I'm on the right track. It seems to be displaying the correct number of rows but it doesn't show the category name. My column name is "category" that I'm trying to display.

    By the way, what does the & do? For example: function &categoriesFixed()

    Thanks for your help so far.

  4. #4
    SitePoint Evangelist
    Join Date
    Dec 2003
    Location
    Arizona
    Posts
    411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The "&" symbol tells PHP to return a reference to the result array instead of returning a copy of the array. HarryF has a a good article on the subject:

    http://www.phppatterns.com/index.php...leview/23/1/2/

    Thanks,

    JT

  5. #5
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what am I missing in my code? I can't get it to display the categories in the html. It shows the rows but not the categories.

    Also, is using arrays good for a larger number of results and can it be setup with a result pager?

  6. #6
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    print "<td width='150'><a href='index.php?category="$row>category_id ."'>"$row->category ."</a></td>\n"
    Missing a '-' there me thinks.

    I would write this as follows:

    PHP Code:
    function &categoriesFixed()
    {
      global 
    $DB;
      
      
    $sql"SELECT * FROM ez_categories WHERE parent_id = 0 ORDER BY Category ASC";
      
    $result $DB->query($sql);

      
    $this->content .= $SKIN->skinOfTHisPage->startTableCategories(); 
      
      while (
    $row mysql_fetch_object($result)) 
      { 
           
    $this->content .= $SKIN->skinOfTHisPage->compileCategoryRow$row['id'], $row['name'] ); 
      } 
      
      
    $this->content .= $SKIN->skinOfTHisPage->endtablecategories(); 



    ...


    print( 
    $this->content ); 
    Now feel free to have some nice functions that get you the markup.

  7. #7
    SitePoint Addict Eric1776's Avatar
    Join Date
    May 2002
    Location
    Atlanta, GA
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Missing a '-' there me thinks
    I was talking about this code. I'm trying to keep it simple, where I can just create the queries and drop it into place in the HTML.

    PHP Code:
    function &categoriesFixed()
    {
    global 
    $DB;
    $sql" SELECT * FROM ez_categories WHERE parent_id = 0 ORDER BY Category ASC";
    $result $DB->query($sql);
     
    $resultArray = array();
    while (
    $row mysql_fetch_object($result)) 

         
    $resultArray[] = $row

    return 
    $resultArray;

    PHP Code:
    <?php
    $rows 
    =& $Catalog->categoriesFixed();
    ?>
    <?php 
    foreach ($rows as $row): ?> 
         <tr>
             <td width="10"><img src="assets/images/icon_folder.gif" alt="" width="16" height="16" /></td>
             <td width="150%"><?php echo $row['category']; ?></td>
         </tr>
    <?php endforeach; ?>


Bookmarks

Posting Permissions

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