如果设置了特定的数据库ID,如何设置要加载的特定CSS样式表?

问题描述 投票:0回答:1

我正在 PHP 8.2.12 上的 Codeigniter 3.1.9 中开发一个项目。

我的问题是我有很多动态数据,这些数据被带入静态视图以创建多个页面。我遇到的问题是,如果不创建自己的 PDF 库,或者每个页面都相同,我需要为每个页面实例引入不同的打印样式表。我可以一次性引入所有不同的样式表,因此每次加载页面时它们都会加载,但这感觉有点笨拙且不必要。

因此,我希望的解决方案是使用 PHP 根据拉入的记录动态加载不同的样式表。我已经通过对管理元素和样式进行管理员检查来做到这一点,效果非常好。然而,这些实例加载到固定视图中相当静态的样式表和元素中。

这是配方控制器:

<?php
 
 class Recipe extends BaseController{

  function __construct(){
   parent::__construct();
   
   // Loading in Pagination Library
   $this->load->library('pagination'); 
   
   // Loading in Recipe_model
   $this->load->model('recipe_model');
   
   $uri = $this->uri->uri_string();
   $this->session->set_flashdata('uri', $uri);
  }
    
  function index(){
   $recipe_linkname = $this->uri->rsegment(3);
   if(!$recipe_linkname){
    $recipe_linkname = $this->recipe_model->first_linkname();
   }
   $recipe = $this->recipe_model->read_record($recipe_linkname);
   if(count($recipe) == 0){
    show_404('');
    die();
   }
   $data = array(
    'title' => "Shirley's Recipes:".$recipe['name'], 
    'columns' => array('toc', 'recipe'), 
    'recipe' => $recipe, 
    'updated' => $this->format_date($recipe['updated'])
    );
   $this->load->view('templates/main', $data);
  }
    
  function flipbook(){  
   
   // Requires the user to be "logged in"   
   $this->require_login();  
   
   // Recipe Flipbook Pagination Configuration
   $config['base_url'] = site_url('recipe/flipbook');
   $config['total_rows'] = $this->recipe_model->num_rows();
   $config['uri_segment'] = '3';
   $config['per_page'] = 1;
   $config['num_links'] = 9; // You will see 10 numerical link buttons in paginnation.
   $config['display_pages'] = TRUE;
   $config['full_tag_open'] = '<div id="pagination" class="grid-item3">';
   $config['full_tag_close'] = '</div>'; 

   // Initialize the Pagination Library
   $this->pagination->initialize($config);  
   
   $recipe = $this->recipe_model->read_records($config['per_page'], $this->uri->segment(3))->row_array();
   $data = array(
    'title' => "Shirley's Recipes:".$recipe['name'], 
    'columns' => array('toc', 'admin/page/recipe_flipbook'), 
    'recipe' => $recipe,
    'updated' => $this->format_date($recipe['updated'])
   );   
   $this->load->view('templates/main', $data);
  }
  
  function search(){
   if($this->input->method() === 'post'){           
    $search = $this->input->post('query');  //modify here:        
    $results = $this->recipe_model->search($search);
    $page = $this->uri->segment(3);
    $perPage = 19;    
    $results = $this->recipe_model->search($search, $page, $perPage);
    $data = array(
     'title' => "Shirley's Recipes:Search Results", 
     'columns' => array('toc', 'public/page/search_results'), 
     'recipes' => $results,  // $results is being renamed $recipes. I don't really know why though yet...
     'search' => $search
    );
    $this->load->view('templates/main', $data);
   }
   else{            
    redirect('recipe');
   }
  } 
    
  function add(){
      
   // Requires the user to be "logged in"     
   $this->require_login();

   if($this->input->post('submit')){
    $data = array(
     'id' => $this->input->post('id'),  
     'name' => $this->input->post('name'), 
     'buttontext' => $this->input->post('buttontext'), 
     'linkname' => $this->input->post('linkname'), 
     'time' => $this->input->post('time'),      
     'category' => $this->input->post('category'), 
     'subcategory' => $this->input->post('subcategory'),
     'img' => $this->input->post('img'),
     'keywords' => $this->input->post('keywords'),   
     'ingredients' => $this->input->post('ingredients'), 
     'equipment' => $this->input->post('equipment'), 
     'prepreparation' => $this->input->post('prepreparation'), 
     'directions' => $this->input->post('directions')
    );
    $recipeid = $this->recipe_model->create_record($data);
    redirect('recipe/'.$this->input->post('linkname'));
   }
   else{ // If the IF statement returns false, then  the ELSE statement tells php to do something else.
    $data = array(
     'title' => "Shirley's Recipes:Add New Recipe", 
     'columns' => array('toc', 'admin/edit-page/recipe_edit'), 
     'recipe' => array(
      'id' => '',
      'name' => '', 
      'buttontext' => '', 
      'linkname' => '', 
      'time' => '', 
      'category' => '', 
      'subcategory' => '',    
      'img' => '',
      'keywords' => '',   
      'ingredients' => '', 
      'equipment' => '', 
      'prepreparation' => '', 
      'directions' => ''
     )
    );
    $this->load->view('templates/main', $data);
   }
  }
  
  function edit(){
      
   // Requires the user to be "logged in"     
   $this->require_login();
   
   $id = $this->uri->segment(3);
   if($this->input->post('submit')){
    $data = array(
     'name' => $this->input->post('name'), 
     'buttontext' => $this->input->post('buttontext'), 
     'linkname' => $this->input->post('linkname'), 
     'time' => $this->input->post('time'),    
     'category' => $this->input->post('category'), 
     'subcategory' => $this->input->post('subcategory'),
     'img' => $this->input->post('img'),     
     'keywords' => $this->input->post('keywords'), 
     'ingredients' => $this->input->post('ingredients'), 
     'equipment' => $this->input->post('equipment'), 
     'prepreparation' => $this->input->post('prepreparation'), 
     'directions' => $this->input->post('directions')
    );
    $this->recipe_model->update_record($id, $data);
    redirect('recipe/'.$this->input->post('linkname'));
   }
   else{ // If the IF statement returns false, then  the ELSE statement tells php to do something else.
    $recipe = $this->recipe_model->read_record($id);
    $data = array(
     'title' => "Shirley's Recipes:Edit Recipe : ".$recipe['name'], 
     'columns' => array('toc', 'admin/edit-page/recipe_edit'), 
     'id' => $id, 
     'recipe' => $recipe
    );
    $this->load->view('templates/main', $data);
   }
  }
    
  function delete(){
      
   // Requires the user to be "logged in"     
   $this->require_login();
   
   $this->recipe_model->delete_record($this->uri->segment(3));
   redirect('/');
  } 
 }

 // End of recipe.php

?> 

这是Recipe_model.php

<?php

 class Recipe_model extends CI_Model{

  function create_record($data){
   if(isset($data['buttontext']) and $data['buttontext'] == ''){
    $data['buttontext'] = NULL;
   }
   $this->db->insert('recipe', $data);
   return $this->db->insert_id();
  }
  
  function read_records($limit, $offset){
   $this->db->order_by('name', 'asc');
   if(isset($limit) && isset($offset)){
    $this->db->limit($limit, $offset);
   }
   $q = $this->db->get('recipe');
   return $q;
  }
  
  function read_record($key){
   if(is_numeric($key)){
    $this->db->where('id', $key);
   }
   else{
    $this->db->where('linkname', $key);
   }
   $q = $this->db->get('recipe');
   return $q->row_array();
  }
  
  function first_linkname(){
   $this->db->select('linkname, name');
   $this->db->order_by('name', 'asc');
   $this->db->limit(1);
   $q = $this->db->get('recipe');    
   return $q->row()->linkname;
  }
        
  function read_names(){
   $this->db->select('linkname, name');
   $this->db->order_by('name', 'asc');
   $q = $this->db->get('recipe');
   return $q->result();
  }
  
  function read_names_categories(){
   $this->db->select('buttontext, linkname, name, category, subcategory, img, time');
   $this->db->order_by('name', 'asc');
   $q = $this->db->get('recipe');
   return $q->result();  
  }
    
  function search($search, $currentPage = 1, $perPage = 19){ 
  $offset = ($currentPage = 1) * $perPage;
  $terms = explode(' ', $search); 
  $match = " ";       
  foreach($terms as $term){      
   $match .= $term;
  }       
  $querystr = "SELECT id, name, category, subcategory, keywords, MATCH(name, category,  subcategory, keywords) AGAINST('".$match."') as score FROM recipe WHERE  MATCH(name, category, subcategory, keywords) AGAINST('".$match."') ORDER BY name LIMIT 60 OFFSET 0;";
  $q = $this->db->query($querystr);   
  return $q->result();
 }

  function update_record($id, $data){
   if(isset($data['buttontext']) and $data['buttontext'] == ''){
    $data['buttontext'] = NULL;
   }
   $this->db->where('id', $id);
   $this->db->update('recipe', $data);
  }
  
  function delete_record($id){
   $this->db->where('id', $id);
   $this->db->delete('recipe');
  }
  
  function num_rows(){
   return $this->db->get('recipe')->num_rows();
  }
 }

 // End of Recipe_model.php

?>

这是静态recipe.php视图

<link rel="stylesheet" type="text/css" href="/assets/css/public/page/recipe.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/assets/css/public/page/print/printrecipe.css" media="print" />

<div id="sitecontent" class="grid-item2 grid-container">
<!-- Loads ADMIN items or CSS Stylesheets needed when logged in as ADMIN. -->
 <?php
  if($admin){
   echo '<link rel="stylesheet" type="text/css" href="/assets/css/admin/core/admin.css" />';
   echo '<link rel="stylesheet" type="text/css" href="/assets/css/admin/core/wmd.css" />';
   echo '<link rel="stylesheet" type="text/css" href="/assets/css/admin/page/recipeadmin.css" />';
   echo '<script type="text/javascript" src="/assets/js/wmd.min.js"></script><br>';
   echo '<script type="text/javascript" src="/assets/js/showdown.js"></script><br>';
   echo '<script type="text/javascript" src="/assets/js/confirmdelete.js"></script>';
   $this->load->view('admin/nav/notesnav');
  }
 ?>
 <div id="contents" class="<?php if($admin){echo 'grid-item2';} ?> grid-item1 grid-container scrollbar">
  <div id="spiral" class="grid-item1">
   <img class="spiral" src="/assets/img/lines/spiral1trans.png"/> 
  </div>
  <div id="recipe" class="grid-item2 grid-container">
  <div id="title" class="grid-item1">
   <?=$recipe['name']; ?>
   <br>
   <?=$recipe['time']; ?>
  </div>
  <div id="image" class="grid-item2">
   <?=$recipe['img']; ?>
  </div>
  <div id="ingredients" class="grid-item3">
   <h2>Ingredients:</h2>
   <?php 
    if(isset($recipe['ingredients']) && $recipe['ingredients'] != ''){
     echo $this->mkdn->translate($recipe['ingredients']);
    }
    else{
     echo '<br><br><br><br><br><br><br><br><br><br><br>';
    }
   ?>
  </div>
  <div id="equipment" class="grid-item4">
   <h2>Equipment:</h2>
    <?php 
     if(isset($recipe['equipment']) && $recipe['equipment'] != ''){
      echo $this->mkdn->translate($recipe['equipment']);
     }
     else{
      echo '<br><br><br><br><br><br><br><br><br><br><br>';
     }
    ?>
   </div>
   <div id="prepreparation" class="grid-item5">
    <h2>Pre-Preparation:</h2>
    <?php 
     if(isset($recipe['prepreparation']) && $recipe['prepreparation'] != ''){
      echo $this->mkdn->translate($recipe['prepreparation']);
     }
     else{
      echo '<br><br><br><br><br><br><br><br><br><br><br>';
     }
    ?>
   </div>
   <div id="directions" class="grid-item6">
    <h2>Directions:</h2>
    <?php 
     if(isset($recipe['directions']) && $recipe['directions'] != ''){
      echo $this->mkdn->translate($recipe['directions']);
     }
     else{
      echo '<br><br><br><br><br><br><br><br><br><br><br>';
     }
    ?>
   </div>   
  </div>
 </div> 
 <?php
  if(isset($updated)){
   echo '<div id="updated" class="grid-item2">Updated '.$updated.'</div>';
  }
   if($admin){
    echo '<div id="optionbuttons" class="grid-item noprint">';
    echo anchor('recipe/edit/'.$recipe['id'], img(array('src'=>'assets/img/icons/edit.png', 'alt'=>'Edit', 'title'=>'Edit')));
    echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    echo '<a href="javascript:confirmdelete(\'Delete recipe?\',\''.base_url().'recipe/delete/'.$recipe['id'].'\')">'.img(array('src'=>'assets/img/icons/delete.png', 'alt'=>'Delete', 'title'=>'Delete')).'</a>';
    echo '</div>';
   }
  ?>
</div> 

<?php
  if($admin){
   echo '';   
  }
?>

我可以用 IF 语句来完成这个任务吗?我该如何设置它?

我尝试将样式表设置为单独加载,但是使用我编写的 if 语句,它们无论如何都会加载。

我的 if 语句看起来像这样:

<?php
 if($recipe['linkname'] = 'gallonsizesweettea'){
  echo '<link rel="stylesheet" type="text/css" href="/assets/css/public/page/print/recipes/printgallonsizesweettea.css" media="print" />';
 }
 if($recipe['linkname'] = 'applecobbler'){
  echo '<link rel="stylesheet" type="text/css" href="/assets/css/public/page/print/recipes/printapplecobbler.css" media="print" />';
 }
?>

我在这里缺少什么?预先感谢任何人可以提供的任何帮助!

php css codeigniter-3
1个回答
0
投票

我认为你的编码错误。

你必须这样做

$recipe['linkname'] == 'gallonsizesweettea'

您的代码

$recipe['linkname'] = 'gallonsizesweettea')
不起作用。

 if($recipe['linkname'] == 'gallonsizesweettea'){
  echo '<link rel="stylesheet" type="text/css" href="/assets/css/public/page/print/recipes/printgallonsizesweettea.css" media="print" />';
 }
 if($recipe['linkname'] == 'applecobbler'){
  echo '<link rel="stylesheet" type="text/css" href="/assets/css/public/page/print/recipes/printapplecobbler.css" media="print" />';
 }
© www.soinside.com 2019 - 2024. All rights reserved.