Jump to content

pirnting a list - problem


thara

Recommended Posts

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

 

  Quote
+--------+--------------+-------------+------------+

| 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

Link to comment
https://forums.phpfreaks.com/topic/263170-pirnting-a-list-problem/
Share on other sites

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'];
}

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

 

  Quote
An error occurred in script 'C:\wamp\www\lanka_institute\profiles\centers\index.php' on line 120: Undefined variable: previous_category_name

 

 

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.