duanbi7247 2010-11-10 12:15
浏览 42

更好的编码方式?

What would be the best, simplest way to code this: I have a php document that gets most of the page requests (set up in routes config, using code igniter framework) and depending on the uri i want to show the user different content. Example:

http: // domain.tld/2010/  
Should show one type of content

http: // domain.tld/2010-nov/  
should show another type of content

http: // domain.tld/2010-nov-10/  
should show yet another type of content

http: // domain.tld/2010-nov-10-blog-post-title/  
should show once again another type of content

Everything else should be treated as if is a product, example:
http: // domain.tld/light-bulb/
and if such a product doesnt exist, its a 404

Below is the code I got at the moment, but I feel it is way too messy. Any suggestion how to make it simpler and more effective? (Tried to get it formatted correctly here but it seem a bit tricky to get code to align properly, a apologize)

Regards, Jason
(had to add spaces in all my urls here because im new and not allowed to post that many urls)

$val is the uri (/2010-nov.......)

    function show_what($val){
 $arr=array("jan"=>01,"feb"=>02,"mar"=>03,"apr"=>04,"may"=>05,"jun"=>06,"jul"=>07,"aug"=>08,"sep"=>09,"oct"=>10,"nov"=>11,"dec"=>12);
 // first check to see if the uri starts with a year (4 digits)
 if(is_int((int)substr($val,0,4)) && (int)substr($val,0,4)!=0){
  // Get all posts for specified YEAR
  if(strlen($val)==4){
   // Show all blog posts for specified year
   // example: http: // domain.tld/2010/

  // Get all posts for specified YEAR and MONTH
  }elseif(strlen($val)==8 && substr($val,4,1)=="-" && array_key_exists(substr($val,5,3),$arr)){
   // show all blog posts for specified year and month
   // example: http: // domain.tld/2010-nov/

  // Get all posts for specified YEAR, MONTH and DAY OR! get specified post
  }elseif(strlen($val)>=11 && substr($val,4,1)=="-" && array_key_exists(substr($val,5,3),$arr) && substr($val,8,1)=="-" && is_int((int)substr($val,9,2)) && (int)substr($val,9,2)!=0){

   // Get all posts for specified YEAR, MONTH and DAY
   if(strlen($val)==11){
    // show all blog posts for specified year, month and day
    // example: http: // domain.tld/2010-nov-10/

   // Get specified post
   }elseif(substr($val,11,1)=="-"){
    // show specified post or 404
    // example: http: // domain.tld/2010-nov-10-blog-post-title/
   }else{
    // "Not a valid article url<Br/>";
    // example: http: // domain.tld/2010-nov-10there-is-a-dash-missing-after-day/
   }
  }else{
   // 404, not a real date
  }
 }else{
  // show product with current uri or if it doesnt exist, 404.
 }
}
  • 写回答

5条回答 默认 最新

  • doutangxi2144 2010-11-10 12:19
    关注

    you can simple explode it to array

    $array = explode('-',$val);
    

    and make an switch case of the array size like

       switch(count($array){
        # is like 2010
        case 1:
           // Show all blog posts for specified year
           // example: http: // domain.tld/2010/
           $year = $array[0];
        break;
        .....
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能