douliudong8108 2014-01-27 19:04
浏览 74
已采纳

基于带链接的URL的面包屑(PHP,HTML,Codeigniter)

I'm makeing dynamic bredcrumbs based on current url i have. My problem is, that i can't figure out how to generate right link and attach it to word, because of foreach loop. My code is realy messy, so i whould realy need a help. Hope you guys can help!

Tnx

Here is my code:

<?php
        $url = current_url();
        $home = "www.homepage.com";
        $sub_url = substr($url, strpos($url, $home));
        $segments = explode("/", $url);
        $parts = array_slice($segments, 3);
        $count = count($parts);
        $i = 0;
        ?>
       //Don't show breadcrumb on home page
    <?php if (current_url() !== base_url() && current_url() !== base_url() . "home") { ?>
            <div class="breadcrumbs">
                <a href="<?php echo base_url(); ?>"><div class="home">Home</div></a>
                <div class="divider1"></div>
                <?php
                foreach ($parts as $row) {
                    $bread = ucfirst(str_replace("-", " ", str_replace("_", " ", $row)));
                    ?>
                    <div class="level1" style="margin-left: -6px ;">
                       <a href="#"><?php echo $bread." ". $i; ?></a>
                    </div>
                    <?php if ($i < $count - 1) { ?>
                        <div class="divider1"></div>
                        <?php
                    }
                    $i++;
                }
                ?>
            </div>
   <?php }  ?>

EDIT: Right link means if you have url like www.homepage.com/mens_where/mens_jackets/Jacket123

Output should be something like that if i don't consider echo-ing classes in upper code

<a href="www.homepage.com">home</a>
<a href="www.homepage.com/mens_wear">Mens Wear</a>
<a href="www.homepage.com/mens_wear/mens_jackets">Mens jackets</a>
<a href="www.homepage.com/mens_wear/mens_jackets/Jacket123">Jacket123</a>
  • 写回答

2条回答 默认 最新

  • 普通网友 2014-01-27 21:49
    关注

    This is my final solution!

     <?php if (current_url() !== base_url() && current_url() !== base_url() . "home") { ?>
                <div class="breadcrumbs">
                    <?php
                    $segments = $this->uri->segment_array();
                    $last_segment = '';?>
                    <a href="<?php echo base_url(); ?>"><div class="home">Home</div></a>
                    <div class="divider1"></div>
                    <?php
                    foreach ($segments as $segment) {
                        $last_segment .= '/' . $segment;
                        ?>
                     <div class="level1" style="margin-left: -6px ;">
                         <?php
                        echo '<a href="'.base_url() . substr($last_segment,1) . '">' .    ucfirst(str_replace('-', ' ', str_replace('_', ' ', $segment))) . '</a>';
                        ?>
                         </div>
                         <div class="divider1"></div>
                         <?php
                    }
                    ?>
                </div>
            <?php } ?><br>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历