Results 1 to 3 of 3

Thread: PhP auto generate table

  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PhP auto generate table

    Hey guys,

    I have weird problem,

    Somewhere in my DB I have a table that had items like this:

    id | Name | Sub_of
    1 | A | 0
    2 | B | 1
    3 | C | 1
    4 | D | 2
    5 | E | 2

    All I am trying to do is automatically generate a table so that I can see all the sub_of like this (A,B,C... are table headers I will be writing the table row by row. e.g. <tr>...</tr> ):
    Code:

    |------------------------------------|
    |----------------A-------------------|
    --------------------------------------
    |-----------B---------- |------C-----|
    --------------------------------------
    |----D--| ----- E ------|------------|
    --------------------------------------
    |--------| --------------|------------|
    |--------| --------------|------------|
    |--------| --------------|------------|
    ...

    I have tried to get the depth of the final table header and the number of final set of columns like this :

    Code:

    function tree_span($category_id,$find) {
    $q = "SELECT category_id, name, id from ".$table_prefix."categories where sub_of = ".$category_id." order by name";
    $query = mysql_query($q);

    if (!$query) echo "Database Error : ".$q;
    else {
    $depth = mysql_num_rows($query);
    if($depth !=0) {
    $category_depth = $category_depth+1;
    }
    while ($row=mysql_fetch_row($query)) {
    if ($row[0] != $id) {
    $category_span = $category_span +1;
    tree_span($row[0],$find);
    }
    }
    }

  2. #2
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    Funny that. I was about to post needing help on how to auto generate a table in PHP. I had some more rudimentary questions.

    Code:
    <?php
      function maketable($query, $fieldarray){
    	//count number of columns
    	 $columns = count($fieldarray);
    	//run the query
    	 $result = mysql_query($query) or die(mysql_error()) ;
    	 $itemnum = mysql_num_rows($result);
    	 if($itemnum > 0){
    	  do{  
    	   echo "< tr >" ;
    	   for($x = 0; $x < $columns; $x++){
    	    echo "< td >" .$items[$fieldarray[$x]]. "< /td >" ;
    	   }
    	   echo "< /tr >" ;
    	  }while($items = mysql_fetch_assoc($result));
     }
    	}
    	
        echo "< table >";
    	$fieldarray = array("id","title","description");
    	maketable("SELECT * FROM bw_news", $fieldarray);
    	echo "< /table >";	
    	?>
    I found this code on another forum but am having trouble trying to figure out where the $query fits in.

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    I found a table that may actually work for you.

    You only need to configure the hostname, username, password, database and the query, and the code does the rest.

    Code:
    <?php
    $hostname='localhost';
    $username='username';
    $password='password';
    $db='database';
    $query_string="SELECT * FROM `table`";
    
    $global_dbh = mysql_connect($hostname, $username, $password)
    or die("Could not connect to database");
    mysql_select_db($db, $global_dbh)
    or die("Could not select database");
    function display_db_query($query_string, $connection, $header_bool, $table_params) {
    	// perform the database query
    	$result_id = mysql_query($query_string, $connection)
    	or die("display_db_query:" . mysql_error());
    	// find out the number of columns in result
    	$column_count = mysql_num_fields($result_id)
    	or die("display_db_query:" . mysql_error());
    	// Here the table attributes from the $table_params variable are added
    	print("<TABLE $table_params >\n");
    	// optionally print a bold header at top of table
    	if($header_bool) {
    		print("<TR>");
    		for($column_num = 0; $column_num < $column_count; $column_num++) {
    			$field_name = mysql_field_name($result_id, $column_num);
    			print("<TH>$field_name</TH>");
    		}
    		print("</TR>\n");
    	}
    	// print the body of the table
    	while($row = mysql_fetch_row($result_id)) {
    		print("<TR ALIGN=LEFT VALIGN=TOP>");
    		for($column_num = 0; $column_num < $column_count; $column_num++) {
    			print("<TD>$row[$column_num]</TD>\n");
    		}
    		print("</TR>\n");
    	}
    	print("</TABLE>\n"); 
    }
    
    function display_db_table($tablename, $connection, $header_bool, $table_params) {
    	$query_string = "SELECT * FROM $tablename";
    	display_db_query($query_string, $connection,
    	$header_bool, $table_params);
    }
    ?>
    <HTML><HEAD><TITLE>Displaying a MySQL table</TITLE></HEAD>
    <BODY>
    <TABLE><TR><TD>
    <?php
    //In this example the table name to be displayed is  static, but it could be taken from a form
    $table = "table1";
    
    display_db_table($table, $global_dbh,
    TRUE, "border='2'");
    ?>
    </TD></TR></TABLE></BODY></HTML>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •