Recommend this page to a friend! |
Dynamic Menu in PHP | > | All threads | > | Dropdown menu | > | (Un) Subscribe thread alerts |
|
David Quirk - 2010-01-17 17:47:16
Downloaded and ran this code without a single problem. That is not my usual experience with contributed classes, which almost always require editing before they function.
The code is straightforward, clean, and, in fact, elegant. I highly recommend it.
Michael Schulz - 2010-01-18 14:18:57 - In reply to message 1 from David Quirk
Well, my web server is a bit more squeamish about cleanliness of code, so I ran in a few warnings, nothing too serious but not clean:
- In quite a number of lines the index names are not quoted (e.g. $res[$i][id] instead of $res[$i]['id']) which causes warnings like this: Use of undefined constant id - assumed 'id' in <someplace>/functions.php on line 76 - It might be a good idea to define the variable $data in function select_row(), e.g. add a "$data=array();" in line 28. Without this my whole output was garbled in just one big orange rectangle peppered with this warning: Notice: Undefined variable: data in <someplace>/functions.php on line 38 But anyway, thanks for writing this class, I really can use it.
Pravin Sonawane - 2010-01-18 15:20:46 - In reply to message 1 from David Quirk
Thanks for your valuable message
Pravin Sonawane - 2010-01-18 15:24:04 - In reply to message 2 from Michael Schulz
Thanks for your inputs, I will definitely try to do this updates.
sachin - 2011-09-22 07:28:51 - In reply to message 1 from David Quirk
Guys use this code in functions.php and use <?php echo ?> where ever you have <?= ?>
<?php // PROJECT RELATED FUNCTIONS class PHP_fun { function getConfig() { //include("config.php"); $this->SITE_URL = 'http://localhost:443/menu'; $this->DB_SERVER = 'localhost'; $this->DB_USER = 'root'; $this->DB_PASS = ''; $this->DB_NAME = 'menu'; } function __construct() { $this->getConfig(); $Conn = mysql_connect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS); if (!$Conn) die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn)); $DB_select = mysql_select_db($this->DB_NAME, $Conn); if (!$DB_select) die("Error: ".mysql_errno($Conn).":- ".mysql_error($Conn)); } function select_row($sql) { $data=array(); //echo $sql . "<br />"; if ($sql!="") { $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error()); if ($result) { while($row = mysql_fetch_array($result)) $data[] = $row; } return $data; } } function recordCount($sql) { if ($sql!="") { $result = mysql_query($sql) or die("Error: ".mysql_errno().":- ".mysql_error()); if ($result) { $cnt = mysql_num_rows($result); return $cnt; } } } function createProductUrl($url) { $url = trim($url); if ($url != "") { $url = trim(str_replace(" ","-",$url)); //return $url.".html"; return $url; } } function getChild($id) { $this->getConfig(); $menu = ""; $str = ""; $s = "select id,title,parentid,link from ms_product where parentid = '$id' "; $res = $this->select_row($s); $menu .= '<div id="'.$id.'" style="display:none; position:absolute;" onmouseover="javascript: return showId('.$id.');" onmouseout="javascript: return hideId('.$id.');">'; $menu .= '<table border="1" cellspacing="0" cellpadding="0" style="border: 1px solid #FDCB55; border-collapse:collapse;">'; for ($i=0;$i<count($res);$i++) { $cnt_of_child = $this->recordCount("select id from ms_product where parentid = '".$res[$i]['id']."' "); if ($cnt_of_child > 0) $str = ' <img src="'.$this->SITE_URL.'images/arrow_white.gif">'; else $str = " "; $menu .= '<tr height="20"><td align="left" class="aerial12" onmouseover="this.className=\'aerial12over\';return showId('.$res[$i]['id'].');" onmouseout="this.className=\'aerial12\';return hideId('.$res[$i]['id'].');" style="cursor:pointer;">'; $menu .= '<div style="padding-left:10px;padding-right:5px; width:125px;" onclick="javascript: return redirect(\''.$res[$i]['link'].'/\');">'; $menu .= $res[$i]['title'].$str; $menu .= '</div>'; $menu .= '</td><td align="left" valign="top">'; $menu .= $this->getChild($res[$i]['id']); $menu .= '</td><tr>'; } $menu .= '</table>'; $menu .= '</div>'; return $menu; } function getMenu($parentid) { $this->getConfig(); $menu = ""; $s = "select id,title,parentid,link from ms_product where parentid = '$parentid' "; $res = $this->select_row($s); ob_start(); ?> <table border="0" cellspacing="0" cellpadding="0" width="740" align="center"> <tr height="30"> <?php for ($i=0;$i<count($res);$i++) { ?> <td align="left" valign="middle" bgcolor="#FCBB2B"> <div align="center" onmouseover="javascript: return showId('<?php echo $res[$i]['id']?>');" onmouseout="javascript: return hideId('<?php echo $res[$i]['id']?>');" onclick="javascript: return redirect('<?php echo $res[$i]['link']?>');" class="aerial12" style="height:15px; vertical-align:middle; padding-top:5px;cursor:pointer;"><?php echo $res[$i]['title']?></div><?php echo $this->getChild($res[$i]['id'])?> </td> <?php if ((count($res) - 1) > $i) {?> <td align="left" valign="middle" bgcolor="#FCBB2B">|</td> <?php } ?> <?php } ?> </table> <?php $menu = ob_get_contents(); ob_end_clean(); return $menu; } }//class PHP_fun() ?>
Shashank Goyal - 2013-11-01 15:24:26 - In reply to message 1 from David Quirk
Can you please share the code if instead of link from database, i want content from database and that will show below the dropdown menu on the same page without refresh.
Thanks in advance. |
info at phpclasses dot org
.