Catch dynamic page without use database ID - php

This is my content.php. I want to show sectors/sector_id=1.php if some one click one my side bar sectors/sector_id=1.php then sector pass an ID 1 then it shows some content then another menu then it goes to another page, but it shows only one page sector_id=0.php and when ID is 1 or 2 it shows sector_id=0.php this page:
<?php
if($id==1) include('sectors/sector_id=1.php');
if($id==2) include('sectors/sector_id=0.php');
?>
This is my side bar
<ul id="sector-nav" class="nav">
<li >
<!--Fibre-->
Fibre
</li>
<li >
<!--Hand Protection-->
Hand Protection
</li>
<li >
<!--Purification Products-->
Purification Products
</li>
<!-- others <li>'s -->
</ul>

The problem here is how you're trying to pass the variables. If you're Link is:
Fibre
This will not get through to your PHP the way you want. I would advise creating the link like so:
Fibre
When this link is followed, sectors.php will be able to get that value from $_GET['id'] variable. You can then do:
<?php
if(isset($_GET['id'])){
include('sectors/sector_id=' . $_GET['id'] . '.php');
}
?>
Now, if you do something like:
Fibre
This can be used too, but will generate a NULL value at the index called 1, $_GET['1']. So you could grab the Key versus the value if you wanted.

Related

PHP advice with defining a GET Method

I have a navigation like this:
<div class="navigation" id="<?php echo $page ?>">
<ul>
<li>`<a class="home" href="index">Home</a>`</li>
<li>`<a class="training-gallery" href="training-gallery">Training Gallery</a>`</li>
<li>`<a class="products-and-merchandise" href="products-and-merchandise">Products & Merchandise</a><`/li>
<li>`<a class="facebook-page" href="facebook-page">Personal Training on Facebook</a>`</li>
<li>`<a class="personal-training-forum" href="client-feedback">Client Feedback</a>`</li>
<li>`<a class="dannys-blog" href="dannys-blog">Danny's Blog</a>`</li>
</ul>
</div>
in my page, I have put the following line of code at the top of the page of a page called training gallery:
<?php $page ='training-gallery' ?>
On my website when I inspect the element using firebug, I get a message like:
Notice: Undefined variable: page in /Applications/XAMPP/xamppfiles/htdocs/Websites/twd/dtpt/includes/navigation.php on line 1 on the link of the training gallery page.
I am assuming I need a GET method instead of this simple bit of code to say that if you are on the training-gallery page, put the name of the page training-gallery on the navigation id line in the code. by defining where the page is. Hopes this makes sense.
Yes, you do need a GET parameter :-)
In the link, you can specify: <a href="?page=home
and in php
<?php
$page = (isset($_GET['page'])) ? $_GET['page'] : '';
this will check, whether the $_GET['page'] exists and if not, it will set the $page to empty string...

Check foreach statement values

I have a code like this.
<ul>
<?php foreach (getUserMainMenus($get_user_id) as $get_main_menu): ?>
<li class='has-sub'><a href='#'><span><?=$get_main_menu['menu_name']; ?></span></a>
<ul>
<?php foreach (getUserChildMenu($get_main_menu['m_id'], $get_user_id) as $sub_menu): ?>
<li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a>
<ul>
<?php foreach (getUserSubChildMenu($sub_menu['m_id'], $get_user_id) as $sub_sub_menu): ?>
<li class='has-sub'><a href='<?=$sub_sub_menu['menu_url']; ?>'><span><?=$sub_sub_menu['menu_name']; ?></span></a></li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
And the result set for the second foreach is like this.
Array ( [m_id] => 9 [menu_name] => MenuName [parent_menu_id] => 4 [menu_order] => 1 [menu_url] => menu1.php [status] => 1 )
How to check a if condition after all foreach statement.
In other words, i need to get the $sub_menu values and if there is values show the li tag.
Also - I need to check the $sub_sub_menu variable, and if there is values show the li tag.
How can i achieve that?
Thanks,
Kimz
I guess you try to create a navigation menu. Where the sub entries should only appear when the top menu item is selected by the visitor of your page.
Is that right?
Ok if so. You might have in mind that.
if a user displays your page first. you might show only the top menu items.
if then a user selects one of the top menu items he/she clicks on a link an that reloads your script with some additional information.
Now your script needs to figure out which top menu item the user selected based on the additional information.
Depending on the selection of the user you might show or hide submenu items.
What your job here is, you have to make sure that your script detects which top-menu item is clicked at.
Do you need more help, or is it clear what to do?
Ok how about this as an basic example for dynamic php menus as test.php
<?php
$menu="";
extract( $_GET, EXTR_PREFIX_ALL, "url" );
if (isset($url_menu)){
$menu=$url_menu;
echo "you selected ".$menu."<br>";
}
echo "<ul>";
// top menu 1
echo '<li>Top1';
if ($menu=="top1"){
echo "<ul>";
echo "<li>Submenu</li>";
echo "</ul>";
}
echo "</li>";
// top menu 2
echo '<li>Top2';
if ($menu=="top2"){
echo "<ul>";
echo "<li>Submenu</li>";
echo "</ul>";
}
echo "</li>";
echo "</ul>";
?>
See any top menu item hands over the additional variable "menu". This is either "top1" or "top2" in this case. Now your script on reload checks whether "menu" is already set and depending on the value of "menu" it shows the corresponding sub menu.
There is still a long way to go, because in my case I use fixed menu items where in your case you load the menu items depending on the "userid".
Let me know if the example above works at your place and if you need additional support to adopt it to your dynamically loaded menus.
Following that idea you need to replace
<li class='has-sub'><a href='#'><span><?=$get_main_menu['menu_name']; ?></span></a>
by adding for example the variable name "level0"
<li class='has-sub'><a href='<?= ?level0=$sub_menu['menu_name']; ?>'><span><? $get_main_menu['menu_name']; ?></span></a>
then you can check in you sub menu if "level0" is set as you expect it and then show or hide the sub menu items.

See if one item in array is true

I'm trying to put a menu together where it will sense what page is loaded. This site is not done in a CMS and is straight PHP/HTML code.
I currently have the navigation working for the primary links. There is a dropdown where I am having problems. I need to be able to see if the parent or any dropdown children are active. If one of the children are active. In the example below this is "FAQ" and the children are "FAQ1," "FAQ2," and "FAQ3."
For this example I'm using a CSS state called "active."
<style>
a{color:red;}
.active{color:blue;}
</style>
Here is the script used for the menu. The links for Home, Products, and Contact are working as expected.
<ul>
<li><a href="index.php" id="homenav" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?>>Home</a></li>
<li><a href="products.php" id ="prodnav" <?php if (strpos($_SERVER['PHP_SELF'], 'products.php')) echo 'class="active"';?>>Products</a></li>
<li><a href="faq.php" id ="faqnav" <?php if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php')) echo 'class="active"';?>>FAQ</a>
<ul>
<li>FAQ1</li>
<li>FAQ2</li>
<li>FAQ3</li>
</ul>
</li>
<li><a href="contact.php" id ="connav" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="active"';?>>contact us</a></li>
</ul>
Can I please get some help on how I should be writing this line to let it work the way the others are?
<li>
<a href="faq.php" id ="faqnav"
<?php
if (strpos($_SERVER['PHP_SELF'], 'faq.php, faq1.php, faq2.php, faq3.php'))
echo 'class="active"';
?>
>FAQ</a>
</li>
strpos() only accepts one string per parameter, you can not give it a list.
Try this:
<?php if (in_array(basename($_SERVER['PHP_SELF']), array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php'))) echo 'class="active"';?>
basename() strips the path from the filename, so you only have the pure file name
in_array() then checks if this path is in an array
array() generates an array of strings to be handed over to in_array(). Note that there are 4 separate strings, not one long one as in your code.
Use in_array for this purpose:
if (in_array($_SERVER['PHP_SELF'], array('faq.php', 'faq1.php', 'faq2.php', 'faq3.php')) echo 'class="active"';
http://php.net/manual/de/function.in-array.php
If you don't have any other pages that contain faq, you can simply use:
if (strpos($_SERVER['PHP_SELF'], 'faq') !== false)
Note that I am using !== false as strpos can return 0 when faq is found at the beginning of your string. You should probably use that for your other comparisons too.
Otherwise, go with the in_array solution of #MrTweek.

Calling an array of config items in CodeIgniter

I am new to CodeIgniter so I'm just trying to create a pretty basic site. I have 4 controllers/pages that I want to load, and a possibility to add a few more.
I have an array of items in my /applications/config/site.php file (which is autoloaded) as shown:
$site['MenuItems']['Home'] = "http://mysite.com/site/home";
$site['MenuItems']['Network Info'] = "http://mysite.com/site/info";
$site['MenuItems']['Staff'] = "http://mysite.com/site/staff";
$site['MenuItems']['Support'] = "http://mysite.com/site/support";
$config['site'] = $site;
I want to be able to take the $site['MenuItems'] array and echo out key/value pairs to place ultimately into my view page so that they are displayed as links on my site in the header. I want to be able to add and subtract items from this $site['MenuItems'] array as I need to to create more links in my header.
For example, in my view if I were to echo out the 'Home' => "http://mysite.com/site/home" key value pair:
<li>
Home
</li>
I'm not sure if I use $this->config->load('site','MenuItems') to do this...or what?
Thanks for any help you can provide me. Let me know if I'm missing something. It's probably something incredibly easy and I just can't grasp it right now :(
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('site');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks['MenuItems'] as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>
try this
Controller's code:
$data['MyVarsArray'] = "That's my menu!";
$data['MyLinks'] = $this->config->item('MenuItems');
$this->load->view('myview',$data);
myview.php code:
<h2><?=$MyVarsArray?></h2>
<ul>
<?php
foreach($MyLinks as $key=>$value){?>
<li>
<?=$key?>
</li>
<?}
?>
</ul>

Adding a Class to PHP Menus

I have a small php website that has a menu that is included via include_once. I need an HTML class added to the current menu item for each page.
So if a user clicks on a link and goes to that page the new page will load with the new menu item with class="current". How can I go about doing this?
Let's say you write your menu like this:
<ul>
<li> Link1 </li>
<li> Link2 </li>
<li> Link3 </li>
</ul>
Here is a good old php4 style solution :-p
<?php
//you get which link was clicked
if(isset($_GET['menu']) $menu = $_GET['menu'];
else $menu = 1;
$1 = $2 = $3 = ""; //you create 3 empty strings
switch($menu){ //you assign the correct string with your css class name
case "1": $1 = 'class="current"';break;
case "2": $2 = 'class="current"';break;
case "3": $3 = 'class="current"';break;
}
?>
//echo the variables into the menu anchors ( <?=$1?> is equivalent to <? echo $1; ?> )
<ul>
<li><a href="link1.com?menu=1" <?=$1?> >Link1</a></li>
<li><a href="link2.com?menu=2" <?=$2?> >Link2</a></li>
<li><a href="link3.com?menu=3" <?=$3?> >Link3</a></li>
</ul>
You can also place the <?=$1?> into the <li> or anywhere else wanted...
If you're using javascript in your project, you could also give an id to your anchors (like Link1), get the menu value like above with $_GET, and then use javascript to add the class to the desired link.
I gave you this answer because from what you're writing I guess you're not using any PHP framework but coding you app the old way... there's plenty of nice PHP framework around here that will have pre-made solution for this... might be hard at the beginning but it's worth it...
Good luck!

Categories