Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP MySQL AJAX

Posted on 2010-11-26
6
Medium Priority
?
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

660 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