need help in connnect menu to database

Discussion in 'PHP' started by legend19892008, Jul 2, 2008.

  1. #1
    legend19892008, Jul 2, 2008 IP
  2. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    From what I gather you want a tree stucture, so when you click a category it expands to give subcategories?
    If this is correct, the best way to do this is with ajax so the page doesnt have to completely reload.

    Send the category name to a javascript function, the function will then load a php script which will handle which subcategories to show (possibly from a database) and what each one of them links to. This will all be done in place of a paticular element, so each category may have a div element around them, the ajax function will then replace the div element with the appropriate category and also the new categories.

    I have an example of ajax in place at projectword.co.uk. Search for a word, go to define it, then add it to the basket. The div underneath "Add to basket" is replaced with the appropriate information.

    I hope this is a help. If this is what you need I have code I have written which could be of a use to you

    Thanks
    ProjectWORD
     
    projectWORD, Jul 2, 2008 IP
  3. legend19892008

    legend19892008 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i think ur description is right,but ur code is so different
    if u have an ajax code or any code that do that plz help us
    i have database contain categories and each category contain some subcategories ,i want them in a menu containing these items and sub items
    waiting
     
    legend19892008, Jul 2, 2008 IP
  4. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #4
    Ok, well wrap a div around each category, give them an id of the corresponding category. In the link, make an onclick event to a function in a .js file. (Ensure you link to your .js file in your <head></head> section)

    The javascript that will work for this is:-

    var xmlHttp

    function showCategory(category)
    {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
    alert ("Browser does not support HTTP Request")
    return
    }
    var url="category_change.php"
    url=url+"?category="+category
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }

    function stateChanged()
    {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById(category).innerHTML=xmlHttp.responseText
    }
    }

    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
    //Internet Explorer
    try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    return xmlHttp;
    }

    There are three key elements of this.
    1) The link to a php script that will handle all database manipulation and displaying the relevant information
    2) The name of the div that the php script will replace
    3) The arguement that is passed to the script, use this to know what category the user has clicked on and therefore select the relevant subcategories.

    I hope this is a help to you. Post a reply if you need any more help. (PM me as you did to remind me)

    Regards
    projectWORD
    http://www.projectword.co.uk
     
    projectWORD, Jul 3, 2008 IP
  5. legend19892008

    legend19892008 Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i am sorry i am a beginner ,so i cant understand this
    if u can do me a favour and help me in the coding
    my code to read categories is
    i want to show the subcategories of each category
     
    legend19892008, Jul 3, 2008 IP
  6. projectWORD

    projectWORD Active Member

    Messages:
    287
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #6
    <?
    $conn=mysql_connect("localhost","root","root");
    mysql_select_db('project3');
    $sql="select * from category";
    $rs=mysql_query($sql);

    while($data=mysql_fetch_array($rs))
    {
    $cat = $data['name'];
    ?>

    <a href="#" onclick="loadSubCat('<? print $cat; ?>')" /><? print $cat; ?></a>

    <?
    }
    ?>

    Then create a javascript file with the function loadSubCat as like the one I showed you.
    Ensur you have this in your header
    <script src="NAMEOFFILE.js"></script>
     
    projectWORD, Jul 4, 2008 IP