Page 1 of 1

Confused by my own logic...

Posted: Thu Feb 23, 2012 6:53 pm
by marcushjortsberg
I make a query where I fetch som individuals and group them by class and count the rows.

I make a for statement and in that statement I make a new query where I fetch all individuals in each class and sort them by id.

Each member in each class should be printed into a file, limited by 15, if there is more than 15 the rest should be printed to the next file.

My logic is a little confusing to me...

Code: Select all

$queryorder = "SELECT *
					  FROM mytable
					  
					  GROUP BY class ORDER BY cat_order";
				count $rows1...
				

				
				for ($ii = 0; $ii < $rows1; $ii++)

				{						
					$query = "SELECT *
					FROM mytable
					WHERE class  = '". $rows1[$ii]['class'] . "' 
					ORDER BY id";
					$rows2
					
					$count = 1;
					$sid_nr = 1;
					$list_nr = 1;
					
					for ($iii = 0; $iii < $rows2; $iii++)

					{
						if($count == 1)
						{
							createnewfile
					
							
							$ring = $rows2[$iii]['ring'];
						}
																														
						for($count_i = 1; $count_i < 16; $count_i++):
												
							if($rows2[$iii]['C'] == 1){$rows2[$iii]['C'] = 'C';}else{$rows2[$iii]['C'] = '';}
							if($rows2[$iii]['S'] == 1){$rows2[$iii]['S'] = 'S';}else{$rows2[$iii]['S'] = '';}

							if($count_i == $count)
							{
								variabel1='VAR1'.$count_i, $rows2[$iii]['nr']);
								variabel2='CS'.$count_i, $rows2[$iii]['C'] . $rows2[$iii]['S']);
								variabel3='NAME'.$count_i, $rows2[$iii]['id_nr'] . ' ' . $rows2[$iii]['name']);
							}
							
						endfor;
						
						$count++;
																																				
						if($count == 15)
						{						
							$filename = somefilename;
							$myFile = $resultiddir.DS.$filename;
							closefile
							
							$count = 1;
							$sid_nr++;

						}
						
						if($count == $rows2
						{						
							$filename = somefilename;
							$myFile = $resultiddir.DS.$filename;
							closefile
							
							$count = 1;
							$sid_nr = 1;

							
						}
					}
				}
Some of it is pseudocode but the idea is the important thing:)

Re: Confused by my own logic...

Posted: Thu Feb 23, 2012 7:16 pm
by Eric!
Sounds like pagination. Why don't you just run through a loop of all the data and count each line. Each time you hit a number divisible by 15, start a new file.

I probably missed something with your problem, but I don't understand why you need to do the first two queries when you just want to sort all the individuals by class. Just do that query ordered by class. Whenever the class changes or if $count%15 == 0, start a new file.

Code: Select all

$results= //array with all students ordered by class
$count=0;
$lastclass="";
foreach($results as $row)
{
    if (strcasecmp($lastclass, $row['class'])!=0)
    {  
         // new class
         $lastclass=$row['class']; // update lastclass value
         //close old file and start new file for new class;
         $count=0;
   }
   else if($count>0 && $count%15==0)
   {
         //more than 15 ... close old file and start new file for overflow
         $count=0;
   }
   //write data to file and keep looping
   $count++;
}

Re: Confused by my own logic...

Posted: Fri Feb 24, 2012 2:41 am
by marcushjortsberg
The reason for the two queries is that when I learned php I made this logic and it works as long I don't have to break it like every 15th individual...

I know there is better ways and I am learning some:)

But I have one question, what happens here?

Code: Select all

if (strcasecmp($lastclass, $row['class'])!=0)

Re: Confused by my own logic...

Posted: Fri Feb 24, 2012 6:43 am
by Celauran
Binary safe case-insensitive string compare. Handy thing, that PHP manual.