I am attempting to build a category navigation system and so far have all my tables set up and indexed in mysql. My issue is understanding the logic for retrieving the categories I want using php.
I am thinking I need something like:
On click - store clicked category name in variable ($category) - query variable against database (SELECT subcategory_name FROM subcategories WHERE parent = "$category")
Does this sound right? If so how do I grab the clicked category and add it to my variable?
Capture clicked category and assign to variable.
Moderator: General Moderators
Re: Capture clicked category and assign to variable.
Can you post the code you have written so far? Please use the "PHP Code" button to surround your code with Syntax tags, for easier reading. Yes, you're on the right track.Richard66 wrote:I am attempting to build a category navigation system and so far have all my tables set up and indexed in mysql. My issue is understanding the logic for retrieving the categories I want using php.
I am thinking I need something like:
On click - store clicked category name in variable ($category) - query variable against database (SELECT subcategory_name FROM subcategories WHERE parent = "$category")
Does this sound right? If so how do I grab the clicked category and add it to my variable?
Re: Capture clicked category and assign to variable.
It also sounds like you will need to use AJAX, as you obviously can't run PHP code after the page has loaded (without using AJAX). Your logic looks right, although be careful when dynamically changing a select field's options... If you just remove the options then re-add new ones it won't work in IE. You pretty much have to remove the entire select box, otherwise IE just fills it with blanks. Took me hours to work out what was going on when I tried it.
Re: Capture clicked category and assign to variable.
Thanks for your input guys! For a little more clarity:
The following code grabs my first level categories from the database and displays them:
I dont know if I can use the anchor tag to my advantage which currently has no value entered?
I then want to query the database based on which of these categories I click. This will hopefully help explain whats in my head :0)
I have very little/no experience with ajax so would probably need an existing script to play with, but so far unfortunately I have not found one that can do what I want.
The following code grabs my first level categories from the database and displays them:
I dont know if I can use the anchor tag to my advantage which currently has no value entered?
Code: Select all
<?php
$dbc = mysql_connect($db_host,$db_user,$db_pass);
$sdb = mysql_select_db($db_database);
$query = "SELECT category_name, category_desc FROM categories";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$catname = $row["category_name"];
$catdesc = $row["category_desc"];
echo "<li><a href=''>$catname<br /><span>$catdesc</span></a></li>";
}
?>
Code: Select all
<?php
$category = "/*This is where i am confused... I need to fill this variable with the string of the category clicked */";
$query = "SELECT subcategory_name FROM subcategories WHERE parent = "$category";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$subcatname = $row["subcategory_name"];
$subcatdesc = $row["subcategory_desc"];
echo "<li><a href=''>$subcatname<br /><span>$subcatdesc</span></a></li>";
}
?>
Re: Capture clicked category and assign to variable.
Hey,
Is the problem just that you're not sure how to get the category name after a link has been clicked? AJAX is only necessary if you're trying to dynamically load in the new categories without actually changing page, but I can't see you saying that you don't want to change page?
What do your links look like? If I'm understanding your problem correctly, then you just need your links to look like: viewCategory.php?cat=clothes
This creates a '$_GET' variable called 'cat', with the value 'clothes', which you can then use to query your database. Is this the kind of thing you mean?
Is the problem just that you're not sure how to get the category name after a link has been clicked? AJAX is only necessary if you're trying to dynamically load in the new categories without actually changing page, but I can't see you saying that you don't want to change page?
What do your links look like? If I'm understanding your problem correctly, then you just need your links to look like: viewCategory.php?cat=clothes
This creates a '$_GET' variable called 'cat', with the value 'clothes', which you can then use to query your database. Is this the kind of thing you mean?
Re: Capture clicked category and assign to variable.
You must first clearly understand how PHP is related to a web page. You can use PHP to generate the code for a selection dropdown HTML element from data in a database at the server, but once the server sends the HTML web page to the browser, PHP is finished. There is NO PHP CODE by the time the web page arrives at the browser. So when you say "On click - store clicked category name in variable ($category) - query variable against database", you are talking about 2 different worlds: by the time a user views the page and can click on a selection, THERE ARE NO PHP VARIABLES anymore, they don't exist. There are only 2 ways to interact with the server and PHP code after that:
1. You can send the selection data back to the server as $_GET or $_POST data (probably using an HTML Form) addressed to another PHP script (or the same one, if you provide code branching to handle the logic appropriately) which can then process the data and SEND A NEW WEB PAGE BACK TO THE BROWSER with the appropriate content based on the selection; OR
2 You can detect the selection with Javascript in the browser and employ a special Javascript object called "XMLHttpRequest", which can send the selection data as an "asynchronous" request back to the server, requesting that the server process the data and send back appropriate data to the Javascript object, NOT as a new web page, but as pure data that your Javascript function can use to update the content of the SAME PAGE that it's already displaying. This is known as "AJAX" (Asynchronous Javascript And XML).
The choice between the 2 methods above will depend on what specific functionality you need. The first choice sends an entirely new page, which has to be loaded and thus may cause a slight delay or "flicker", depending on how complex the page is. Sometimes this is what needs to be done anyway, such as when the choice would dictate entirely different page content. The second choice produces a smooth update because it doesn't involve loading a new page in the browser, only some Javascript that modifies some of the content of the page that's already loaded. But you have to do additional programming to make that happen.
1. You can send the selection data back to the server as $_GET or $_POST data (probably using an HTML Form) addressed to another PHP script (or the same one, if you provide code branching to handle the logic appropriately) which can then process the data and SEND A NEW WEB PAGE BACK TO THE BROWSER with the appropriate content based on the selection; OR
2 You can detect the selection with Javascript in the browser and employ a special Javascript object called "XMLHttpRequest", which can send the selection data as an "asynchronous" request back to the server, requesting that the server process the data and send back appropriate data to the Javascript object, NOT as a new web page, but as pure data that your Javascript function can use to update the content of the SAME PAGE that it's already displaying. This is known as "AJAX" (Asynchronous Javascript And XML).
The choice between the 2 methods above will depend on what specific functionality you need. The first choice sends an entirely new page, which has to be loaded and thus may cause a slight delay or "flicker", depending on how complex the page is. Sometimes this is what needs to be done anyway, such as when the choice would dictate entirely different page content. The second choice produces a smooth update because it doesn't involve loading a new page in the browser, only some Javascript that modifies some of the content of the page that's already loaded. But you have to do additional programming to make that happen.
Re: Capture clicked category and assign to variable.
Eh... I think you messed up your reply.jerry0503222 wrote:Is the problem just that you're not sure how to get the category name after a link has been clicked? AJAX is only necessary if you're trying to dynamically load in the new categories without actually changing page, but I can't see you saying that you don't want to change page?
Re: Capture clicked category and assign to variable.
No, that was a spammer, whom I have now deleted, along with his 4 posts this morning. They think they are so cool, posting nonsense, but with a link to their commercial websites, which are usually either fraudulent or really trashy. We get dozens of them a week, but the other moderators and I usually spot them within a short time (often less than an hour) and so they don't get many (if any) viewers, so their efforts are wasted anyway. That's what I think is funny.RCA86 wrote:Eh... I think you messed up your reply.jerry0503222 wrote:Is the problem just that you're not sure how to get the category name after a link has been clicked? AJAX is only necessary if you're trying to dynamically load in the new categories without actually changing page, but I can't see you saying that you don't want to change page?