thara Posted May 26, 2012 Share Posted May 26, 2012 Hello... I am some confuse with this matter. I want to print a order list with a title in php. those come from my database. Title is my category name and order list should be my subjects. I used 'institute_category_subject' table and i contain all institutes, categories and their subjects. something like this... I now need to create my list with these values. It is something like this.. +--------+--------------+-------------+------------+ | ics_id | institute_id | category_id | subject_id | +--------+--------------+-------------+------------+ | 1 | 1 | 1 | 2 | | 2 | 1 | 1 | 4 | | 3 | 1 | 2 | 2 | | 4 | 1 | 2 | 3 | | 5 | 1 | 3 | 1 | | 6 | 1 | 3 | 4 | +--------+--------------+-------------+------------+ category_name1 sub1 sub2 sub3 category_name2 sub1 sub2 sub3 and so on.... My problem is category id with same value going on many rows in the table and when i retrieve that id and when try to print, same category name printing repeatedly. I need to print my category name at only one time as my title and then need to print their subjects as a order list. any comments are highly appreciated. thank you.. thara is online now Report Post Edit/Delete Message Quote Link to comment https://forums.phpfreaks.com/topic/263170-pirnting-a-list-problem/ Share on other sites More sharing options...
.josh Posted May 26, 2012 Share Posted May 26, 2012 compare the current category name with the previous category name and only output if it changes... // example loop while ($row = mysql_fetch_row($result)) { if ($row['category_name'] != $previous_category_name) { // echo category name } // echo sub stuff $previous_category_name = $row['category_name']; } Quote Link to comment https://forums.phpfreaks.com/topic/263170-pirnting-a-list-problem/#findComment-1348788 Share on other sites More sharing options...
thara Posted May 26, 2012 Author Share Posted May 26, 2012 thanks for response.. I tried like that... <?php require_once ('../../includes/config.inc.php'); require_once( MYSQL1 ); $q = "SELECT category_name FROM category INNER JOIN institute_category_subject ON institute_category_subject.category_id = category.category_id WHERE institute_category_subject.institute_id = $instituteId"; while ( $row = @mysqli_fetch_array ( $r, MYSQLI_ASSOC ) ) { if ( $row['category_name'] != $previous_category_name ) { echo '<h5>' . $row['category_name'] . '</h5>'; } $previous_category_name = $row['category_name']; } ?> then I got this error message.... An error occurred in script 'C:\wamp\www\lanka_institute\profiles\centers\index.php' on line 120: Undefined variable: previous_category_name Quote Link to comment https://forums.phpfreaks.com/topic/263170-pirnting-a-list-problem/#findComment-1348793 Share on other sites More sharing options...
.josh Posted May 26, 2012 Share Posted May 26, 2012 so initialize $previous_category_name before the loop...error tells you what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/263170-pirnting-a-list-problem/#findComment-1348831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.