标签: codeigniter

CodeIgniter在JSON响应中返回HTML

我正在尝试在codeigniter中构建一个支持ajax的购物车,现在我遇到了如何将响应作为HTML进行编码并将其编码为JSON响应的问题,然后在购物车页面附加响应.

这是javascript代码:

$('.addtocart').on('click', function (event) {
    event.preventDefault();
    var AddedQty = parseInt($('#attr-quantity').val());

    $('#shoppingcart div.cart, #shoppingcart div.loading').remove();
    $('#shoppingcart').append('<div class="loading"></div>');
    shoppingcart.open();

    $.post('/mywebsite/cart/add', {
        itemId: $('.addtocart').data('itemId'),
        quantity: AddedQty
    }, function (response) { 
        var html = $(response.ShoppingCartHtml).html(); 
        shoppingcart.update(html);
        shoppingcart.close();
    });
});
Run Code Online (Sandbox Code Playgroud)

这是购物车控制器的代码:

public function add() {     
    $this->load->model('cart_model');
    $id = $this->input->post('itemId');
    $qty = $this->input->post('quantity');
    $cart = $this->cart->contents();
    $exists = false;
    $rowid = '';

    foreach ($cart as $item) {
        if ($item['id'] == $id) {
            $exists = true;
            $rowid = $item['rowid'];
            $qty = $item['qty'] + $qty;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

php ajax jquery json codeigniter

5
推荐指数
1
解决办法
2万
查看次数

Jquery Datatables事件处理程序不使用分页

我有最后一列的表是一个获取整个行数据的元素,

当我使用分页时,只有触发我的事件处理程序的第一页,第2 - 3 - 4页等等不会触发我的事件处理程序.

禁用分页时,所有记录都会触发我的事件处理程序.

下面是用于初始化的javascript和事件处理程序

var OTkaryawan = $('#table_karyawan').dataTable({ });

$(".select_row").click(function() {
//                    var row = $(this).closest('tr')[0];
//                    var aData = OTkaryawan.fnGetData(row);
//                    alert(aData[0]);
                    alert('A');
                });
Run Code Online (Sandbox Code Playgroud)

下面是我的表的html

<table id="table_karyawan" style="border: 1px #ccc solid;">
                    <thead>
                        <tr>
                            <th>NIK</th>
                            <th>Nama Lengkap</th>
                            <th>Lokasi Kerja</th>
                            <th>Departemen</th>
                            <th>Jabatan</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($master_karyawan as $key => $value): ?>
                            <tr>
                                <td><?= $value['nik']; ?></td>
                                <td><?= $value['nama']; ?></td>
                                <td><?= $value['lokasikerja']; ?></td>
                                <td><?= $value['departemen']; ?></td>
                                <td><?= $value['jabatan']; ?></td>
                                <td class="select_row">Select</td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody> …
Run Code Online (Sandbox Code Playgroud)

javascript php datatable jquery codeigniter

5
推荐指数
1
解决办法
3875
查看次数

CodeIgniter的set_value()没有重新填充

我检查并重新检查了我的代码,引用了整个网络上的CI文档和其他帖子,但是我没有成功实现set_value()方法,以便在验证失败后重新填充表单字段.也许我错过了CI框架的一些非常重要的东西(我对它很新),但是你的见解会非常感激.

我的控制器方法中有以下内容:

public function form_step2(){
    //Form Setup
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $data['title'] = $this->base_title;
    $data['base_url'] = base_url();

    //Validation Settings - must be set per step
    $this->form_validation->set_rules('request_type', 'Request Type', 'required');
    *...more of the same set_rules()*


    if ($this->form_validation->run() === FALSE) {
        ### Validation failed or New Form

        // Get form element data
        $data['request_types'] = $this->my_model->get_form_data('request_type');
        *...more of the same get_form_data() calls for loading form elements*

        //Generate Page from View Templates
        $this->load->view('templates/header', $data);
        $this->load->view('templates/form_step2', $data);
        $this->load->view('templates/footer');
    } else {
        ### Save to database
        $this->my_model->set_data($data); …
Run Code Online (Sandbox Code Playgroud)

php codeigniter

5
推荐指数
1
解决办法
1万
查看次数

从amazon s3 buckets子文件夹中获取文件

我试图从亚马逊S3桶子文件夹中获取所有文件,并使其可以在网页中下载.

我有一个叫做图像的桶.在那个桶里面我有一些其他的文件夹.现在我试图获取该子文件夹中的所有文件并在页面中显示它.S3铲斗:/ images/images/test1// images/test2// images/test1/1/images/test1/2/images/test1/1/item

我试过这样,但无法得到预期的结果.

// Target files full path : images/test1/1/item 

$bucketName = 'images';
$source = '/test1/1/item' 

$image = $this->s3->getBucket($bucketName);
foreach ($image as $key=>$data){
   $k = $data['name'];
   print_r($k); // now this gives me full list of everything inside the images bucket. 
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我想从存储桶中获取对象,我可以使用getObject,我尝试这样做:

$result = $this->s3->getObject($image); // I am confused about this one
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.谢谢,

在使用Josue Ibarra的以下答案后,我得到了类似的东西

(

 [tableuploads/1/emaillists/15.10.2013-18.03.23-emailList.csv] => Array
     (
         [file_name] => 15.10.2013-18.03.23-emailList.csv
         [file_folder] => emaillists
         [file_size] => 64
         [created_on] => 2013-10-15 19:03:26
         [s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-       18.03.23-emailList.csv …
Run Code Online (Sandbox Code Playgroud)

php codeigniter amazon-s3 amazon-web-services

5
推荐指数
1
解决办法
1万
查看次数

403如果在网址中找到"union"一词,则禁止使用

我注意到如果在我的网站的网址上找到/添加了"union"这个词,我就会收到403禁用错误.

这是一个ajax(getjson)url的快照,其中"union"被添加为GET变量: 现在,如果你改变"union"这样的单词,就像"unsion"一样,我得到一个回复​​但是如果"union"这个词到位了,像"xxunionxx"一样可读,但仍然禁止回复.

我的网站运行在CI(CodeIgniter)框架上.我想知道我的服务器是否有过滤?

谢谢!

php codeigniter

5
推荐指数
1
解决办法
639
查看次数

为什么Codeception的PhpBrowser不遵循"重新加载"标题?

我在Ion Auth身份验证库中的一个真正的CodeIgniter项目中发生了这种情况,但为了清楚起见,我将其简化为最简单的形式.

剧本:

我有这一行脚本位于http://localhost/~captbaritone/redirect/index.php:

<?php header("Refresh:0;url=https://google.com");¬
Run Code Online (Sandbox Code Playgroud)

在我的浏览器中,它会重定向到Google.com.

考试:

为了测试它,我写了这个验收测试:

<?php
$I = new WebGuy($scenario);
$I->wantTo('Redirect to Google.com');
$I->amOnPage('/index.php');
$I->seeCurrentUrlEquals('https://www.google.com/');
Run Code Online (Sandbox Code Playgroud)

我acceptance.suite.yml看起来像这样:

class_name: WebGuy
modules:
    enabled:
        - PhpBrowser
        - WebHelper
    config:
        PhpBrowser:
            url: 'http://localhost/~captbaritone/redirect/'
Run Code Online (Sandbox Code Playgroud)

结果:

Codeception PHP Testing Framework v1.7.1
Powered by PHPUnit 3.7.27 by Sebastian Bergmann.

Acceptance Tests (1) -----------------------------------------
Trying to redirect to google.com (RedirectCept.php)       Fail
---------------------------------------------------------------


Time: 460 ms, Memory: 9.75Mb

There was 1 failure:

---------
1) Failed to redirect to …
Run Code Online (Sandbox Code Playgroud)

php codeigniter ion-auth codeception

5
推荐指数
1
解决办法
3438
查看次数

Codeigniter RESTful API - {"status":false,"error":"未知方法."}

我已经设法在我的Codeigniter应用程序中设置RESTful API.现在我想从我的MySQL数据库中获取一些数据,所以在我的Codeigniter模型文件夹中,我创建了一个名为category_model.php的模型:

<?php
  Class category_model extends CI_Model {
      var $table_name = 'category';

     function get_all_categories()
     {
       $this->db->select('*');
       $this->db->from($this->table_name);
       return $this->db->get();
     }
 }
?>
Run Code Online (Sandbox Code Playgroud)

然后在Codeigniter controller-folder中创建了一个category.php文件:

<?php

 include(APPPATH.'libraries/REST_Controller.php');

 class Category extends REST_Controller {

  function __construct()
    {
        parent::__construct();
        $this->load->model('category_model');
    }

  function category_get()
    {
        $data = $this->category_model->get_all_categories();
        $this->response($data);
    }

 }

?>
Run Code Online (Sandbox Code Playgroud)

现在,当我进入http://localhost/myproejcts/ci/index.php/category/category- 我得到错误{"status":false,"error":"Unknown method."}?

问题是什么?

[UPDATE] =设置时出现同样的错误function index_post()

php mysql rest codeigniter

5
推荐指数
2
解决办法
2万
查看次数

codeigniter中的控制器性能

对控制器类响应的最大LOC(LINE OF CODE)影响是否......这是重要的事情..

 1.a)Controller: LOC 120, public functions exist 3.
   b)Controller: LOC 120, public functions exist 3.
   c)Controller: LOC 120, public functions exist 3.

 2.) Controller LOC 720, pubic function 10.
   it handle all the things in the same class.
Run Code Online (Sandbox Code Playgroud)

哪一个更好?

php lines-of-code codeigniter micro-optimization

5
推荐指数
1
解决办法
523
查看次数

codeigniter - 电子邮件类 - 提交后注册

做了一个简单的提交用户表格电子邮件和城市数据保存在数据库中,现在我需要在提交数据系统后添加这个表格在用户地址上生成自动电子邮件但是提到的变量我使用一个变量调用$lang如果用户输入是ar所以发送阿拉伯语的电子邮件或如果用户输入en所以用英语发送电子邮件,我面临两个问题,不知道如何在代码中修复此电子邮件类,以便我共享代码.

电子邮件类

public function email() {
    $email = $this->input->post('email');
    $city = $this->input->post('city');
    $this->load->library('email');
    $this->email->from('noreply@abc.com', 'Halalat');
    $this->email->to('$emai');
    $this->email->subject('Halalat Newsletter Subscription');
    $this->email->message( 'Thankyou for submission' );
    $this->email->send();
    echo $this->email->print_debugger();
}
Run Code Online (Sandbox Code Playgroud)

控制器中的user.php

<?php 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('user_agent');
        $this->load->library('form_validation');
    }

    public function create_user() {
        // field name, error message, validation rules
        $lang = $this->input->post("lang");
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');        
        $this->form_validation->set_rules('city', 'City', 'trim|required');
        if ($this->form_validation->run() == …
Run Code Online (Sandbox Code Playgroud)

php forms email codeigniter

5
推荐指数
1
解决办法
1642
查看次数

模型中的Codeigniter和Set Session

我想取消设置并在模型函数中设置会话依赖于传递的id.
我有每个记录的语言字段,应考虑到这一点改变会话.
在示例单词中我有这样的链接:http:
//www.mysite.com/report/2020现在这篇文章可能由英语保存,但当前会话是french.so我应该更改会话.这是我的代码,但它不起作用.

  //function for checking language by id
  public function lang($id)
     {
         $data = $this->db
                        ->select('language')
                        ->from('content')
                        ->where('id',$id)
                        ->get();

        if ($data->num_rows > 0) {

            return $data;
  }else{
            return false;
        }
    }
  //function for reading data from db depend on id and language
  public function report($id)
    {

    $cat=2;
    $code=$id;

    $langu= $this->lang($id)->row()->language;
    if($langu==3){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','dr');
    }
    if($langu==2){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','pa');
    }
    if($langu==1){
    $this->session->unset_userdata('lang');
    $this->session->set_userdata('lang','en');
    }
    $language= $this->session->userdata('lang');
    $lang=$language;


    $data = $this->db
                        ->select('*')
                        ->from('content')
                        ->where('category',$cat)
                        ->where('language',$lang)
                        ->where('id',$code)
                        ->get();

        if ($data->num_rows …
Run Code Online (Sandbox Code Playgroud)

session model codeigniter

5
推荐指数
1
解决办法
8476
查看次数