-2

大家好,THX 寻求帮助。我有一种情况,然后我需要从 switch/case 返回值,但我做不到。这很容易,但是???我的代码是这样的。

<? if (is_array($all_data)) { foreach ($all_data as $key => $data) { 
                                $sec = $data->section_id;
                                switch ($sec) {
                                    case 1:
                                        $pref = 'news_detail';
                                        break;
                                    case 3:
                                        $pref = 'article';
                                        break;
                                    case 4:
                                        $pref = 'perestr';
                                        break;
                                };
                            if ($key % 2 == 0) { ?>
                                <div class="news_content">
                                    <?php $href_link1 = base_url() . "pages/". $pref . '/' . $data->id . '/' . str_replace($replace_simbols, '-', $data->title); ?>
                                    <a href="<?=$href_link1.'.php'?>"><img src="<?=base_url();?>assets/uploads/articles/<? echo $data->base_img;?>"/></a>
                                    <a class="news_title_left" href="<?=$href_link1.'.php'?>"><? echo $data->title;?></a>
                                </div>
                            <? } else { ?>
                            <div class="news_content">
                                <?php $href_link2 = base_url() . "pages/". $pref . '/' . $data->id . '/' . str_replace($replace_simbols, '-', $data->title); ?>
                                <a href="<?=$href_link2.'.php'?>"><img src="<?=base_url();?>assets/uploads/articles/<? echo $data->base_img;?>"/></a>
                                <a class="news_title_right" href="<?=$href_link2.'.php'?>"><? echo $data->title;?></a>
                            </div>
                            <? }; }; ?>
                            <? } else echo '<p style="text-align: center;">Нет записей, повторите попытку позже!</p>'; ?>

我需要为我的 $href_link 返回 $pref。其中 $sec = $data->section_id; 是我表中的 SECTION ID (1,2,3...)

如果我有案例 1 (section_id = 1) 我需要 $pref = 'news_detail' 和 $href_link weel

.../pages/news_detail/...

4

1 回答 1

1
 $sec = (int) $data->section_id;
 switch ($sec) {
                                    default:
                                        $pref = "Default";
                                        break;
                                    case  1: 
                                        $pref = 'news_detail';
                                        break;
                                    case  3:
                                        $pref = 'article';
                                        break;
                                    case 4:
                                        $pref = 'perestr';
                                        break;
                                };
于 2013-03-14T22:36:40.940 回答