Solved

PHP MySQL AJAX

Posted on 2010-11-26
6
646 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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…
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.

786 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