I hope somebody can help me, because i got an menu that is auto generated via my MySQL db.
Because i got the menu to work inside the website and with that i mean it works with "test.dk/about" but the a href is empty when it's going out of the website like "http://google.com"...
btw it's just a very simple UL LI menu no dropdown or something.
Here is my script
static function build_menu()
{
$result = mysql_query("SELECT * FROM menu");
$menu = array();
while ($row = mysql_fetch_assoc($result)) {
if ($row["is_external"]) {
$url = $row["url"];
} else if (empty($row["is_external"])) {
$url = get_page_url($row["page_id"]);
}
$menu[] = array("name" => $row["name"], "page_id" => $row["page_id"], "is_external" => $row["url"], "url" => $url);
}
return $menu;
}
static function get_page_url($page_id)
{
$result = mysql_query("SELECT view_id FROM page WHERE id = '$page_id'");
$result = mysql_fetch_assoc($result);
$view_id = $result["view_id"];
$result = mysql_query("SELECT listen_path FROM view WHERE id = '$view_id'");
$result = mysql_fetch_assoc($result);
$listen_path = $result["listen_path"];
return $listen_path;
}
static function render()
{
$result = mysql_query("SELECT * FROM menu"); ?>
<div class="menu">
<ul><?php while ($item = mysql_fetch_assoc($result)) { ?>
<li><?php echo $item["name"] ?>
</li> <?php } ?></ul></div><?php
}
How can i fix it, so it works both internal and external?
<div class="menu"> <ul> <li>Homepage</li> <li>About</li> <li>Develop</li> <li>Support</li>
This should be <li>Support</li>; </ul> </div>
You only check for an external link in the function build_menu(), but this function isn't called anywhere from your render() function.
The render() function only calls get_page_url() which doesn't distinguish between internal and external links.
Href parameter of external URL must start with protocol declaration, so with "http://" in your case.
So change your code in condition inside the function "build_menu", if the URL is external, add "http://" to it, something like this:
$url = 'http://'.$row["url"];
I got it work after a while!
I simply just created an If else statement in the render function
static function render(){
$menu_items = self::get();
?><div class="menu"><ul><?php while ($item = mysql_fetch_assoc($menu_items)) { ?>
<li><a href="<?php
if(empty($item["is_external"]))
{
echo self::get_page_url($item["page_id"]);
}
else if($item["is_external"] = 1)
{
echo $item["url"];
}
?>"><?php echo $item["name"] ?></a>
</li> <?php } ?></ul></div><?php
}
Related
I have a php page with two navs. One is an admin nav, the other is public. using the code below I'm trying to determine the directory i'm in and depending on that show the proper nav. I feel like this php snippet should work.
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else if (is_dir($admin)) {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?">
<nav class="<?php echo $admin; ?">
I've also tried the following:
<?php
$public = APP_PUBLIC_PATH;
$admin = APP_ADMIN_PATH;
if(is_dir($public)) {
$publicnav = "showme";
$adminnav = "hideme";
}
else {
$publicnav = "hideme";
$adminnav = "showme";
}
?>
<nav class="<?php echo $publicnav; ?>">
<nav class="<?php echo $admin; ?>">
is_dir only checks to see if the path you provided is a directory or not. Your goal is to check whether the current path is for the admin or for a regular user?
Look into the $_SERVER superglobal variable on how to get the current URI. I believe something like this.
$currentPath = $_SERVER['REQUEST_URI'];
if ($currentPath == $public) {
// do logic
} else if ($currentPath == $admin) {
// other logic
}
Here Is my question: What I am wanting To do is Take Results from a mysql table and turn them into a menu and a drop down menu
HERE IS A QUICK EXAMPLE:
if you see in my mysql table i have page_name and parent, So the example is:
page_name and if i have row 1 the page_name is 'Home' now it's parent is 'none' right but on id number 39 the page_name is 'Contact Us' and the Parent Is 'Far Far Away 123' so if the parent is equal to 'none' then it will show at the top of the menu not the drop down if it has a parent it will show under that parent like this:
Home | the ben page | The Brock Page | Far Far Away 123 | dsfk
Contact Us
You see Contact Us is under Far Far Away Because the parent Is Far Far Away 123
here is my table:
Here is my code That I am trying but it is not working for some reason:
<ul>
<?php
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($links = mysqli_fetch_assoc($result)) {
if($links['parent'] !== "none") {
?>
<li id = "<?php echo $links['id']; ?>"><a href="
<?php
echo "page.php?id=" . $links['id'] . "\" title=\"" . $links['page_title'] . "\"";
?>>
<?php
echo $links['page_name'];
?>
</a>
<?php
if($links['parent'] !== "none") {
$child = "";
$sql = "SELECT * FROM pages";
$result = mysqli_query($db, $sql);
while($row = mysqli_fetch_assoc($result)) {
if($row['parent'] !== "none") {
$child = $row['page_name'];
}
}
echo "<ul id=\"sub_menu\" class=\"sub_navagation" . $links['id'] . "\">";
echo "<li>";
echo $child;
echo "<li>";
echo "</ul>";
}
?>
</li>
<?php
}
}
?>
</ul>
CSS:
#sub_menu {
display: none;
}
#sub_menu:hover {
display: block;
}
Ok if as you can see i have the parent row in the MYSQL table and on id number 39 i want the 'Far Far Away123' to be the parent of Contact Us and i want to show it when i hover over 'Far Far Away123'
My suggestion is to build out an array of all the results. Then run through that array (instead of multiple database queries).
I added a function build_dropdown() that will take the page name and run through the array of pages to see if there are any items with a parent matching. If so, we make an array of those items and run through them to build the dropdown menu. If not, it does nothing and moves on to the next menu item.
<?php
function build_dropdown ($parent, $pages){
foreach($pages as $page){
if($page['parent'] == $parent){
$items = $page;
} // END if
} // END foreach
if(is_array($items)){ // If a sub
echo '<ul id="sub_menu" class="sub_navagation'. $item['id'] .'">';
foreach($items as $item){
echo '<li>'.$item['name'].'<li>';
} // END foreach
echo '</ul>';
} // END if
}
$sql = "SELECT * FROM pages ORDER by item_order";
$result = mysqli_query($db, $sql);
confirm_query($result);
while ($row = mysqli_fetch_assoc($result)) {
$pages[] = $row; // Add each row to $pages array to use later
}
foreach($pages as $key => $page){
if($page['parent'] == 'none'){ ?>
<li id = "<?php echo $page['id']; ?>">
<a href="page.php?id=<?php echo $page['id']; ?>" title="<?php echo $page['page_title']; ?>">
<?php echo $page['page_name']; ?>
</a>
<?php
build_dropdown($page['page_name'], $pages); // If there are child items then build them out
?>
</li>
<?php
} // END if
} // END foreach
?>
I suggest you will need to JOIN your table to basically query it again to get the parent value, and add that to your markup.
SELECT *
FROM Pages
LEFT JOIN Pages p2 on page_name = p2.parent
(note: the syntax above may not be right, but I wanted to give you an idea of where I would start).
I'm getting this annoying error and haven't been able to fix it yet.
<b>Fatal error: Class 'Console' not found in /home/serellyn/public_html/HEIM/php/nieuwbeheer/console_overview.php on line 45.</b>
Let's first start with the hierarchy which is like this.
index (main page)
console_overview (section of page)
include/connect (connect to DB)
include/console.class (the class)
The index.php requires the connect.php and the console.class.php and loads the console_overview.php. Here's the code:
<?php
require_once('include/connect.php');
require_once('include/console.class.php');
var_dump(file_exists('include/connect.php'));
var_dump(file_exists('include/console.class.php'));
?>
<div id="mainpage" class="main-container inner">
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = "console_overview";
}
?>
</div>
<!-- end: MAIN CONTAINER -->
<script>
var page = "<?php echo $page;?>";
$( "#mainpage" ).load( page + ".php" );
</script>
I've used var_dumps to check if both file exists (and they do). The console_overview.php loads correctly. Now in the console_overview.php I'm trying to get data from the Console class, as following:
<?php
foreach(Console::getAllConsoles() as $aConsole) {
$consoleID= $aConsole->getID();
$consoleName = $aConsole->getName();
$consoleHostname = $aConsole->getHostname();
$consoleGameID = $aConsole->getGameID();
$consolePowerState = $aConsole->getPowerState();
echo "<tr>";
echo "1";
}
?>
The error I'm getting is caused by the foreach... but I can't find out what's wrong...
The Console class looks like this (I'm pasting the most important parts, otherwise the code would be too long).
<?php
class Console{
private $ID, $hostname, $mac, $ip, $roomID, $gameID, $register, $powerState, $dateUpdated;
public function Console($tID, $tHostname, $tMac, $tIp, $tRoomID, $tGameID, $tRegister, $tPowerState, $tDateUpdated) {
$this->ID = $tID;
$this->hostname = $tHostname;
$this->mac = $tMac;
$this->ip = $tIp;
$this->roomID = $tRoomID;
$this->gameID = $tGameID;
$this->register = $tRegister;
$this->powerState = $tPowerState;
$this->dateUpdated= $tDateUpdated;
}
...
public static function getAllConsoles() {
$sql = "SELECT * FROM `console` ORDER BY `hostname` ASC";
$result = mysql_query($sql);
$theResults = array();
while ($row = mysql_fetch_array($result)) {
$theResults[] = new Console($row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],$row[8],$row[9],$row[10]);
}
return $theResults;
}
}
?>
So can anyone see what the problem is?
Thank you for your help.
Edit: O and yes, I know MySQL is deprecated and will change this whenever the issue of not finding the console is fixed =).
Your console_overview.php does not include the required files. When you make an AJAX call with JavaScript from the client it is a separate HTTP request to the server, so you have to add the require() call again there:
<?php
require_once('include/connect.php');
require_once('include/console.class.php');
foreach(Console::getAllConsoles() as $aConsole) {
$consoleID= $aConsole->getID();
$consoleName = $aConsole->getName();
$consoleHostname = $aConsole->getHostname();
$consoleGameID = $aConsole->getGameID();
$consolePowerState = $aConsole->getPowerState();
echo "<tr>";
echo "1";
}
?>
I am new w/ OPP and big pardon if my question maybe too simple :)
Table category, navigation, etc contains multiple rows (category : samsung, apple, etc; and navigation : about us, terms, etc) and both stand as Menu in all pages (home, product,etc)
My old php code and work good is below
<div id="categories">
<ul>
<?
$mydbcategories = new myDBC();
$resultcategories = $mydbcategories->runQuery("SELECT * FROM `category`");
while ($rowcategories = $mydbcategories->runFetchArray($resultcategories)) {
echo '<li>'.$rowcategories[title].'</li>';
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$mydbnavigation = new myDBC();
$resultnavigation = $mydbnavigation->runQuery("SELECT * FROM `navigation`");
while ($rownavigation = $mydbnavigation->runFetchArray($resultnavigation)) { echo '<li>'.$rownavigation [title].'</li>';
}
?>
</ul>
</div>
I would like to implement OOP PHP and create class then store in classes.php
<?
class Menu{
var $title;
var $url;
function setMenu($db){
$mydbMenu= new myDBC();
$resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
$resultmenurows = mysqli_num_rows($resultmenu);
while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)){
$this->title = $rowmenu[title];
$this->url = $rowmenu[url];
}
}
function getTitle() { return $this->title;}
function getUrl() { return $this->url;}
}
?>
Then i'm edit my old code with new one below;
<div id="categories">
<ul>
<?
$catmenu = new Menu();
while ($catmenu ->setMenu('category')) {
echo '<li>'.$catmenu->getTitle().'</li>';
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$navmenu = new Menu();
while ($navmenu ->setMenu('category')) {
echo '<li>'.$navmenu ->getTitle().'</li>';
}
?>
</ul>
</div>
I tested and error maybe because there are multiple rows (from table) in the setMenu func.
How can i return this multiple rows ? should i use array ?
Please help me to solve this and any reply really very appreciate
You are coding PHP4 OOP style, this is very outdated. Don't use var, use public, protected, private.
$this->title = $rowmenu[title] in here, title is used as a constant (no quotes), proper: $this->title = $rowmenu['title'], same with $rowcategories[title]
"SELECT * FROM $db" is this correct? Or do you mean SELECT * FROM menu WHERE xxx='" . $db . "', do you catch errors if the lookup fails?
You should also look at PHP design patterns and code style to improve!
Try following PHP code
<?
class Menu {
var $title;
var $url;
function setMenu($db) {
$mydbMenu = new myDBC();
$resultmenu = $mydbMenu->runQuery("SELECT * FROM `$db`");
$resultmenurows = mysqli_num_rows($resultmenu);
$this->title = array();
$this->url = array();
while ($rowmenu = $mydbMenu->runFetchArray($resultmenu)) {
$this->title[] = $rowmenu['title'];
$this->url[] = $rowmenu['url'];
}
}
function getTitle($ind) {
return $this->title[$ind];
}
function getUrl($ind) {
return $this->url[$ind];
}
}
?>
And HTML
<div id="categories">
<ul>
<?
$catmenu = new Menu();
$catmenu->setMenu('category');
$i = 0;
while ($catmenu->getTitle($i)) {
echo '<li>' . $catmenu->getTitle($i) . '</li>';
$i++;
}
?>
</ul>
</div>
<div id="navigation">
<ul>
<?
$navmenu = new Menu();
$navmenu->setMenu('navigation');
while ($navmenu->getTitle($i)) {
echo '<li>' . $navmenu->getTitle($i) . '</li>';
$i++;
}
?>
</ul>
</div>
I am making a module and trying to figure out how to get the Search engine friendly url to articles from this module
this is the helper class today
public function getItems($amount)
{
$db = &JFactory::getDBO();
$query = 'SELECT * FROM `#__content`, `#__content_frontpage` WHERE `#__content_frontpage`.content_id = `#__content`.id AND `#__content`.state = 1 ORDER BY `#__content`.publish_up DESC LIMIT ' . $amount . '';
$db->setQuery($query);
$items = ($items = $db->loadObjectList())?$items:array();
return $items;
} //end getItems
And this is the default.php to display stuff
<ul class="frontpage_news">
<?php foreach ($items as $item) { ?>
<li>
<div class="frontpage_date"><?php echo JText::sprintf('DATE_FRONTNEWS', $item->publish_up); ?></div>
<div id="ffTitle" class="frontpage_title"><?php echo JText::sprintf('TITLE_FRONTNEWS', $item->title); ?></div>
<div id="ffRead" class="frontpage_readmore"><?php echo JText::sprintf('READ_MORE_FRONTNEWS'); ?></div>
</li>
<?php } ?>
</ul>
So how do I get the correct link to each article displayed in SEF format?
Thanks for any help!
For Joomla 1.5:
echo JRoute::_(ContentHelperRoute::getArticleRoute($article_id_and_alias, $category_id_and_alias, $section_id));
For Joomla 1.6/1.7:
echo JRoute::_(ContentHelperRoute::getArticleRoute($article_id_and_alias, $category_id));