Solved

PHP MySQL AJAX

Posted on 2010-11-26
6
660 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

617 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