Jump to content

Nested for loop problem


zrodimel

Recommended Posts

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
        // 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

Link to comment
Share on other sites

Bad boy. If you're going to use nested loops, don't use the same variable names. The value that you set for $num_results in the second loop affects the number of iterations of the outer loop (which would be better off as a while loop, actually). Also, you assign the mysql result of the inner loop to the same variable as the outer loop. Same thing with $row, but that doesn't affect anything since you don't use it after starting another loop.

 

Use different variables for each inner loop.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.