Page 3 of 3 First 123
  • Jump to page:
    #31
  1. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by mallen
    Thanks. The field type is (INT) and the values are all "0" except for two that are the ID of "1"
    Fix that. If there's no category, then the cat_parent must be NULL. And then put the "IS NULL" in the query above back in.

    In a proper database setup with foreign keys, you wouldn't even be able to insert pseudo IDs like 0. But I guess that's another topic.



    Originally Posted by mallen
    Seems like I need to put another loop in there to print out the child categories.
    No. The rows of the result set already are the child categories.

    Execute the query in phpmyadmin. Each row represents a child category (or a root category in case it has no children).

    What you've done now is you went through the child categories and output their parents:

    html Code:
    <ul>
    	<li>
    		<h2>Main Cat 1</h2>
    		<!-- add children here -->
    	</li>
    	<li>
    		<h2>Main Cat 2</h2>
    		<!-- add children here -->
    	</li>
    	...
    </ul>

    Now you need to output the children themselves as well.

    To keep it simple for now, output an <ul> after the h2 and add another if statement in the loop after the if statement for the main categories:

    PHP Code:
    if ($category['child_cat_id'])
        echo 
    $category['child_cat_name']; 
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  2. #32
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Thanks. It didn't occur to me to change "0" to NULL in the database. I was getting "NULL" when I ran the query in phpmyadmin yet "0" was entered.

    I added the WHERE main_categories.cat_parent IS NULL
    and also
    if ($category['child_cat_id'])

    echo '<ul>' .$category['child_cat_name']. '</ul>';

    But it will only show 1 category as a sub category. Not two.

    Such as:
    Fruit-
    >Apples

    Not:
    Fruit-
    >Apples
    >Banannas
  4. #33
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    An HTML list looks like this:

    Code:
    <ul>
    	<li>Entry 1</li>
    	<li>Entry 2</li>
    	...
    </ul>
    You somehow only have a single <ul></ul>.

    Apart from that: What does the query result look like? What does the full code (with the correct list) look like?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #34
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    PHP Code:
            global $wpdb;
            
    $query "      SELECT 
            main_categories.cat_id AS main_cat_id
            , main_categories.cat_name AS main_cat_name
            , child_categories.cat_id AS child_cat_id
            , child_categories.cat_name AS child_cat_name
        FROM
            my_categories AS main_categories
            LEFT JOIN my_categories AS child_categories
                ON child_categories.cat_parent = main_categories.cat_id
       WHERE main_categories.cat_parent IS NULL
        ORDER BY
            main_categories.cat_id
        

                
        "

            echo 
    '<ul>';
            
    $is_first_cat true;
    $previous_main_cat '';
    $categories $wpdb->get_results($wpdb->prepare($query), ARRAY_A);

    foreach (
    $categories as $category) {

        
    // do we have a new main category?
        
    if ($is_first_cat || $category['main_cat_id'] != $previous_main_cat) {
            
    // unless it's the first main category, terminate the previous main category
            
    if (!$is_first_cat)
                echo 
    '</li>';
            else
                
    $is_first_cat false;
            
    // display heading
            
    echo '
                
                    <li>' 
    htmlspecialchars($category['main_cat_name'], ENT_QUOTES ENT_XHTML'UTF-8') .'</li>';
            
            
    $previous_main_cat $category['main_cat_id'];
            if (
    $category['child_cat_id'])
             
         echo 
    '<ul>' .$category['child_cat_name']. '</ul>' 
            
        

        }
    }
    if (!
    $is_first_cat)
        echo 
    '</li>';
          
    echo 
    '</ul>';  
        } 
    Code:
    cat_id  | cat_name | cat_id | cat_name
    --------+----------+--------+---------
    1       | Fruit    | 2      | Apples
    --------------------------------------
    1       | Fruit    | 3      | Oranges
    --------------------------------------
    4       | Colors   | Null   | Null
    --------------------------------------
    5       | Shapes   | Null   | Null
    --------------------------------------
    6       | Animals  | Null   | Null
    --------------------------------------
    Code:
    <ul>
                
                    <li>Fruit</li><ul>Apples</ul>
                    <li>Colors</li>
                    <li>Shapes</li>
                    <li>Animals</li>
                   </ul>
  8. #35
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Your "if" statement checking if there's a child category is in the "if" statement for the headings. But it has to come after that. Otherwise, you only output a child category if the main category has changed (which only applies to the very first child category).

    Check the logic again:

    1. We fetch all child categories together with their main category plus the main categories with no children.
    2. We loop through those categories.
    3. Whenever the main category changes, we display the heading.
    4. Whenever we have a child category (as opposed to a main category with no children), we display it as a sublist element.

    Now imagine going through the result set above:

    • First row: New main category? Yes, display the heading "Fruit". Child category? Yes, display "Apples" as a list element.
    • Second row: New main category? No, we already had "Fruit". Child category? Yes, display "Oranges".
    • Third row: New main category? Yes, display a "Colors" heading. Child category? No.
    • The same with the next two rows.

    And like I already said above, an HTML list consists of an ul or ol element with li elements as the list entries. You only have a single ul element. That will mess up the formatting regardless of the logical errors.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #36
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Thanks. I moved the if statement and now I am getting all the correct results under each heading. I do know how to write an unordered list. But I can't get the output to include each tag in the correct order. Also I do have more than one UL tag in the above code example.
  12. #37
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Still no luck getting it to format correctly.
  14. #38
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Ok is been over a month since I last posted here. Thought this was a good time to revisit this issue. Still can't get the correct format of the list.
  16. #39
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Getting this as a result
    Code:
    <ul>
      <li>Fruits</li>
       	<ul>apples</ul>
      <li>Colors</li>
      <li>Shapes</li>
      <li>Animals</li>
    </ul>
    But need this instead
    Code:
    <ul>
    	<li>Fruits
       <ul>
       <li>Apples</li>
       </ul>
    </li>
    <li>Colors</li>
    <li>Shapes</li>
    <li>Animals</li>
    </ul>
  18. #40
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    Well I am back. I had it all working and accidentally saved over my page and have to recreate it. I got the categories to list but it lists the sub category and then lists the sub category again as a main category and then continues.

    Fruits
    >apples
    Apples
    Colors
    Shapes
    Animals

    PHP Code:
     $query "      SELECT 
            main_categories.cat_id AS main_cat_id
            , main_categories.cat_name AS main_cat_name
            , child_categories.cat_id AS child_cat_id
            , child_categories.cat_name AS child_cat_name
        FROM
            categories AS main_categories
            LEFT JOIN categories AS child_categories
                ON child_categories.cat_parent = main_categories.cat_id
       WHERE main_categories.cat_parent IS NOT NULL
     
       
       
        ORDER BY
            main_categories.cat_id"

            echo 
    '<ul>';
        
            
    $is_first_cat true;
    $previous_main_cat '';
    $categories $wpdb->get_results($queryARRAY_A);

    foreach (
    $categories as $category) {

        
    // do we have a new main category?
        
    if ($is_first_cat || $category['main_cat_id'] != $previous_main_cat) {
            
    // unless it's the first main category, terminate the previous main category
            
    if (!$is_first_cat)
                echo 
    '</li>';
            else
                
    $is_first_cat false;
                
    // display heading
                
    echo '<li><a href=?page_id=' $this->curCompanyPage'&category=' .$category['main_cat_id']. '>'$category['main_cat_name'].'</a>'.'</li>';
                
              
    $previous_main_cat $category['main_cat_id'];
             
       
           }
      
       
          if (
    $category['child_cat_id']) 
              
    // display sub heading
          
    echo '<ul><a href=?page_id=' $this->curCompanyPage'&category='.$category['child_cat_id']. '>'.$category['child_cat_name']. '</a>'.'</ul>'

         
       
     
        }
    if (!
    $is_first_cat)
        echo 
    '</li>';   
     
          
    echo 
    '</ul>';  
           

     } 
  20. #41
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    392
    Rep Power
    76
    I was able to retrieve my previous code. Then ran it and was still having the same problem. I had the database set up incorrect and had to update the cat_parent fields to NULL. Working now.

    PHP Code:
    function displayCategories()
        {
            global 
    $wpdb;
            
    $query " SELECT 
            main_categories.cat_id AS main_cat_id
            , main_categories.cat_name AS main_cat_name
            , child_categories.cat_id AS child_cat_id
            , child_categories.cat_name AS child_cat_name
        FROM
            categories AS main_categories
            LEFT JOIN categories AS child_categories
            ON child_categories.cat_parent = main_categories.cat_id
            WHERE main_categories.cat_parent IS NULL
            ORDER BY
            main_categories.cat_name ASC"

            echo 
    '<ul>';
            
    $is_first_cat true;
    $previous_main_cat '';
    $previous_subcat '';

    $categories $wpdb->get_results($queryARRAY_A);

    foreach (
    $categories as $category) {
       
        if (
    $is_first_cat || $category['main_cat_id'] != $previous_main_cat) {
           
            if (!
    $is_first_cat)
                echo 
    '</li>';
          
                
    $is_first_cat false;
                 
        if(
    $_REQUEST['category'] == $category['main_cat_id'])     {
            
    echo 
    '<li><a href=\'?page_id=' $this->curCompanyPage .'&category='.  $category['main_cat_id'] .'\''.'class="cur_category"' .'>'$category['main_cat_name'] .'</a>'.'</li>'."\n";
        
            
    $previous_main_cat $category['main_cat_id'];}
        
        else    echo 
    '<li><a href=\'?page_id=' $this->curCompanyPage .'&category='.  $category['main_cat_id'] . '\'' .'>'$category['main_cat_name'] .'</a>'.'</li>'."\n";
        
            
    $previous_main_cat $category['main_cat_id'];
        
        
        }
        
        
        if (
    $category['child_cat_id'])     
         if(
    $_REQUEST['category'] == $category['child_cat_id'])  {    
        
        echo 
    '<ul><a href=\'?page_id=' $this->curCompanyPage .'&category='.  $category['child_cat_id']  . '\'' .'class="cur_category"' .'>'$category['child_cat_name'] .'</a>'.'</ul>'."\n";}
        else echo 
    '<ul><a href=\'?page_id=' $this->curCompanyPage .'&category='.  $category['child_cat_id']  . '\''  .'>'$category['child_cat_name'] .'</a>'.'</ul>'."\n";
         
    }


    if (!
    $is_first_cat)
        echo 
    '</li>';
          
    echo 
    '</ul>';  
        } 
  22. #42
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    11
    Rep Power
    0

    concept of cookies and session


    can someone make me clear the concept of cookies and session, as it was asked by me in interview and i was anable to answer that
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo