SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create unordered list?

    Hello, I am creating an Org Chart and I cannot seem to figure out a way to have php create an unordered list in the proper format. I have been racking my head for about 2 days now and am in need for some guidance, thanks. Here is my code;

    MySQL Database

    | category_id | name | lft | rgt |

    list_view,php
    Code:
    <?php
      $localhost = "";
      $sql_username = "";
      $sql_password = "";
      $database = "";
      //connect to database
      @mysql_connect($localhost,$sql_username,$sql_password);
      mysql_select_db($database) or die("Unable to select database for some reason!");  
      //Insert into database
      $query = "SELECT node.name, (COUNT(parent.name) - 1) AS depth ". 
      			"FROM test_tbl AS node, ". 
    			"test_tbl AS parent ". 
    			"WHERE node.lft BETWEEN parent.lft AND parent.rgt ". 
    			"GROUP BY node.name ". 
    			"ORDER BY node.lft";
      $result = mysql_query($query)or die(mysql_error());
      $num = mysql_numrows($result);  
      $tab = '  ';
      $previous_depth = 0;
      $current_depth = $row['depth'];
      mysql_close();
      echo "<ul>\n";
      while($row = mysql_fetch_array($result)) {
    
    		echo $tab . "<li>" . $row['name'] . "  " . "\"" . $row['depth'] . "\"\n";
    
    		++$previous_depth;
      }
      echo "</ul>;
    ?>
    I know this code does not do much but it is all I have. I am looking for the loop to do is start with depth 0 and as the depth changes it will create a new <ul>. The problem I am having is I have no idea on how to even begin with this. Below is an example of what kind of output I am trying to achieve:

    Code:
    <ul>
      <li>name</li>
        <ul>
          <li>name</li>
          <li>name</li>
          <li>name</li>
    	  <ul>  
            <li>name</li>
            <li>name</li>
            <li>name</li>
    		<ul>
              <li>name</li>
              <li>name</li>
    		  <ul>
                <li>name</li>
                <li>name</li>
                <li>name</li>
    			</ul>
    			</ul>
    			</ul>
      <li>name</li>
      <ul>
        <li>name</li>
        <li>name</li>
        <li>name</li>
    	<ul>
          <li>name</li>
    	</ul>
    	</ul>
    	</ul>
    	</ul>  
    </ul>

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Take a look at this recent thread, the OP answered his own question, but I think he was using the same system as you are.

  3. #3
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was doing some searching and I found this script that caught my eye but, I think it has some problems. Here is the script;

    Code:
    <?php
    
      $localhost = "";
      $sql_username = "";
      $sql_password = "";
      $database = "";
      
      //connect to database
      mysql_connect($localhost,$sql_username,$sql_password);
      @mysql_select_db($database) or die("Unable to select database for some reason!");
      
      
    function getLowerRanks($id,$level)
    {
          $response = ' ';
          $result = mysql_query("SELECT * FROM table WHERE category_id=".$id)or die(mysql_error());
          while ($row = mysql_fetch_array($result))
          {
                $response .= '<tr><td>';
                for ($i = 0; $i < ($level-1); $i++) {
    	$response .= '&nbsp;&nbsp;';
                	$response .= '|-'.$row['name'];
                	$response .= '</td></tr>';
                	$response .= getLowerRanks($row['category_id'],$level+1);
                    }
    	  }
          return $response;  
    }    
    $tree = '<table cellpadding="2" cellspacing="0" border="0">';
    $result = mysql_query("SELECT * FROM table WHERE category_id = '0'");
    while ($row = mysql_fetch_array($result))
    {
          $tree .= '<tr><td>';
          $tree .= $row['name'];
          $tree .= '</td></tr>';
          $tree .= getLowerRanks($row['category_id'],2);
    }
    $tree .= '</table>';
    ?>
    It creates a blank screen so it is hard to troubleshoot the problem. From what I have found it has to do with something in the for loop in the function. Maybe if someone could take a look and see what they can find. Thanks

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have you got error reporting turned up on your dev environment?
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 

  5. #5
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies Cups, I have looked and I am running PHP Version 4.3.6. I thought this version does not handle error meseges? If you would like I can show you the output of the phpinfo();. Thanks -

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your php.ini file should not matter, save this code as test.php and load it up in a browser and tell us what it says on the page.

    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL); 

    // a line with no semicolon ending, should throw an error
    echo 'error'

    ?>

  7. #7
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL); 

    // a line with no semicolon ending, should throw an error
    echo 'error'

    ?>
    It won't because it's the last line in the PHP block.

  8. #8
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried what you suggested and it only outputs a blank screen. I will try doing some more research on the .ini and see if I can change something in there. Thanks -

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by decowski View Post
    It won't because it's the last line in the PHP block.
    *sigh*

    Can't do it when I have to ... otherwise this happens all day.

    Try this instead:

    PHP Code:
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL); 

    // a line with no semicolon ending, should throw an error
    echo 'an error'
    echo 'not an error';


    ?>

  10. #10
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I have downloaded and installed XAMPP and now my life is a little easier. Thanks again for your help


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
  •