473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

36
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

Expand|Select|Wrap|Line Numbers
  1. mysql_connect('localhost','root','');
  2.  
  3. mysql_select_db("database") or die("Unable to select database");
  4.  
  5. $title=$_POST['post_title'];
  6. $result=$_POST['post_result'];
  7. $content=$_POST['post_content'];
  8. $sid=$_POST['cid'];
  9. $date=$_POST['date'];
  10.  
  11. $insertquery="INSERT INTO review (post_title,post_result,post_content,cid,date)
  12.           VALUES('$title','$result','$content',$sid,NOW())";
  13.  
  14.  
  15. $result=mysql_query($insertquery);
  16.  
  17. if(! $result )
  18. {
  19.   die('Could not enter data: ' . mysql_error());
  20. }
  21. else {
  22.  
  23.         echo "Entered data successfully\n";
  24.         header('Location:home.php');
  25.     }    
  26.  
This is my cat.php which displays posts of particular category.

Expand|Select|Wrap|Line Numbers
  1. $link = mysql_connect("localhost","root","");
  2. mysql_select_db("database");
  3.  
  4. $query="SELECT * FROM category WHERE cid='$cid'";
  5.  
  6.  $result = mysql_query($query) or die("Query to get data failed with error:  ".mysql_error());
  7.  
  8. while($row = mysql_num_rows($result)) { 
  9. echo    //details goes here
  10.  
  11. }    
  12.  
This is my category menu list on home page .

Expand|Select|Wrap|Line Numbers
  1. <ul>            
  2. <li><a class="my-button" href="#">Cat 1</a></li>
  3. <li><a class="my-button" href="#">Cat 2</a></li>
  4. <li><a class="my-button" href="#">Cat 3</a></li>
  5. </ul>    
  6.  
  7.  
This is javascript.

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. $(document).ready(function() {
  3. $('body').on('click', '.my-button', function() {
  4.  
  5.    $("#display").load("cat.php");
  6.  
  7.    });
  8. });
  9. </script>    
  10.  
  11.  
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.
Jan 15 '13 #1
18 3788
Rabbit
12,516 Expert Mod 8TB
In your cat.php, you never define what $cid is supposed to be.
Jan 15 '13 #2
yateesh
36
Okay. How do i go about that ?
Jan 15 '13 #3
Anas Mosaad
185 128KB
You second snippet, the fourth line may need to be something like this:
Expand|Select|Wrap|Line Numbers
  1. $query="SELECT * FROM category WHERE cid='" . $cid . "'";
  2.  
Jan 15 '13 #4
yateesh
36
That is not working..
Jan 15 '13 #5
Rabbit
12,516 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. $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.
Jan 15 '13 #6
Anas Mosaad
185 128KB
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.
Expand|Select|Wrap|Line Numbers
  1. <ul>            
  2. <li><a class="my-button" id="cat1" href="#">Cat 1</a></li>
  3. <li><a class="my-button" id="cat2" href="#">Cat 2</a></li>
  4. <li><a class="my-button" id="cat3" href="#">Cat 3</a></li>
  5. </ul>    
  6.  
JavaScript handlers cat1 menu item:
Expand|Select|Wrap|Line Numbers
  1. $('body').on('click', '#cat1', function() {
  2.    $("#display").load("cat.php?cid=Cat%201");
  3.    });
  4. });
  5.  
You need to repeat the above code for all the categories.

Finally read the request parameter in your cat.php page.
Expand|Select|Wrap|Line Numbers
  1. $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.
Jan 15 '13 #7
yateesh
36
@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 .
Jan 16 '13 #8
Rabbit
12,516 Expert Mod 8TB
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.
Jan 16 '13 #9
yateesh
36
This is my updated statements in cat.php

Expand|Select|Wrap|Line Numbers
  1. $cid = $_GET["cid"];
  2.  
  3. $query="SELECT pid,post_title,post_result,post_content,date FROM review WHERE cid=$cid";
  4.  
  5. $result = mysql_query($query) or die("Query to get data failed with error: ".mysql_error());
  6.  
  7. while($row = mysql_num_rows($result)) {
  8. //details 
  9. }
  10.  
This is the script . What should i put in place of ?? .
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. $(document).ready(function() {
  3.      $('body').on('click', '#cat1', function() {
  4.  
  5.     $("#display").load("cat.php?cid=??");
  6.  
  7.   });
  8. });
  9. </script>
  10.  
If i put 1 in place of ?? , nothing is getting displayed and no error message also .
Jan 16 '13 #10
yateesh
36
@Anas Mosaad
$("#display").load("cat.php?cid=Cat%201"); here what should i put in place of Cat%201 . What does it actually mean .
Jan 16 '13 #11
adi501
2
in 1st table give cid as primary key,and 2nd table cid as foreign key ,and use join condition
Jan 16 '13 #12
adi501
2
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM post
  2. INNER JOIN category
  3. ON post.cid=category.cid   
u can use where condition also.
Jan 16 '13 #13
yateesh
36
@adi501
In javascript $("#display").load("cat.php?cid=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 .
Jan 16 '13 #14
yateesh
36
@adi501
Nothing is getting displayed from below code :
Expand|Select|Wrap|Line Numbers
  1. $("#display").load("cat.php?cid=cid%1");
  2.  
Expand|Select|Wrap|Line Numbers
  1. $cid = $_GET["cid"];
  2.  
  3. $query="SELECT * FROM review
  4. INNER JOIN category
  5. ON review.cid=category.cid WHERE review.cid='$cid'";
  6.  
Jan 16 '13 #15
Anas Mosaad
185 128KB
@yateesh: sorry for my late response. We are in a completely different time zones.

Anyway, Cat%201 is the encoded URL version of "Cat 1". Instead you should use 1, 2 or 3. I mistakenly put the category name instead of the cid. This should fix your issue.
Jan 16 '13 #16
yateesh
36
@Anas Mosaad


Expand|Select|Wrap|Line Numbers
  1. $('body').on('click', '#cat1', function() {
  2. $("#display").load("cat.php?cid%1");
  3.  
cat.php
Expand|Select|Wrap|Line Numbers
  1. $cid = $_GET["cid"];
  2.  
  3. $query="SELECT * FROM post
  4. INNER JOIN category
  5. ON post.cid=category.cid WHERE post.cid='$cid'";
  6.  
Tell me if the above code is proper or not .
The above code says undefined variable : cid in cat.php
Jan 16 '13 #17
Anas Mosaad
185 128KB
Your line 2 in the first snippet should be:

Expand|Select|Wrap|Line Numbers
  1. $("#display").load("cat.php?cid=1");
Jan 16 '13 #18
yateesh
36
@Anas Mosaad
Thank you very much .
My problem has been solved .
Jan 17 '13 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: wasntme | last post by:
I want to toggle a <div in and out of view. My question, which of the below examples would best be supported. Or is there another approach, that would be better used..TIA.. ..A.. <a href="#"...
7
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is...
1
by: Cleus | last post by:
Is there a way to display read only values in a drop down listbox? Below is an example of what I need displayed in the drop down list. The users should only be able to select Item 1-9. Category A-C...
6
by: GOI via AccessMonster.com | last post by:
Good day! I was fiddling around with the Tools-Startup menu. I unchecked the following checkboxes: Display Status Bar Allow Full Menus Allow Default Shortcut Menus Allow Built-in Toolbars...
4
by: Lisa | last post by:
Hi. I have an ASP app that allows administrators to add articles to a database for viewing by the public. What I want to do is display a summary of the article and give the user the visitor to the...
2
by: Jpipher | last post by:
Let me explain what I am trying to accomplish... Two forms -- we'll call them "Main" and "Related" A command button on "Main" runs a union query. The union query results are shown in "Related"...
20
by: Highlander | last post by:
Hello all. Consider the following HTA: <html> <head> <title>Date Pulldowns</title> <HTA:APPLICATION ID="HTAUI" APPLICATIONNAME="Date Pulldowns" SCROLL="no" SINGLEINSTANCE="yes"
7
by: karen987 | last post by:
The code below is for an asp page that pulls out a list of authors in a given category, from a news weblog. In this case, the category id (CID) is "37" . So the page should list all authors from...
0
by: Bogdan | last post by:
LoginStatus can be configured with LogoutPageUrl but there is no way to configure it with 'LoginPageUrl', i.e. a page that will be displayed upon a successful login. I'd like users to be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.