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 -
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. -
$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 . -
<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. -
<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.
18 3788
In your cat.php, you never define what $cid is supposed to be.
Okay. How do i go about that ?
You second snippet, the fourth line may need to be something like this: - $query="SELECT * FROM category WHERE cid='" . $cid . "'";
-
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:
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.
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. -
<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: -
$('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.
One more thing, your page is vulnerable to SQL injection attack. You should encode/escape the parameters in order to close this security hole.
@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 .
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.
This is my updated statements in cat.php -
$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 ?? . -
<script>
-
$(document).ready(function() {
-
$('body').on('click', '#cat1', function() {
-
-
$("#display").load("cat.php?cid=??");
-
-
});
-
});
-
</script>
-
If i put 1 in place of ?? , nothing is getting displayed and no error message also .
@Anas Mosaad
$("#display").load("cat.php?cid=Cat%201"); here what should i put in place of Cat%201 . What does it actually mean .
in 1st table give cid as primary key,and 2nd table cid as foreign key ,and use join condition
- SELECT * FROM post
-
INNER JOIN category
-
ON post.cid=category.cid
u can use where condition also.
@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 .
@adi501
Nothing is getting displayed from below code : -
$("#display").load("cat.php?cid=cid%1");
-
-
$cid = $_GET["cid"];
-
-
$query="SELECT * FROM review
-
INNER JOIN category
-
ON review.cid=category.cid WHERE review.cid='$cid'";
-
@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.
@Anas Mosaad -
$('body').on('click', '#cat1', function() {
-
$("#display").load("cat.php?cid%1");
-
cat.php -
$cid = $_GET["cid"];
-
-
$query="SELECT * FROM post
-
INNER JOIN category
-
ON post.cid=category.cid WHERE post.cid='$cid'";
-
Tell me if the above code is proper or not .
The above code says undefined variable : cid in cat.php
Your line 2 in the first snippet should be: - $("#display").load("cat.php?cid=1");
@Anas Mosaad
Thank you very much .
My problem has been solved .
Sign in to post your reply or Sign up for a free account.
Similar topics
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="#"...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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"
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |