Solved

PHP MySQL AJAX

Posted on 2010-11-26
6
644 Views
Last Modified: 2012-05-10
Hi there,

My question is based on this example:
http://www.opencrypt.com/blog.php?a=23

I need to make the static tree menu dynamic with my MySQL database:

ere is my database table structure:
Table name: pages
fields:
id - int 255 auto-increment;
parent_id - int 255;
linklabel - varchar 255;

Please help!

Thanks,

owennicol
0
Comment
Question by:owennicol
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Zado
ID: 34218094
0
 

Author Comment

by:owennicol
ID: 34218114
Thanks for the quick reply Zado!

That is only half a solution really, the bits in bold in the code are the bits I need help with too!

0
 
LVL 11

Accepted Solution

by:
patsmitty earned 250 total points
ID: 34220876
Okay, so I threw together an example for you to dynamically create this tree with values from your database via php and mysql. You need to add the source files into your directory and then you can see the syntax from the following code of how to connect to your database to create this menu.

 
<?php
mysql_connect("dbHost", "dbUserName", "dbPassword") OR DIE('Unable to connect to database! Please try again later.');
mysql_select_db('dbName');
$sql = 'SELECT parent_id, linklabel FROM pages';
$result = mysql_query($sql);
$counter = 0;
$label_array = array();
if ($result):
    while ($row = mysql_fetch_array($result)):
        $label_array[$counter] = array("parent" => $row['parent_id'], "linklabel" => $row['linklabel']);
        $counter += 1;
    endwhile;
endif;

function getParent($id) {
mysql_connect("dbHost", "dbUserName", "dbPassword") OR DIE('Unable to connect to database! Please try again later.');
mysql_select_db('dbName');
    $sql = "SELECT linklabel FROM pages WHERE id = '" . $id . "'";
    $result = mysql_query($sql);
    if ($result):
        while ($row = mysql_fetch_array($result)):
            return $row['linklabel'];
        endwhile;
    endif;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Dynamic Tree Menu Example</title>

        <style type="text/css">

            .text					{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 13px; color : #000000; }
            a.text			{ color: #0000aa; text-decoration: none; }
            a.text:hover		{ color: #0000aa; text-decoration: underline; }

            .ajax_tree0		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 18px; color: #D21A20; text-decoration: none; }
            a.ajax_tree0		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree0:hover        { color: #D21A20; text-decoration: underline; }

            .ajax_tree1		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 17px; color: #D21A20; text-decoration: none; }
            a.ajax_tree1		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree1:hover        { color: #D21A20; text-decoration: underline; }

            .ajax_tree2		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 16px; color: #D21A20; text-decoration: none; }
            a.ajax_tree2		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree2:hover        { color: #D21A20; text-decoration: underline; }

            .ajax_tree3		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 15px; color: #D21A20; text-decoration: none; }
            a.ajax_tree3		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree3:hover        { color: #D21A20; text-decoration: underline; }

            .ajax_tree4		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 14px; color: #D21A20; text-decoration: none; }
            a.ajax_tree4		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree4:hover        { color: #D21A20; text-decoration: underline; }

            .ajax_tree5		{ font-family : Verdana, Arial, Helvetica, sans-serif; font-size : 13px; color: #D21A20; text-decoration: none; }
            a.ajax_tree5		{ color: #D21A20; text-decoration: none; }
            a.ajax_tree5:hover        { color: #D21A20; text-decoration: underline; }

        </style>

        <script type="text/javascript">

            var open_obj = new Array()

            function Expand(obj,checkbox) {

                var obj2 = obj;
                var img_obj = "img_" + obj;
                var div_obj = "div_" + obj;
                var check_obj = "check_" + obj;

                if (open_obj[obj] == true) {
                    if (checkbox != "1") {
                        document.getElementById(div_obj).style.display = "none";
                        document.getElementById(img_obj).src = "collapsed.gif";
                        open_obj[obj] = false;
                    }
                } else {
                    document.getElementById(div_obj).style.display = "block";
                    document.getElementById(img_obj).src = "expanded.gif";
                    open_obj[obj] = true;
                }

                if (checkbox == "1") {

                    var checkboxes = document.forms["form"].elements[check_obj];
                    for (var i = 0; i < checkboxes.length; i++) {
                        if (checkboxes[i].checked == true) {
                            checkboxes[i].checked = false;
                        } else {
                            checkboxes[i].checked = true;
                        }
                    }
                }
            }
        </script>
    </head>
    <body style="background: #000000 url('image/bgd_main.gif') repeat-x; color: #FFFFFF;">
        <?php
        $tree_data = array();
        $counter = 0;
        foreach ($label_array as $menu_item):
            $parent_id = $label_array[$counter]["parent"];
            if ($parent_id != '0'):
                $parent_id = getParent($parent_id);
            endif;
            $tree_data[$counter] = array(url => "#", parent => $parent_id, name => $label_array[$counter]["linklabel"]);
            $counter++;
        endforeach;
        require "ajax_tree.php";
        print ajax_tree($tree_data, 1, 1);
        ?>
    </body>
</html>

Open in new window


Here are the database contents:

 Image of the database contents...
The following link shows you the result of the above code: http://www.patsmitty.com/OpenCryptExample/example.php

Hope this helps you out!

cheers
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:owennicol
ID: 34221034
Thank you so much, your solution was perfect for my needs.

It's really helped me out a lot, thanks again.
0
 
LVL 11

Expert Comment

by:patsmitty
ID: 34221084
Cool, you're welcome. I didn't comment on any of the code so I hope you know what's going on here. Don't hesitate to ask...

Cheers
0
 

Author Comment

by:owennicol
ID: 34221097
Actually, yes please to an extra bit of help!

I've just posted a related question!

Thanks

Owen
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now