Jump to content

Bit of code that isnt quite right


deansaddigh

Recommended Posts

I have this page http://www.languageschoolsuk.com/quicksearchresults.php

 

Which if you choose southwest and Adult english programe on the search at the top of the page.

 

Will bring the results i want you to check. it shows Course for : KINGS twice.

However i have written an if statement that should only ever show The school name once but its respective courses however many times they appear.

 

 // Initialise temp variable
         $previousschoolname = "";
         
            while($row = mysql_fetch_array($result, MYSQL_ASSOC))
            {
		//get school id
	  	$school_id = $row['school_id'];
            // Get the school name
            $schoolname = $row['school_name'];
                       
            // If the previous school name is equal to the new name then do not print
            // it out as we only want the same school name printed once
            if ( $schoolname != $previousschoolname )
            {
		  
		   	
		   echo '<br />';
		   echo  '<h2 class ="schoolname">Course for :  <a href="school_details.php?id='.$school_id.'">'.$schoolname.'</h2></a>';
             
            }
                echo '<p>' . "\r\n";
                echo 'Course name: ' . ucfirst($row['course_name']) . '<br />'."\r\n";
                echo 'Course description: ' . $row['info'] . '<br />'."\r\n";
                echo 'Price: £' . number_format($row['price'], 2) . '<br />'."\r\n";
                echo 'Duration: ' . $row['duration'] . ' weeks<br />'."\r\n";
                echo 'Difficulty: ' . ucfirst($row['level']) . '<br />'."\r\n";
                echo '</p><hr />' . "\r\n";
            // Make temp variable equal the previous school so can be used in check
            $previousschoolname = $schoolname;
            }
                              
            		
				if (mysql_num_rows($result) == 0)
				{
					echo '<img src="images/no-results.png" class="floatleft" WIDTH=50 HEIGHT=50 alt="No Results" />';
					echo '<p class="important">We are sorry no results found! Please try other search criteria</p>';
				}


			?>

 

Can anyone help find out why its printing the school name out twice.

I hope i have made some sort of sense, i am useless at explaining problems

 

I also forgot to mention if you go directly to that page it will error out ill fix that later

Link to comment
https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/
Share on other sites

Here's a few things to consider off the top of my head.

 

Can $result contain more than 1 school (and if it can, are you sure that like schools are grouped together?)? If it can, and the schools aren't grouped, then anytime a new school is used any previously used schools will have their header repeated.

 

Is this code used in a loop (or called from a loop)? If so, any school that appears in both $result variables will be repeated.

It's because you're checking if the new school name is the same as what was just used--but you have a school in between, so as far as the code is concerned, it's not the same as what was just used.

 

Are you sorting your results by school ASC when you pull the query from the DB?

Yeah its wrong. it will work if only one school is brought back but this piece of code

// Initialise temp variable
         $previousschoolname = "";
         
            while($row = mysql_fetch_array($result, MYSQL_ASSOC))
            {
		//get school id
	  	$school_id = $row['school_id'];
            // Get the school name
            $schoolname = $row['school_name'];
                       
            // If the previous school name is equal to the new name then do not print
            // it out as we only want the same school name printed once
            if ( $schoolname != $previousschoolname )
            {

 

wil check if the previous school was the same as the new school school thats being pulled from db. but as you can see a test school called  george  gets  called then kings again, so it will print kings again, if that makes sense, do you know how i can rectify it, basically i want it so that it gets a school name once prints all the courses related to that school then prints the next schools name  and its respective courses

It's because you're checking if the new school name is the same as what was just used--but you have a school in between, so as far as the code is concerned, it's not the same as what was just used.

 

No would that fix it do you think?

 

Are you sorting your results by school ASC when you pull the query from the DB?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.