程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP寫緩存

PHP寫緩存

編輯:關於PHP編程

//寫入緩存
   $menu_query = exequery( $connection, " SELECT ct.id as tid, ct.type_name , mb.id, mb.cookname,
       mb.price, mb.description, mb.pinyin FROM cook_type ct LEFT JOIN menubook mb ON ct.id = mb.cook_type WHERE
       mb.id = ".$cook_id);
   $menu_result = mysql_fetch_array( $menu_query );

   //echo "菜單id:".$cook_id.";分類id:".$menu_result[tid];exit;
   //本次菜單
   $row[mid] = $cook_id;
   $row[menu_name] = $cook_name;
   $row[description] = $description;
   $row[menu_price] = $cook_price;
   $row[pinyin] = ChangeToPinyin(iconv("utf-8","gb2312",$cook_name),1);
  
   if( file_exists( "./menubook.php") )
   {
    require_once( "./menubook.php" );
   }
  
  
   if( is_array( $menu_cache) && !empty( $menu_cache) )
   {
    $classnum = count( $menu_cache );
    $unmatchnum = 0;
    for ( $i=0; $i<$classnum; $i++ )
    {
     if( $menu_result[tid] != $menu_cache[$i][tid] )
     {
      $unmatchnum++;
      continue;
     }
     else //找到分類 , 繼續尋找菜品的緩存是否存在
     {
      $cartenum = count( $menu_cache[$i][list]);
      $unmarrynum = 0;
      for ( $j=0; $j<$cartenum; $j++ )
      {
       if ( $menu_cache[$i][list][$j][mid] != $cook_id )
       {
        $unmarrynum++;
         continue;
       }
       else //修改菜單
       {
        $menu_cache[$i][list][$j][menu_name] = $cook_name;
        $menu_cache[$i][list][$j][description] = $description;
        $menu_cache[$i][list][$j][menu_price] = $cook_price;
        $menu_cache[$i][list][$j][pinyin] = $row[pinyin];
       }
      }
     
      //新添加的菜單
      if( $cartenum == $unmarrynum )
      {
       $menu_cache[$i][list][] = $row;
      }
     }
    }
   
    //未找到分類
     if ( $unmatchnum == $classnum )
     {
      $type_row[tid] = $menu_result[tid];
      $type_row[type_name] = $menu_result[type_name];
      $type_row[list][0] = $row;
   
      $menu_cache[] = $type_row;
     
     }
   }
   else { //數組的生成
       $type_row[tid] = $menu_result[tid];
     $type_row[type_name] = $menu_result[type_name];
     $type_row[list][0] = $row;
  
     $menu_cache[] = $type_row;
    
   }
   //print_r( $menu_cache );
   //寫入文件
   ob_start();
   var_export( $menu_cache );
   $content = ob_get_contents( );
   ob_end_clean();
  
   $filename = "menubook.php";
   $fp = fopen( $filename, "w" );
   fwrite( $fp, "<?php $menu_cache = $content ?>" );
   fclose( $fp );

二、添加菜單,寫入緩存

     $str=ChangeToPinyin(iconv("utf-8","gb2312",$cook_name),1);
     $query_cook_add="insert into menubook(cookname,price,cook_type,description,pinyin) values(".$cook_name.",".$cook_price.",".$cook_type.",".$description.",".$str.")";
     $cursor_cook_add=exequery($connection,$query_cook_add);
     $cook_id = mysql_insert_id();
    
     $menutype_query = exequery( $connection, " SELECT type_name FROM cook_type WHERE id = ".$cook_type);
     $menutype_result = mysql_fetch_array( $menutype_query );
    
    
   //本次菜單
   $row[mid] = $cook_id;
   $row[menu_name] = $cook_name;
   $row[description] = $description;
   $row[menu_price] = $cook_price;
   $row[pinyin] = $str;
  
   if( file_exists( "./menubook.php") )
   {
    require_once( "./menubook.php" );
   }
  
   if( is_array( $menu_cache) && !empty( $menu_cache) )
   {
    $classnum = count( $menu_cache );
    $unmatchnum = 0;
    for ( $i=0; $i<$classnum; $i++ )
    {
     if( $cook_type != $menu_cache[$i][tid] )
     {
      $unmatchnum++;
      continue;
     }
     else //找到分類 ,添加改產品菜品的緩存
     {  
        $menu_cache[$i][list][] = $row;
     }
     
    }
    if( $classnum == $unmatchnum ) //未找到分類
    {
      $type_row[tid] = $cook_type;
      $type_row[type_name] = $menutype_result[type_name];
      $type_row[list][0] = $row;
   
      $menu_cache[] = $type_row;
     
    }
   }
   
   else { //數組的生成
       $type_row[tid] = $cook_type ;
     $type_row[type_name] = $menutype_result[type_name];
     $type_row[list][0] = $row;
  
     $menu_c

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved