我在使用Codeigniter框架更新记录时遇到了一些麻烦.我正在使用MVC模式和活动记录.
该代码用于更新用户配置文件.
在我的模型中,Profile_model我有一个更新功能...
function profile_update()
{
$this->db->where('user_id', 2);
$this->db->update('user_profiles', $data);
}
Run Code Online (Sandbox Code Playgroud)
控制器Profile_crud应从表单中检索数据并将数据发送到模型.
function update()
{
$data = array (
'country' => $this->input->post('country'),
'website' => $this->input->post('website')
);
$this->load->model('Profile_model');
$this->Profile_model->profile_update($data);
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中的表单profile.php提交它会触发update我的控制器中的功能.
<?php echo form_open('profile_crud/update'); ?>
<p>
<label for="country">Country</label>
<input type="text" name="country" id="country" />
</p>
<p>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
Run Code Online (Sandbox Code Playgroud)
当我提交表单时,我得到3种类型的错误.
遇到PHP错误
严重性:注意
消息:未定义的变量:数据
文件名:models/profile_model.php
行号:27
第27行是 $this->db->update('user_profiles', $data);
遇到PHP错误
严重性:警告 …
我正在使用Jquery加载功能$('#result').load('test.php');通过单击选项卡将页面加载到另一个页面.我正在加载的页面包含javascript,php,并包含一个表单.使用firebug控制台,我看到GET我正在加载的页面中有所有脚本源.不确定这是否应该是一个问题..
可以通过jquery加载来攻击数据吗?有什么问题需要考虑吗?
注意:我知道旧的浏览器不能与javascript一起使用,但是还有什么需要考虑的吗?
我正在尝试添加代码来the_content()运行.我尝试了以下内容
function add_something($content) {
echo "test";
}
add_filter( 'the_content', 'add_something', 6);
Run Code Online (Sandbox Code Playgroud)
添加过滤器后,我的帖子只返回test没有内容的回声.如何在不省略内容的情况下执行自定义代码?
时间令人困惑......如何计算PHP中从PM到AM的时间?
例如22:00:00 (10pm),02:00:00 (02am)应该给予04 hours过去.而是我的代码返回-08 hours
function hours_elapsed()
{
$timestart = strtotime("22:00:00");
$timestop = strtotime("02:00:00");
$time_diff = $timestop - $timestart; //time difference
return gmdate("h", $total_hours);
}
Run Code Online (Sandbox Code Playgroud)
很明显,代码以24小时格式计算,所以它当然会返回-08但是如何在没有24小时约束的情况下获得时间...当它通过午夜标记时?
我正在使用名为Redactor的精彩jquery文本编辑器.我正在尝试添加一个新按钮,单击该按钮可获得文本编辑器中突出显示的文本.
该脚本允许通过添加以下设置添加新按钮:
buttonsCustom: {
button1: {
title: 'Button',
callback: testButton //executes callback on button click
}
}
Run Code Online (Sandbox Code Playgroud)
然后在回调中我想获得突出显示的文本
function testButton(obj, event, key)
{
alert(highlighted_text);
}
Run Code Online (Sandbox Code Playgroud)
我彻底查看了文档,没有办法获得突出显示的文本.我试过像......这样的其他功能
function getSelText() {
var txt = '';
if (window.getSelection) {
txt = window.getSelection();
} else if (document.getSelection) {
txt = document.getSelection();
} else if (document.selection) {
txt = document.selection.createRange().text;
} else return;
return txt;
}
Run Code Online (Sandbox Code Playgroud)
...但是文本编辑器脚本已经有了一种方法,最好使用它.
在脚本中我找到了文本选择功能在1719行的位置,但无法弄清楚如何将它用于自定义按钮.
任何有Redactor经验的人,请帮忙!
我有一个表,其中包含自动增量主键列和日期时间列.我想返回按创建日期排序的行.
如果我按日期或主键订购,这有关系吗?我认为通过与日期格式0000-00-00 00:00:00或时间戳相对的主键更快.新记录将具有更大的主键.然而每个人都按日期字段排序.
我在firebase中有以下db结构
我正在尝试获取属于特定用户ID(uid)的数据.该文档具有以下示例:
firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
// ...
});
Run Code Online (Sandbox Code Playgroud)
但是,如何在不知道每个对象的唯一键的情况下从我的示例中检索数据?
更新:
我尝试了一种新方法,将用户ID添加为主键,每个子对象都有自己唯一的ID.
现在的挑战是如何获得"头衔"的价值.
我正在使用CI的Auth Tank库来查询某些用户的记录.
该变量$user_id = tank_auth->get_user_id();从会话中获取用户ID.我想拉记录在哪里user_id = $user_id.
根据我的理解,构造函数可以在每次启动类时加载变量.有点像全局变量.所以我想我会$user_id在模型构造函数中设置我,这样我就可以将它用于模型类中的多个函数.
class My_model extends Model {
function My_model()
{
parent::Model();
$user_id = $this->tank_auth->get_user_id();
}
function posts_read() //gets db records for the logged in user
{
$this->db->where('user_id', $user_id);
$query = $this->db->get('posts');
return $query->result();
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我正在加载模型,在我的控制器中创建一个数组,并将数据发送到我的视图,在那里我有一个foreach循环.
当测试我得到
消息:未定义的变量:user_id
在我的模型中.但是,如果我$user_id在我的posts_read函数中定义变量,但是我不想在每个需要它的函数中定义它.
我在这做错了什么?
我有一个帖子列表和edit每个帖子的链接.点击edit它进入一个页面,我可以编辑我点击的特定帖子.为此我将不得不从db中提取帖子的id.
这是正确的方法吗?
<a href="<?php echo site_url("post/edit/$row->id"); ?>">Edit</a>
post是我的控制器,edit是我的功能,$row->id应该拉出帖子的ID.
我有一个用户提交CSV的文本输入,例如 red, blue, red, yellow
如果用户提交重复值,例如red如上所示,我想删除副本.我开始回调,但我不知道如何完成它.
//callback rule
function _remove_dublicate($str)
{
$val = strtolower($str); //make everything lowercase
$colors = str_getcsv($val); //create array
$result = array_unique($colors); //remove duplicates
}
Run Code Online (Sandbox Code Playgroud)
如果有重复项,我该怎么做才能将新字符串提交$result到数据库?
以下是我的表单验证
$this->form_validation->set_rules('colors', 'Colors', 'trim|required|xss_clean|regex_match[/^[a-z, ]+$/i]|callback__remove_dublicate');
if ($this->form_validation->run() == FALSE) //if validation rules fail
{
$this->load->view('edit/form');
}
else //success
{
$data = array (
'colors' => $this->input->post('colors')
);
$this->My_model->colors_update($data);
}
Run Code Online (Sandbox Code Playgroud)
编辑
根据Cabaret的建议,我在else删除共和党的声明中加入了这一建议
$colors = str_getcsv($this->input->post('colors')); //create array
$result = array_unique($colors); //remove duplicates
$comma_separated = implode(",", $result); …Run Code Online (Sandbox Code Playgroud)