Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a for loop problem 1

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I'm building a gallery script and I'm having trouble with it. The problem section of the script uses 2 tables from the database : gall_sections, and gall_albums. The idea being that user named albums of photos are to be stored in user named sections.

Each row of the gall_sections table has a unique number in the 'sec_id' coloumn. There is also a 'sec_id' coloumn in gall_albums which identifies each of those albums with the section that it is in.

The result of the script ( after a lot more code) is that the albums are returned to the browser as groups of javascript arrays. One array for each section.

I have a for loop that loops through the gall_sections results that is working as I want it to , and a for loop within that loop that loops through the gall_albums results which is not working properly.



Code:
[COLOR=red yellow]//get all the results from gall_sections[/color]
$secquery="select * from gall_sections
            order by sec_id";
            
			
 $secresult=mysql_query($secquery);
 $numrows1=mysql_num_rows($secresult);
 
 
[COLOR=red yellow]// If there are sections defined:[/color]
 if(!empty($numrows1))
 {
 

[COLOR=red yellow]//get all the results from gall_albums[/color]

$getalbs_query="select * from gall_albums";

$getalbs_result=mysql_query($getalbs_query);

$albs_numrows=mysql_num_rows($getalbs_result);

[COLOR=red yellow]//echo the start point of the javascript:[/color]
echo '<script>';


[COLOR=red yellow]//The first loop starts here, works as it should:[/color]
 for($e=0;$e<$numrows1;$e++)
 {
  $row=mysql_fetch_array($secresult);
  $secvar=$row['sec_id'];
  echo 'var section'.$secvar.'=array(\'';


  
[COLOR=red yellow]//second loop  
//Each section will return the variable $numbalb appended
//with that sections 'sec_id' number. The variable should
//equal the number of albums in that section[/color]

    for($b=0;$b<$albs_numrows;$b++)
     {
      $row2=mysql_fetch_array($getalbs_result);
	  $albvar=$row2['sec_id'];

[COLOR=red yellow]//if the sec_id in gall_albums and gall_sections is the same[/color]
	    if($albvar = $secvar)
	     {
[COLOR=red yellow]//if this variable isn't set then create it and give it the value 0[/color]
	      if(!isset(${numalb.$secvar}))
		   {
		    ${numalb.$secvar}=0;
           }
[COLOR=red yellow]//add one to the variable[/color]
		   ${numalb.$secvar}+=1;
	     }
      }
[COLOR=red yellow]//reset the gall_albums result array pointer back to the start of the array [/color] 
	  mysql_data_seek($getalbs_result,0);
   }
}

?>
I'll post up the code without all the comments if it's any help.
At the moment there are 3 sections, and 3 albums with 1 album in each section.
What I'm looking for is for the loop to return the number of albums in each section.
$numalb1=1
$numalb2=1
$numalb3=1

At the moment, at end of the cycles of the outer loop, all $numalb variables are equal to 3.

Can anyone see what I'm doing wrong here?




 
if all you want to do is list a count, then you might try a query that does that for you. Something like:
Code:
SELECT gall_sections.sec_id, count(*) as numalbs
  FROM gall_sections, gall_albums
 WHERE gall_sections.sec_id = gall_albums.sec_id
 GROUP BY gall_sections.sec_id

Then you would just have to issue one query and the results would be closer to what you're looking for.

Then you might do something like:
Code:
while ($row = mysql_fetch_array($resultSet, MYSQL_ASSOC)) {
    echo "Section " . $row['sec_id'] . " has " . $row['numalbs'] . " albums.";
}

It's usually better to let the database do the heavy lifting for you, that's why you're using one in the first place. :)

Brad Gunsalus
bardley90@hotmail.com
 
That looks great Bardley, thanks for your reply. I'll give it a try tomorrow night.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top