Jump to content
php.lv forumi

php class pievienošana


KalashaLv
 Share

Recommended Posts

Heyy, ir kaut kādā vedā iespējams pievienot class="active"?

Lūk navigation :

<nav>
                <ul>
                    <li><a href="<?=$url;?>">Sākums</a></li>
                    <li><a  href="<?=$url;?>/forums">Forums</a></li>
                    <li><a <?php if(isset($_GET['stats']))   { echo " class=\"active\""; }  ?> href="#">Stati</a></li>
                    <li><a <?php if(isset($_GET['bans']))   { echo " class=\"active\""; }  ?>  href="#">Bani</a></li>
                    <li><a <?php if(isset($_GET['shop']))   { echo " class=\"active\""; }  ?> href="#">Veikals</a></li>
                    <li><a <?php if(isset($_GET['contacts']))   { echo " class=\"active\""; }  ?> href="<?=$url;?>?contacts">Kontakti</a></li>
                
                </ul>
            </nav>

 

lūk kur izlasa lapas :

 

if(isset($_GET['poll'])) {    $link = 'admin/add-poll.php';
                                $vieta = "poll";
                                $title = "Aptaujas labošana";}
else if(isset($_GET['admin'])) {$link = 'admin/admin.php';
    $title = 'Admina kontroles panelis';
    $vieta = "admin";}
else if(isset($_GET['add_img'])) {$link = 'admin/add-img.php';
    $title = 'Pievienot jaunumu bildi';
    $vieta = "add_img";}
else if(isset($_GET['add_ban'])) {$link = 'includes/bans.php';
    $title = Bani';
    $vieta = "bans";}
    else if(isset($_GET['contacts'])) {$link = 'includes/contacts.php';
    $title = 'Kontakti';
    $vieta = "contacts";}

  else if(isset($_GET['contacts'])) {$link = 'includes/stats.php';
    $title = 'Stati';
    $vieta = "stats";}
else {     $link = 'includes/news.php';
        $vieta = "news";
        $title = "Jaunumi";}

 

 

vietās, kur, navigācijā, ir <?php if(isset($_GET['contacts']))   { echo " class=\"active\""; }  ?> tur iet, bet man vajag pivienot, lai rāda "active" Sākumlapā, jo ar to pašu metodi nedarbojas, jo na var $_GET

Link to comment
Share on other sites

pat ar js var...

var url = window.location;
$('ul.nav a[href="'+ url +'"]').parent().addClass('active');

$('ul.nav a').filter(function() {
    return this.href == url;
}).parent().addClass('active');

ka ir ertak js vai jquery bez starpibas ... jo padoma rakstisi 10 if del vienas ficas ja var sadi ?

jo realitate jataisa check kads links ir aktivs un tad tikai zagejam klat aktivo tatad jaraksta vainu funkcija vai class bet kada joka pec ja var isak atrak un vienkarsak ?

Edited by Faks
korekcija
Link to comment
Share on other sites

Vel jau sadaļa Stati / Bani / Veikals satur vienādu linku ...  

... href="<?php echo $url. '/bans'; ?>">Bani</a></li>
$menus = [
    'Sākums' => ''
    , 'Forums' => 'forums'
    , 'Stati' => 'stati'
    , 'Bani' => 'bans'
    , 'Veikals' => 'shop'
    , 'Kontakti' => 'contacts'
];

$urlArray = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $urlArray);
$segment_numb = 1;

$html = '';
foreach ($menus as $menu_name => $menu_link) {
    $link_class = (( strtolower($segments[$segment_numb]) == strtolower($menu_link)) || (!$segments[$segment_numb] && !$html) ) ? 'active' : '';
    $html .= '<li><a href="' . $url . ( $menu_link ? '/' . $menu_link : '' ) . '" class="' . $link_class . '">' . $menu_name . '</a></li>';
}
echo '<nav><ul>' . $html . '</ul></nav>';

 

Edited by zeCode
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...