Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP MySQL AJAX

Posted on 2010-11-26
6
Medium Priority
?
666 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:
Bruce Smith earned 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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:Bruce Smith
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

972 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