CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Nested for loop problem

    Hi, first of all thanks in advance for the help. I'm a newbie college student and am having a little problem with a project for a class that I can't figure out.

    I'm trying to construct a loop that creates an array for a javascript image gallery. First I call for the categories and I want to then pull all the images from that category into the array and then loop through all the categories in the database. The code below just cycles through the first category and then quits after pulling the images from that category.

    PHP Code:
            <?php
            
    // SQL Query for Categories
                
    $sql="SELECT CategoryID, CategoryName FROM pro2category";
                
    $result mysql_query($sql);
                
                if(empty(
    $result))
                {
                    
    $num_results 0;
                }
                else
                {
                    
    $num_results mysql_num_rows($result);
                }
            
            
    ?>
            <?php                                
            
    for($i=0$i<$num_results$i++)
            {
                
    $row mysql_fetch_array($result);
                
                
    $CategoryID $row["CategoryID"];
                
    $CategoryName $row["CategoryName"];

            
    ?>
            
            
                var <?php echo $CategoryID ?> = new Array();
                
          <!-- Images inside Array -->
          <!-- Images inside Array -->
          <!-- Images inside Array -->
          <!-- Images inside Array -->
                
                <?php
                    $sql
    ="SELECT ImageID, ImageName, ImageNumber, URL, CategoryID FROM pro2image WHERE CategoryID='$CategoryID'";
                    
    $result mysql_query($sql);
                    
                    if(empty(
    $result))
                    {
                        
    $num_results 0;
                    }
                    else
                    {
                        
    $num_results mysql_num_rows($result);
                    }
                    
                
    ?>
                
                    <?php                                
                    
    for($i=0$i<$num_results$i++)
                    {
                        
    $row mysql_fetch_array($result);
                        
                        
    $ImageID $row["ImageID"];
            
                    
    ?>
                    
                    <?php echo $CategoryID ?>Array[<?php echo $i ?>] = "http://cgtweb2.tech.purdue.edu/356/zrodimel/Project2/admin/upload/<?php echo $ImageID ?>.jpg";
                
                    <?php
                    
    }
                    
    ?>
                
                

            <?php
            
    }
            
    ?>

    thanks for the help

    Zach
    Last edited by PeejAvery; April 13th, 2011 at 11:52 AM. Reason: Added PHP tags

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Nested for loop problem

    First off, always remember that you should do your best to query a database as few times as possible. This is key for maximum performace.

    So, you really only want two queries in this instance.
    • First query to get the category names. Store this in a PHP array.
    • Second query to get all the images' data into a PHP array.


    From there, you can read the first array to determine into what category to put this data. Personally, I'd use a multidimentional array with the image categories as the first dimension index. Then, the second dimension could be all the individual image data. Here's an example.

    Code:
    array(
      "Headshot" => array(
        array(id, name, number, url),
        array(id, name, number, url)
      ),
      "Landscape" => array(
        array(id, name, number, url),
        array(id, name, number, url)
      ),
      "Strobist" => array(
        array(id, name, number, url),
        array(id, name, number, url)
      )
    )
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured