/    Sign up×
Community /Pin to ProfileBookmark

php category tree

i want to develop a php category and subcategory tree dynamically.i maintained 2 tables for category and the subcategory.the category id of category table will become a foreign key to the subcategory table.i want to maintain a tree structure for the category and subcategory..onclick of category i need to display the corresponding subcategory..please anyone help me out i know a bit of javascript.

thanks in advance..

regards

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperJan 22.2010 — i want to develop a php category and subcategory tree dynamically.i maintained 2 tables for category and the subcategory.the category id of category table will become a foreign key to the subcategory table.i want to maintain a tree structure for the category and subcategory..onclick of category i need to display the corresponding subcategory..please anyone help me out i know a bit of javascript.[/quote]

Here are my ideas, noting you might get replies from others with completely different concepts/approaches since you didn't describe the interface you want:

Assuming all the data is in an actual database as you hinted and the data is static (doesn't change each run), load in all categories and sub-categories into an array structure, i.e. $data['category'][id]=>['subcategory'] or similar as there are a million ways to do that. Then just walk through the array and create your interface, which could be any of the following:

Simple div's created dynamically with each category and sub-category in a tree format graphically, just set display: none for sub-categories by default and as a user selects a category have it "expand" its sub-category by changing the display style to block or inline as you see fit. Technologies include database retreival (i.e. MySQL/PHP, PHP, Javascript, CSS and changing style attributes and targeting element ID's or classes via Javascript).

If the database data changes constantly you might consider this approach when walking through the same kind of array as noted above:

Simple div's created dynamically in the same way as a graphical tree, or maybe a pulldown menu which changes dynamically as a category is selected. But instead of pre-population use Ajax to dynamically load the sub-category. Ajax will call a server side script that queries your database to get the sub-categories for the currently selected category before returning control to the script that displays the interface. Technologies include the same as the other idea but Ajax as well.

Get the idea?

To get the gears turning in your head and so you can see something in action to spurn your creativity and come up with your own solutions please look at this cool page which loads database content dynamically and creates dynamic pulldown menus updated via Ajax, similar to idea #2 stated above, but with real code you can browse and study:

http://www.plus2net.com/php_tutorial/php_drop_down_list.php

-jim
Copy linkTweet thisAlerts:
@prathikanoeauthorJan 23.2010 — THANKS FOR THE EXPLANTION OF MAKING THE TREE BUT THE DEMO LINK WHAT U HAVE GIVEN FR THE REFERENCE IS FOR DROP DOWN LIST. I IMPLEMENTED THE SAME LOGIC FOR CREATING THE CATEGORY AND THE SUBCATEGORY.SO FOR DISPLAYING THE TREE I USED AJAX FUNCTION AS JS IS A ONCLICK EVENT IN AJAX I AM CALLING A PHP PAGE. EVERYTHNG IS FINE BUT THE POSITIONING I AM NOT GETTNG EXACTLY LIKE IF I CLICK ON the category.

please help me out
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 23.2010 — Might not wanna type in all caps, on the Internet this is construed as screaming even if you didn't intend that. FYI.

BUT THE DEMO LINK WHAT U HAVE GIVEN FR THE REFERENCE IS FOR DROP DOWN LIST[/quote]

Um, yes. That's why I said, when I gave you the link, "look at this cool page which loads database content dynamically and creates dynamic pulldown menus..."

EVERYTHNG IS FINE BUT THE POSITIONING I AM NOT GETTNG EXACTLY LIKE IF I CLICK ON the category[/QUOTE]

Too many possibilities here without seeing a screenshot or link I can test showing exactly what is not positioned correctly. Or explain in detail at minumum. Thanks!

-jim
Copy linkTweet thisAlerts:
@prathikanoeauthorJan 25.2010 — hi..thanks for ur reply..i will post the code just check it and let me knw pls..




<script type="text/javascript">

function getXMLHTTP()

{ //fuction to return the xml http object

var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}

return xmlhttp;
}



function callAjax(URL) {

var req = getXMLHTTP();

if (req) {

req.onreadystatechange = function()
{
if (req.readyState==4)
{
// only if "OK"
if (req.status == 200)
{
alert(document.getElementById('txtid').innerHTML=req.responseText); }
else
{
alert("There was a problem while using XMLHTTP:n" + req.statusText);
}
}
}
req.open("GET", URL, true);
req.send(null);
}

}



</script>


</head>

</html>

<?php

include("connection.php");

$q="select * from category";

$r=mysql_query($q);

$row=mysql_fetch_object($r);

while($row!=NULL)

{

$id=$row->category_id;

$name=$row->caty_name;

?>

<table border="0">


<td>

<td class="style5" >





<input type="button" onclick="callAjax('newajpro.php?category_id='+this.value);" value="<?php echo $id;?>"/><?php echo $name;?>

<div id="txtid"></div>

</td>

<?php

$row=mysql_fetch_object($r);

}

?>


[B]newajpro.php[/B]

<?php

include("connection.php");

$id1=$_REQUEST['category_id'];

echo $id=$id1+1;

$query="select * from subcategory s,product p where s.category_id=$id1 and p.procat=$id1";

$r1=mysql_query($query);

$row1=mysql_fetch_array($r1);

while($row1!=NULL)

{

$id2=$row1['sub_catid'];

$name2=$row1['sub_catname'];

$name3=$row1['proname'];

$id3=$row1['productid'];

?>

<div id="txtid">

<?php echo $name2;?>

</div>

<a href="newcatrgory2.php?productid=<?php echo $id3?>"><?php echo $name3;?></a>

<?php

$row1=mysql_fetch_array($r1);

}

?>

please let me know wr i am wrong..query is working fine
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 25.2010 — Please always use the PHP and other code tags when posting code so the syntax highlighting takes effect, next time. Thank you.

The code you supplied does not help me with positioning/CSS issues, for that I need to see it in action via browser and test it. Based solely on the code you're updating the textid div and an alert with the output from the Ajax call, simple as that. Please supply me with what I asked for.
Copy linkTweet thisAlerts:
@prathikanoeauthorJan 27.2010 — hi..thanks fr ur reply..i am not using any css for the postioing.just i used div. just tell me what css i need to write for positioning..thanks in advance
Copy linkTweet thisAlerts:
@SrWebDeveloperJan 27.2010 — [I]Round 2... ding...[/I]

If you're not using CSS for positioning, then you're doing it in the HTML either by table layout or within the flow of the browser page, etc. But it's hard to tell what you want without seeing it via browser and comparing it with what is actually happening. As, maybe now you can see why I made my request? That's rhetorical.

Since you said "just tell me what css i need to write for positioning", your wish is my command, so here is some excellent help so you may achieve your goal:


[LIST]
  • [*]Learn CSS positioning here in 10 easy steps (float, static, absolute, relative position and top/left)

  • [*]Learn CSS centering techniques here

  • [/LIST]


    If you're new in general to CSS, take this beginner's tutorial (W3 schools, a great source for stuff like this - if you're newbie, read this first)

    [B]Why learn and use CSS positioning?[/B]

    These days we encourage developers to separate style from content, i.e. wrap content with div's and span's and assign classes and ID's and use a CSS stylesheet for layout, positioning, look and feel. Doesn't have to be done that way, but it has certain advantages.

    So take the time to learn and have fun! We're here to help if you get stuck on a technical issue. Hope you found this reply helpful. Cheers.

    -jim
    Copy linkTweet thisAlerts:
    @barnabaApr 30.2010 — I can also suggest html tutorial for learning css and other.
    ×

    Success!

    Help @prathikanoe spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...