How to set "class = active" for second tab? - php

I have some tabs. In that sometimes first tab is "introduction" and sometimes "outline" is first tab. I have set introduction class=active when it comes first. But when outline comes first I am not getting how to set its class as active.
code is like below
<ul class="nav-tabs" role="tablist">
<?php
$tabs = array();
if(!$sessId == "" && !$checkcourse){
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>true, "comment"=>true);
} else if($sessId == "") {
$tabs = array("introduction"=>true, "outline"=>true,"announcement"=>false,"discussion"=>false,"review"=>true, "student"=>false, "comment"=>true);
} else {
$tabs = array("introduction"=>false, "outline"=>true,"announcement"=>true,"discussion"=>true,"review"=>true, "student"=>true, "comment"=>false);
}
?>
<?php if($tabs['introduction']) { ?>
<li class="active">Introduction</li>
<?php } ?>
<?php if($tabs['outline']) { ?>
<li>Outline</li>
<?php } ?>
<?php if($tabs['review']) { ?>
<li>Review</li>
<?php } ?>
<?php if($tabs['student']) { ?>
<li>Student</li>
<?php } ?>
<?php if($tabs['comment']) { ?>
<li>Comment</li>
<?php } ?>
<?php if($tabs['announcement']) { ?>
<li>Announcement</li>
<?php } ?>
<?php if($tabs['discussion']) { ?>
<li>Discussion</li>
<?php } ?>
</ul>

Simple check with ternary operator is:
<?php if($tabs['outline']) {?>
<li <?=$tabs['introduction']? '' : ' class="active"';?>>Outline</li>
<?php } ?>
So you check if $tabs['introduction'] isset then you don't need class="active", else - add this class.
Update:
But as first item of tab can change, I advise to create simple foreach:
$is_first = true;
foreach ($tabs as $tab_name => $value) {
if ($value) {
echo '<li' . ($is_first? ' class="active"' : '') . '>' . $tab_name . '</li>';
$is_first = false; // fix that we have first value here
}
}
Here your main problem is how to link href value and caption.

Related

How to add html inside an if else statement in php?

I have this code where if something then display a link, else display another link. the if and else are php code, and the links are html as you will know, but I was wondering how do you make it so it will not give me any errors, how do I combine php with html?
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
Leave Society;
} else {
Join Society;
}
This might help you. This one is from the basics of PHP:
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "<a href='file.php' class='socbuttons'>Leave Society</a>";
} else {
echo "<a href='anotherfile.php' class='socbuttons'>Join Society</a>";
}
}
?>
Remember PHP should be surrounded by <?php ?>. Simply end the "PHP part" and then start it again when you finished your HTML.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
?> Leave Society <?
} else {
?> Join Society<?
}
Also, you can write HTML in echo statement, but you should escape some special characters like " to not interfere with the php code itself.
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "Leave Society";
} else {
echo "Join Society";
}
See http://php.net/manual/en/function.echo.php to more information.
Try this
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) { ?>
Leave Society;
<?php } else { ?>
Join Society;
<?php }
Use alternate syntax:
<?php
$aUserSocs = array( 'link1', 'link2', 'link3' );
$soca = 'link2';
$iCountUserSocs = count( $aUserSocs );
for( $i = 0; $i < $iCountUserSocs; ++$i ):
?>
<?php if( $soca == $aUserSocs[ $i ] ): ?>
Leave Society;
<?php else: ?>
Join Society;
<?php endif; ?>
<?php endfor; ?>

How can I make a change in the header while I'm using the PHP include?

I'm using include for the header
and it's have this
<li><span>Arrangements</span></li>
<li><span>Build-a-Bouquet</span></li>
<li><span>Special Events</span></li>
and I want only any current opened page get the class="current"
like this
<li class="current"><span>Build-a-Bouquet</span></li>
You can do it like this:
<?php
$menus = array();
$menus['Arrangements'] = 'arrangements.php';
$menus['Build-a-Bouquet'] = 'bouquet.php';
$menus['Special Events'] = 'events.php';
?>
<ul>
<?php
$currnet_page = $_SERVER['PHP_SELF'];
if (! empty($menus)) {
foreach ($menus as $menu => $menu_link) {
$isCurrent = ($currnet_page == $menu_link) ? 'current' : '';
?>
<li class="<?php echo $isCurrent;?>"><span><?php echo $menu;?</span></li>
<?php
}
}
?>
</ul>
Try something like this:
$active = "bouquet";
include("navigation.php");
Navigation:
<li<?php if( isset($active) && $active == "arrangements") echo " class='active'";?>><span>Arrangements</span></li>
... And so on.
This is just a quick example, $_SERVER['PHP_SELF'] can do the job.
<li><span>Arrangements</span></li>
<?php
if($_SERVER['PHP_SELF'] == '/bouquet.php'){
echo '<li class="current"><span>Build-a-Bouquet</span></li>';
}else{
echo '<li><span>Build-a-Bouquet</span></li>';
}
?>
<li><span>Special Events</span></li>
There are a few ways you can do it. Choose the one you like:
1)
Set $current_ var you want and use:
<li><a<? echo isset($current_arrangements) ? 'class="current"' : ''; ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a<? echo isset($current_bouquet) ? 'class="current"' : ''; ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a<? echo isset($current_special_events) ? 'class="current"' : ''; ?>href="events.php"><span>Special Events</span></a></li>
2) same as above but use one variable and check for its value (exactly the way Niet the Dark Absol wrote)
3) wrap it to some function
<?
// Echo Class If Current
function ecic($page) {
echo eregi($page, $_SERVER['PHP_SELF']) ? 'class="current"' : '';
}
?>
<li><a <? ecic('arrangements'); ?>href="arrangements.php"><span>Arrangements</span></a></li>
<li><a <? ecic('bouquet'); ?>href="bouquet.php"><span>Build-a-Bouquet</span></a></li>
<li><a <? ecic('events'); ?>href="events.php"><span>Special Events</span></a></li>
BTW: In most common PHP configurations you can use just <? instead of <?php.

foreach() executing same line again and again

I have a sql field wehre values are like
1,2,3,4,5,6
if fetch those values and explode them below
$amenities = 1,2,3,4,5,6
$amenities_check = explode( "," , $amenities );
Then I run a foreach loop
<?php
$i = 1;
foreach($amenities_check as $amenities_conf)
{
if( $amenities_conf != "" && $amenities_conf == 6)
{
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
else
{
?>
<li class="not_amen">Smoking Allowed</li>
<?php
}
if ($i++ == 1) break;
}
?>
Now the problem is that this loop is display the same line 6 times and display the correct class of li when it meets the 6 digit in the data if i apply $i++ it only check the first data in the dataset.
Any help that foreach look for the desired values like 1 2 3 and do the function according to it ..
Thanks
Is this what you are looking for?
<?php
foreach($amenities_check as $amenities_conf) {
if ($amenities_conf == 6) {
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
else {
?>
<li class="not_amen">Smoking Allowed</li>
<?php
}
}
?>
EDIT:
<?php
foreach($amenities_check as $amenities_conf) {
if ($amenities_conf == 6) {
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
}
?>
<li class="not_amen">Smoking Allowed</li>
or like the guys above said, use in_array().
<?php
if (in_array("6", $amenities_check)) {
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
?>
<li class="not_amen">Smoking Allowed</li>
From the comments, what you actually want to know is if there is a 6 anywhere in the array, so you don't want to be outputting anything at all in the loop. The simplest approach is to use the fairly self-explanatory in_array function; as simple as this:
if( in_array('6', $amenities_conf) )
{
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
else
{
?>
<li class="not_amen">Smoking Allowed</li>
<?php
}
Alternatively, break apart your logic from your display by looping through the database result once and preparing structured data based on it. The below is probably over-kill for this example, but shows a general approach:
// While retrieving data from the DB
$amenity_names = array(
1 => 'pets',
// ...
6 => 'smoking'
);
$included_amenities = array();
$amenities_check = explode( "," , $amenities );
foreach ( $amenities_check as $amenity )
{
$name = $amenity_names[$amenity];
$included_amenities[$name] = true;
}
// In the View / template code
if( $included_amenities[['smoking'] )
{
?>
<li class="available_amen">Smoking Allowed</li>
<?php
}
else
{
?>
<li class="not_amen">Smoking Allowed</li>
<?php
}

Displaying menu names from CMS

I have been trying to display a menu name from my CMS database that I created myself, I got it displayed perfectly but there is a really weird error, I spent many hours to discover the solution but I couldn't.
The problem is that I want to display the menu name as page H1 in line 50 exactly so whenever you click on the menu name its also displaying as H1 title.
The menu name is displaying correctly, but the sub menu(pages) displaying N instead of the page name, I don't know where is that N is coming from.
NOTE: There is no problem with the database connection or retrieving data.
This is my content page code:
<?php include("includes/header.php"); ?>
<?php require_once 'includes/db_connection.php'; ?>
<?php require_once 'includes/b_functions.php'; ?>
<?php require_once 'includes/cms_constants.php'; ?>
<?php db_connect();?>
<?php
if (isset ( $_GET ['subj'] )) {
$sel_subject = get_subject_by_id ( $_GET ['subj'] );
$sel_page = "NULL";
} elseif (isset ( $_GET ['pages'] )) {
$sel_subject = false;
$sel_page = get_page_by_id ( $_GET ['pages'] );
} else {
$sel_subject = "NULL";
$sel_page = "NULL";
}
?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subject["id"]) { echo " class=\"selected\""; }
echo "><a href=\"contents.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
echo "<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page["id"]) { echo " class=\"selected\""; }
echo "><a href=\"contents.php?pages=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if (!is_null($sel_subject)) { // subject selected ?>
<h2><?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif (!is_null($sel_page)) { // page selected ?>
<h2><?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { // nothing selected ?>
<h2>Select a subject or page to edit</h2>
<?php } ?> </td>
</tr>
</table>
<?php require_once 'includes/footer.php';?>
Your 'N' is coming from the "NULL" string. I believe you meant to set your variables with actual NULL and not a string. Like:
$var = NULL;
As opposed to:
$var = "NULL";
When you access a variable that is set to "NULL" as an array, you will get the 'N' (as the string is treated as an array). For instance;
$var = "NULL";
echo $var[0]; // 'N'
Furthermore, if you test for NULL on a string it will return false (the variable is non-null);
$var = "NULL";
if($var) {
echo "var is: " . $var;
}
else {
echo "var is NULL!";
}
The above should output "var is: NULL".
I say this from experience (I did this when I started coding PHP). However, the root of your problem likely doesn't stop there as your variables should be set in subsequent conditionals. I would go through your while statements, and if statements and ensure your variables are being set appropriately. (var_dump($var) or print_r($var) through suspect blocks).

How to optimize menu navigation

I'm building a menu options, having issue in last option, The Anchor method doesn't work as a link popup a new window. Besides, in option 1 and 2, I repeat those codes which is not look great.
Is there a better way to optimize those codes? make it cleaner.
In my controller:
public function loadPage($name, $pageID) {
$data['title'] = $this->tabPageData;
$data['tabMenu'] = $this->model->getAllMenuItems();
if ($name == 'portfolio-1') {
// load portfolio 1, get the page content (photos) and its name
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'portfolio-2') {
$data['tabPageContent'] = $this->model->getPageContentByPageID($pageID);
$data['pageName'] = $this->model->getPageNameByID($pageID);
} elseif ($name == 'contact') {
// load Contact page
$data['tabContact'] = $this->model->getContactByPageID($pageID);
} else {
// load a Blog site
echo anchor('http://mysite.tumblr.com', 'target=_blank');
}
$this->load->view('content', $data);
}
In my View:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item) : ?>
<?php
$url = "<li><a href='" . base_url();
$url .= str_replace("+", "-", urlencode(strtolower($item->name))) . "/". ($item->cat_id) . "'>";
$url .= strtoupper($item->name) . "</a></li>";
echo $url;
?>
<?php endforeach; ?>
</ul>
</div> <!-- end of Menu -->
I would suggest that you clean up your view by creating a helper method that generates a list item for your navigation.
Put the following code in a file named navigation_helper.php in application/helpers/.
if (!defined('BASEPATH')) exit('No direct script access allowed');
if (!function_exists('build_list_item'))
{
function build_list_item ($item) {
$url_item_name = str_replace('+', '-', urlencode(strtolower($item->name)));
$url = base_url() . $url_item_name . "/". $item->cat_id;
return '<li>' . strtoupper($item->name) . '</li>';
}
}
Make sure you are loading the helper in your controller or autoloading it if you use it often.
$this->load->helper('navigation_helper');
Then in your view you could do this:
<div id="menu">
<ul>
<?php foreach ($tabMenu as $item): ?>
<?php echo build_list_item($item); ?>
<?php endforeach; ?>
</ul>
</div>

Categories