How to display posts of a specific category onclick of menus?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yateesh
    New Member
    • Dec 2012
    • 36

    How to display posts of a specific category onclick of menus?

    I have two tables , Post and Category .

    Post

    pid title result content cid
    1 Option1 Opt content1 2
    2 Option2 Opt content2 2
    3 Option3 Opt content3 3

    Category

    cid cname
    1 Cat 1
    2 Cat 2
    3 Cat 3

    This is my insert.php

    Code:
    mysql_connect('localhost','root','');
    
    mysql_select_db("database") or die("Unable to select database");
    
    $title=$_POST['post_title'];
    $result=$_POST['post_result'];
    $content=$_POST['post_content'];
    $sid=$_POST['cid'];
    $date=$_POST['date'];
    
    $insertquery="INSERT INTO review (post_title,post_result,post_content,cid,date)
              VALUES('$title','$result','$content',$sid,NOW())";
    
    
    $result=mysql_query($insertquery);
    
    if(! $result )
    {
      die('Could not enter data: ' . mysql_error());
    }
    else {
    
            echo "Entered data successfully\n";
            header('Location:home.php');
        }
    This is my cat.php which displays posts of particular category.

    Code:
    $link = mysql_connect("localhost","root","");
    mysql_select_db("database");
    
    $query="SELECT * FROM category WHERE cid='$cid'";
    
     $result = mysql_query($query) or die("Query to get data failed with error:  ".mysql_error());
    
    while($row = mysql_num_rows($result)) { 
    echo    //details goes here
    
    }
    This is my category menu list on home page .

    Code:
    <ul>            
    <li><a class="my-button" href="#">Cat 1</a></li>
    <li><a class="my-button" href="#">Cat 2</a></li>
    <li><a class="my-button" href="#">Cat 3</a></li>
    </ul>
    This is javascript.

    Code:
    <script>
    $(document).ready(function() {
    $('body').on('click', '.my-button', function() {
    
       $("#display").load("cat.php");
    
       });
    });
    </script>
    Everything is getting inserted to database correctly . But im not able to display the posts of specific category onclick .

    I have given the complete code . What i want is , when i click on Cat 1 or Cat 2 or Cat 3 on home page , i need to display all data from post table of the selected category in #display div .

    I am getting an error saying , cid is undefined variable .

    Do tell me anything else to be added or it would be better if you write the required code for me .

    Thank you.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    In your cat.php, you never define what $cid is supposed to be.

    Comment

    • yateesh
      New Member
      • Dec 2012
      • 36

      #3
      Okay. How do i go about that ?

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        You second snippet, the fourth line may need to be something like this:
        Code:
        $query="SELECT * FROM category WHERE cid='" . $cid . "'";

        Comment

        • yateesh
          New Member
          • Dec 2012
          • 36

          #5
          That is not working..

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That won't work because you still need to tell it what $cid is supposed to be. Basically, if I just came up to you and ask you, "What is x?" There's no way for you to answer that question unless I told you "x = 5" at some point. That's what you're trying to do with your code. You're telling the code to use $cid without you ever telling it what $cid is supposed to be.

            One way of defining $cid would be to do this:
            Code:
            $cid = 1;
            That hard codes the value 1 to the variable $cid. How you define it is dependent on your requirements. Meaning I can't help you with it unless you tell me what it's supposed to be.

            Comment

            • Anas Mosaad
              New Member
              • Jan 2013
              • 185

              #7
              Currently, you are loading the same page without identifying which category you are querying. You may add an id to each menu item and add action handler for each one to load the page with the required parameter.
              Code:
              <ul>            
              <li><a class="my-button" id="cat1" href="#">Cat 1</a></li>
              <li><a class="my-button" id="cat2" href="#">Cat 2</a></li>
              <li><a class="my-button" id="cat3" href="#">Cat 3</a></li>
              </ul>
              JavaScript handlers cat1 menu item:
              Code:
              $('body').on('click', '#cat1', function() {
                 $("#display").load("cat.php?cid=Cat%201");
                 });
              });
              You need to repeat the above code for all the categories.

              Finally read the request parameter in your cat.php page.
              Code:
              $cid = $_GET["cid"]
              One more thing, your page is vulnerable to SQL injection attack. You should encode/escape the parameters in order to close this security hole.

              Comment

              • yateesh
                New Member
                • Dec 2012
                • 36

                #8
                @Rabbit
                When i click on categories , it should take the id or category name ( not sure which one ) of the selected category and match its id with the table and retrieve the posts related to that particular category . I can't specify cid=1 or cid=2 becoz it needs to take the value of the selected category name .

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I know you can't. That was just an example to show you where you went wrong. You just need to code it to your requirements. Anyways, Anas has answered specifically to your situation.

                  Comment

                  • yateesh
                    New Member
                    • Dec 2012
                    • 36

                    #10
                    This is my updated statements in cat.php

                    Code:
                    $cid = $_GET["cid"];
                    
                    $query="SELECT pid,post_title,post_result,post_content,date FROM review WHERE cid=$cid";
                    
                    $result = mysql_query($query) or die("Query to get data failed with error: ".mysql_error());
                    
                    while($row = mysql_num_rows($result)) {
                    //details 
                    }
                    This is the script . What should i put in place of ?? .
                    Code:
                    <script>
                    $(document).ready(function() {
                         $('body').on('click', '#cat1', function() {
                    	 
                        $("#display").load("cat.php?cid=[B]??[/B]");
                    	
                      });
                    });
                    </script>
                    If i put 1 in place of ?? , nothing is getting displayed and no error message also .

                    Comment

                    • yateesh
                      New Member
                      • Dec 2012
                      • 36

                      #11
                      @Anas Mosaad
                      $("#display").l oad("cat.php?ci d=Cat%201"); here what should i put in place of Cat%201 . What does it actually mean .

                      Comment

                      • adi501
                        New Member
                        • Jan 2013
                        • 2

                        #12
                        in 1st table give cid as primary key,and 2nd table cid as foreign key ,and use join condition

                        Comment

                        • adi501
                          New Member
                          • Jan 2013
                          • 2

                          #13
                          Code:
                          SELECT * FROM post
                          INNER JOIN category
                          ON post.cid=category.cid
                          u can use where condition also.
                          Last edited by acoder; Jan 16 '13, 11:19 AM. Reason: Added [code] tags

                          Comment

                          • yateesh
                            New Member
                            • Dec 2012
                            • 36

                            #14
                            @adi501
                            In javascript $("#display").l oad("cat.php?ci d=Cat%201"); here what should i put in place of Cat%201
                            I need to pass cid value to server right , so what should i give there .

                            Comment

                            • yateesh
                              New Member
                              • Dec 2012
                              • 36

                              #15
                              @adi501
                              Nothing is getting displayed from below code :
                              Code:
                              $("#display").load("cat.php?cid=[B][I]cid%1[/I][/B]");
                              Code:
                              $cid = $_GET["cid"];
                              
                              $query="SELECT * FROM review
                              INNER JOIN category
                              ON review.cid=category.cid WHERE review.cid='$cid'";

                              Comment

                              Working...