codeigniter 相关问题

CodeIgniter是由EllisLab公司创建的开源PHP Web开发框架,它已被不列颠哥伦比亚理工学院采用。该框架实现了Model-View-Controller设计模式的修改版本。有关CodeIgniter类,方法,函数,语法和用法的问题,请使用此标记。

CodeIgniter 的 REST_Controller 如何根据 GET、POST、PUT、DELETE 等 HTTP 方法自动将请求路由到控制器方法?

我使用 CodeIgniter 3 和 CodeIgniter REST 服务器库来构建 RESTful API。我有一个扩展 REST_Controller 的控制器。 我注意到 CodeIgniter 自动路由 HTTP

回答 1 投票 0

codeigniter 图像未移动到文件夹:未定义变量 $file_name

查看: 查看: <form method='post' action="<?php echo base_url('Reg_form/save')?>" enctype="multipart/form-data" name="reg_form"> <div class="form-row"> <div class="form-group col-md-3"> <label for="">First name</label> <input type="text" class="form-control" id="fname" placeholder="First Name" name='Fname' required=""> </div> <div class="form-group col-md-3"> <label for="">Last name</label> <input type="text" class="form-control" id="lname" placeholder="Last Name" name='Lname' required=""> </div> <div class="form-group col-md-3"> <label for="">Age</label> <input type="number" class="form-control" id="age" placeholder="age" name='Age' > </div> <div class="form-group col-md-2"> <label for="">Image</label> <input type="file" class="btn btn-success form-control" name="img"> </div> <div class="form-group col-md-1"> <label for=""> Save</label> <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save</button> </div> </div> </form> 控制器: function save() { $this->Reg_model->save(); redirect(base_url('Reg_form/index'), 'refresh'); } 型号: public function save() { // load base_url $this->load->helper('url'); // Check form submit or not $data = array(); // Set preference $config['upload_path'] = './assets/images'; $config['allowed_types'] = 'jpg|jpeg|png|gif'; $config['max_size'] = '100'; // max_size in kb $config['file_name'] = $_FILES['img']['name']; //Load upload library $this->load->library('upload',$config); // File upload if($this->upload->do_upload('img')){ // Get data about the file $uploadData = $this->upload->data(); $file_name = $uploadData['file_name']; $data['response'] = 'successfully uploaded '.$filename; }else{ $data['response'] = 'failed'; } $data = array( 'Name' => $this->input->post('Fname'), 'Fname' => $this->input->post('Lname'), 'age' => $this->input->post('Age'), 'image' => $file_name ); $this->db->insert('student',$data); } 当我尝试上传图像时,它显示未定义的变量 $file_name。 在此处输入图像描述这是显示错误的 codeigniter 的图像 我在将图像文件上传到 CodeIgniter 应用程序中的目标文件夹时遇到问题,并且它根本无法工作。我参考了很多文章并下载了一些可用的文章,但它们在我的应用程序中一次都不起作用 Replace Your Code with code below if($this->upload->do_upload('img')){ // Get data about the file $uploadData = $this->upload->data(); $file_name = $uploadData['file_name']; $data['response'] = 'successfully uploaded '.$file_name; }else{ $data['response'] = 'failed'; }

回答 1 投票 0

Code Igniter 4:没有要更新的数据

我花了几个小时试图找出 CodeIgniter 4 (PHP 8.2) 中的这个错误,我想其他人可能会注意到我错过了什么。 我有一个具有更新功能的模型。当我按原样使用它时,...

回答 1 投票 0

SimpleXMLElement XML 在 CI3 升级到 CI4 后显示为纯文本

我正在将页面从 CI3 升级到 CI4。 该页面有一个按钮,用于打开显示 XML 数据的新选项卡。 由于某种原因,CI4 版本将 XML 显示为纯文本。 我复制了e的来源...

回答 1 投票 0

使用 htaccess 重写我的网址

我正在从事codeigniter项目 我想使用 htaccess 重写我的 URL。我有一个类似 abc.com/main/demo 的 URL,我想将其更改为 abc.com/demo 我只是删除了“主要”。 我怎么能痛...

回答 6 投票 0

在 CodeIgniter 中检索 JSON POST 数据

我一直在尝试从我的 php 文件中检索 JSON 数据。这让我很困难。这是我的代码 我的视图中的代码: var ProductDetails = {'id':ISBNNumber,'qty':finalqty,'price':finalprice,'name':

回答 7 投票 0

控制器行为不稳定。代码点火器 4

我的项目的配置 公共字符串 $baseURL = 'http://cyx.vix.co.in/admin_panel/'; 控制器 主页.php 我的项目的配置 public string $baseURL = 'http://cyx.vix.co.in/admin_panel/'; 控制器 首页.php <?php namespace App\Controllers; class Home extends BaseController { public function index(): string { return view('welcome_message'); } public function login(): string { //return view('welcome_message'); echo "hello"; } } ?> 管理.php <?php namespace App\Controllers; use Codeigniter\Controller; class Admin extends Controller { public function index(): string { return view('welcome_message'); } } ?> 当我尝试时 http://cyx.vix.co.in/admin_panel/home/login 或 http://cyx.vix.co.in/admin_panel/home 或 http://cyx.vix.co.in/admin_panel/home/index Error: 404 File or directory not found. 只有这个网址有效 http://cyx.vix.co.in/admin_panel/ 或 http://cyx.vix.co.in/admin_panel/index.php 加载 codeigniter4 欢迎页面 同样 http://cyx.vix.co.in/admin_panel/Admin 也不起作用。给出 404。 这是我的设置有问题还是与服务器有关。请指教。 以下是在“Views”文件夹中查找文件: return view('welcome_message'); 在您的情况下,它正在寻找 app/Views/welcome_message.php,这就是您收到 404 错误的原因。

回答 1 投票 0

如何检查 CodeIgniter 模型查询是否返回数据?

我想验证数据库查询是否正确执行并返回预期的数据。 我的 SpecializationModel.php 我想验证数据库查询是否正确执行并返回预期的数据。 我的专业化模型.php <?php defined('BASEPATH') or exit('No direct script access allowed'); class SpecializationModel extends Hirer_Model { public function __construct() { parent::__construct(); } public function getTop10Specializations($dateFrom, $dateTo) { $subQuery = "( SELECT applicant_id, jobspec_id, @rn := CASE WHEN @var_applicant_id = applicant_id THEN @rn + 1 ELSE 1 END AS rn, @var_applicant_id := applicant_id FROM ( SELECT @var_applicant_id := NULL, @rn := NULL ) vars, applicant_jobpref ORDER BY applicant_id, jobspec_id DESC )"; $this->db ->select('spec.name, COUNT(sub_table.jobspec_id) AS total') ->from("$subQuery AS sub_table", false) ->join('applicant_personal personal', 'personal.id = sub_table.applicant_id', 'inner') ->join('job_specialization spec', 'sub_table.jobspec_id = spec.id', 'inner') ->where('sub_table.rn <=', 2) ->where('personal.registration_date >=', $dateFrom) ->where('personal.registration_date <=', $dateTo) ->group_by(['sub_table.jobspec_id', 'spec.name']) ->order_by('total', 'DESC') ->limit(10); $query = $this->db->get(); log_message('debug', 'SQL Query: ' . $this->db->last_query()); print_r($query->result_array()); return $query->result_array(); } } 我尝试使用 Specialization.php 控制器 <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Specialization extends Hirer_Controller { public function __construct() { parent::__construct(); $this->lang->load( dir_hirer($this->currentClass), $this->currentLanguage ); $this->load->model(model_hirer('Specialization'), 'specialization'); $this->addCustomCss(url_assets_css($this->currentClass)); } public function index() { $dateFrom = '2019-07-01'; $dateTo = '2023-06-30'; $specializations = $this->specialization->getTop10Specializations($dateFrom, $dateTo); $xValuesJobs = []; $yValuesJobs = []; foreach ($specializations as $specialization) { $xValuesJobs[] = $specialization['name']; $yValuesJobs[] = $specialization['total']; } // JSON encode the data *before* sending it to the view $data = [ 'xValuesJobs' => json_encode($xValuesJobs), 'yValuesJobs' => json_encode($yValuesJobs), ]; // Load page components $this->loadHeader(); // Load the header if (!empty($xValuesJobs) && !empty($yValuesJobs)) { $this->loadContent('graph', $data); // Load graph view with data } else { // Optional: Load a fallback or message when there's no data $this->loadContent('no_data_message', ['message' => 'No data available for the selected period.']); } $this->loadFooter(); // Load the footer } } 我试图在此视图中显示查询结果 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script> <div class="col-md-12 card p-2 my-3"> <canvas id="myChart2" style="width:100%; height:350px;"></canvas> </div> <script> document.addEventListener("DOMContentLoaded", function () { const yLabels = ["Top 1", "Top 2", "Top 3", "Top 4", "Top 5", "Top 6", "Top 7", "Top 8", "Top 9", "Top 10"]; const xValuesJobs = []; const yValuesJobs = []; const barColorsJobs = xValuesJobs.map(() => "#FF8A00"); new Chart(document.getElementById("myChart2"), { type: "bar", data: { labels: xValuesJobs, datasets: [{ backgroundColor: barColorsJobs, data: yValuesJobs }] }, options: { legend: { display: false }, title: { display: true, text: "Top 10 Preferred Specializations" }, scales: { yAxes: [{ ticks: { beginAtZero: true, callback: function (value) { return yLabels[10 - value] || ""; }, stepSize: 1, max: 10 } }] } } }); }); </script> 没有错误,只是我的视图或图形变成白色空白,只显示背景,yLabels不会被渲染,我希望这是因为查询,但我如何检查它是否真的有效? 查询在原始 SQL 中工作,但在这种情况下,我将其转移到代码点火器查询生成器中,我还没有为此创建任何路由,因为这个视图就像我们当前仪表板内的卡片类型,这只是需要显示带有新数据的新模式,我是否还需要在仪表板的路径中配置某些内容,而不仅仅是只关注此 MVC?这个视图是一个模式,可以在租车者/主页中看到,我没有添加任何新路线,因为它已经在租车者/主页中呈现。 我使用 print_r($query->result_array()) 和 log_message() 进行调试,但是我在哪里可以看到这个?我只知道 echo 然后它将显示在视图中,在这种情况下我仍然没有视图或控制器,-我在浏览器中没有看到任何输出”或“日志文件没有显示查询。 查询在原始 SQL 中工作,但在这种情况下,我将其转移到代码点火器查询生成器中,我还没有为此创建任何路由,因为这个视图就像我们当前仪表板内的卡片类型,这只是需要显示一个带有新数据的新模式,我是否还需要在仪表板的路径中配置一些东西,而不仅仅是只关注这个 MVC?这个视图是一个模式,可以在租车者/主页中看到,我没有添加任何新路线,因为它已经在租车者/主页中呈现。 确保在 application/config/config.php 中启用日志记录,并使用 $config['log_threshold'] = 4; 可以使用print_r($query->result_array())直接打印查询结果 public function getTop10Specializations($dateFrom, $dateTo) { .... log_message('debug', 'SQL Query: ' . $this->db->last_query()); print_r($query->result_array()); return $query->result_array(); } 检查你的控制器(Specialization)是否将查询结果传递给视图 public function index() { $dateFrom = '2019-07-01'; $dateTo = '2023-06-30'; $specializations = $this->specialization->getTop10Specializations($dateFrom, $dateTo); if (empty($specializations)) { echo 'No data available for the selected period.'; return; // Prevent rendering a blank graph } echo '<pre>'; print_r($specializations); // Output the data echo '</pre>'; $xValuesJobs = []; $yValuesJobs = []; foreach ($specializations as $specialization) { $xValuesJobs[] = $specialization['name']; $yValuesJobs[] = $specialization['total']; } // Verify data before sending to the view var_dump($xValuesJobs, $yValuesJobs); // JSON encode the data properly for the view $data = [ 'xValuesJobs' => json_encode($xValuesJobs), 'yValuesJobs' => json_encode($yValuesJobs), ]; $this->loadHeader(); $this->loadContent('graph', $data); $this->loadFooter(); } 看起来您的图表的问题是 xValuesJobs 和 yValuesJobs 数据未正确传递到图表。使用 const xValuesJobs = <?= $xValuesJobs; ?>; const yValuesJobs = <?= $yValuesJobs; ?>;

回答 1 投票 0

输入类型点击时不会触发提交

我的代码有问题,我想创建一个上传按钮来上传我的 .xlsx 文件,但即使我尝试单击它也不会触发。 我的视图.php: 我的代码有问题,我想创建一个上传按钮来上传我的 .xlsx 文件,但即使我尝试单击它也不会触发。 我的视图.php : <form action="<?php echo base_url("index.php/fin/cost_control/workingcanvas/import"); ?>" method="post" id="import_form" enctype="multipart/form-data"> <p><label>Pilih File Excel</label> <input type="file" name="file" id="file" required accept=".xls, .xlsx" /></p> <br /> <input type="submit" id="import" name="import" value="Import" class="btn btn-info" /> </form> 我的控制器: public function import(){ include APPPATH.'third_party/PHPExcel/PHPExcel.php'; $excelreader = new PHPExcel_Reader_Excel2007(); $loadexcel = $excelreader->load('excel/'.$this->filename.'.xlsx'); $sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true); $numrow = 1; foreach($sheet as $row){ if($numrow > 1){ array_push($data, array( 'nis'=>$row['A'], 'nama'=>$row['B'], 'jenis_kelamin'=>$row['C'], 'alamat'=>$row['D'], )); } $numrow++; } $this->SiswaModel->insert_multiple($data); redirect("Siswa"); } 还有我的模型: public function insert_multiple($data){ $this->db->insert_batch('siswa', $data); } 我尝试在自动加载上添加一些部分,例如 $autoload['helper'] = array('url','form','文件'); 有什么建议或建议吗?谢谢。 首先在 codeigniter 的自动加载文件中加载表单助手或手动加载 $autoload['helper'] = array('url'); // autoload $this->load->helper->('form'); // manually loading form helper 你最好用这个 $attributes = array('enctype' => 'multipart/form-data'); echo form_open('index.php/fin/cost_control/workingcanvas/import', $attributes); <your upload form code> <?php echo form_close(); ?> 代替标签 更多信息:https://codeigniter.com/user_guide/helpers/form_helper.html

回答 1 投票 0

如何迭代 json 对象并与 html 元素绑定。?

我需要迭代来自控制器的 JSON 对象并将数据绑定到 html 元素。当我发出警报时,我得到了物体。 我如何迭代这些对象来访问

回答 1 投票 0

Codeigniter 分页链接按降序/逆序排列?

我正在按照本教程在 codeigniter 中使用分页库。我已经正确实现了该教程,甚至使用 mysql 按降序显示我的结果。 然而,

回答 2 投票 0

Codeigniter 3 升级会话锁定导致问题

我们最近将旧的 Codeigniter 应用程序从 2.1.0 升级到了 3.1.9,一切都很顺利。除此之外,新的会话锁定导致了问题,我想知道修复的正确方法......

回答 2 投票 0

在此服务器上找不到请求的 URL /clients/login。 Codeigniter 应用程序出现错误

我厌倦了服务器配置。 我有一个 Codeigniter 应用程序,当我将其托管在共享服务器上时,它运行得很好。现在我正在尝试将其托管在我自己的服务器上,我已经完成了所有必需的工作

回答 3 投票 0

codeigniter 中的折扣车

我正在使用 codeigniter 购物车进行在线商务,但我仍然困惑如何制作优惠券,当找到某个商品的优惠券代码时,该优惠券会自动更新总购物车。 谢谢

回答 2 投票 0

数据表类型错误:aData 未定义

我想用表中的数据库值填充数据表。下面是我的代码清单,一切似乎都正常,但返回错误“TypeError:aData is undefined”,因此数据表未创建...

回答 2 投票 0

为什么我的 codeigniter 3 设置一直显示 404 错误?

使用 codeigniter 3 我修改了以下内容。 配置 $config['base_url'] = 'http://demo.xyz.com/xyz/'; $config['log_threshold'] = 4 ; $config['index_page'] = ''; $config['uri_protocol'] = '

回答 1 投票 0

CI4 中 hasChanged() 结果为 true 并且原始值和新值相同是什么?

我不确定我是否完全理解实体。 这是我的代码片段: $feature['id'] = '关系/5466186'; $entity = new \App\Entities\SknowedWinterSportsEntity(); $

回答 1 投票 0

$this->db->escape() 函数在 codeigniter 中添加单引号

在codeigniter中,当我在插入数据时使用函数$this->db->escape()时,它在数据库中添加单引号,任何人都可以帮助我为什么会遇到这个问题吗? 这是我的代码 $数据 = ...

回答 2 投票 0

CodeIgniter 4.5.5 - 未捕获错误:未找到“CodeIgniter\Log\Logger”类 -

我已经使用下载的 zip 文件安装了 CodeIgniter。我正在尝试将谷歌登录集成到应用程序中。 我已经使用下面的代码包含了 google-php-client。 我已经使用下载的 zip 文件安装了 CodeIgniter。我正在尝试将谷歌登录集成到应用程序中。 我已经使用下面的代码包含了 google-php-client。 <?php namespace App\Controllers; include_once(APPPATH.'Libraries/google-api-php-client/vendor/autoload.php'); include_once(APPPATH.'Libraries/google-api-php-client/src/Client.php'); use CodeIgniter\Controller; use Google\Client; class Account extends Controller{ 当我运行应用程序时,浏览器会显示。 此页面目前无法使用 本地主机当前无法处理此请求。 php错误日志显示 [2024 年 12 月 28 日 10:26:17 UTC] PHP 15.include_once() D:\wamp64\www\michelm\system\Autoloader\Autoloader.php:317 [2024 年 12 月 28 日 10:26:17 UTC] PHP 致命错误:未捕获错误:类 找不到“CodeIgniter\Log\Logger” D:\wamp64\www\michelm\system\Config\Services.php:405 还显示 [2024 年 12 月 28 日 10:26:17 UTC] PHP 致命错误:声明 CodeIgniter\Log\Logger::emergency(Stringable|字符串 $message, 数组 $context = []): void 必须兼容 Psr\Log\LoggerInterface::emergency($message, array $context = []) 在 D:\wamp64\www\michelm\system\Log\Logger.php 第 162 行 如何解决这个问题。 他们的 GitHub 页面上分享了安装步骤。 它告诉你你需要 PHP 8.0+,我将在这个答案的空间中假设你已经放弃了。该文档为您提供了两种可能的方法。第一个是使用Composer: 如果您尚未安装 Composer,请阅读有关 Composer 安装的信息。 一旦您有了正确运行的作曲家,请在项目根目录中运行以下命令: composer require google/apiclient 如果您遇到超时,则可以调整超时限制,如自述文件中的示例所示: { "config": { "process-timeout": 600 } } 当然,process-timeout 需要在作曲家模式的配置部分中解决。然后确保包含自动加载器: require_once '/path/to/your-project/vendor/autoload.php'; 您可以将库固定到最新版本,以防止因破坏性更改而导致问题。进一步阅读有关清理未使用的库的信息,它可能会对您将项目需求减少到您实际需要的需求有所帮助。 您还可以 从发布页面下载该版本并将其嵌入到您的存储库中。 您有一个与 Logger 相关的错误。让我们确保 1. 在开始使用 Google API PHP 客户端之前,您的 CodeIgniter 可以正常工作;2. 您已正确安装 Google API PHP 客户端。 一旦我们可以将这两个陈述作为事实,您将需要仔细阅读有关 CodeIgniter 和 Google API PHP 客户端兼容性的信息。很有可能 composer update 将解决问题。您可以添加类似的内容 "psr/log": "^3.0" 到你的composer.json的require部分。

回答 1 投票 0

PHP 从上下文调用 protected (从另一个模型)::__construct()

如果您需要的话,请友善地向我询问更多信息。请确保我已尽我所能,没有提出另一个问题 我需要访问并行模型内的模型方法。 型号...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.