deansaddigh Posted February 22, 2010 Share Posted February 22, 2010 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 More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 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. Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016450 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 Thanks for your help. ill take another looksy Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016451 Share on other sites More sharing options...
jcanker Posted February 22, 2010 Share Posted February 22, 2010 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? Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016453 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 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 Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016455 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 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? Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016456 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 Ahhh yes do you think that will sort it Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016459 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 Just by doing what you said ORDER BY school_name ASC"; its fixed the problem, many thanks for your help. Just one last question is the way i coded this a way thats exceptable? Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016462 Share on other sites More sharing options...
jcanker Posted February 22, 2010 Share Posted February 22, 2010 If it works, it's acceptable It's fine. It's a clean and simple loop. Like scientific theories, the simpler possibility is ALWAYS better. Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016464 Share on other sites More sharing options...
deansaddigh Posted February 22, 2010 Author Share Posted February 22, 2010 Thanks for the help guys, im always wondering if my code is good enough and would other people do it, maybe it should be more complex etc , but like you said it works so im happy, thanks again for the help Link to comment https://forums.phpfreaks.com/topic/193005-bit-of-code-that-isnt-quite-right/#findComment-1016465 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.