我正在创建一个用户可以上传自己的背景图片的网站,上传后他们会看到他们在一个数字所代表的菜单中上传的每个背景,点击这个数字理论上会在新背景中加载,但是我注意到了这也是在视图中再次调用(视图已经从另一个函数加载,有没有一种方法可以将数据传递给视图而不加载视图,我可以用ajax吗?如果是这样的话?
我的代码目前
public function set_background() {
$this->load->model('image_model');
if($query = $this->image_model->get_background_by_id($this->uri->segments[3])) {
if($query) {
$data['new_background'] = $query;
}
}
$this->load->view('template/background-select', $data);
}
我的模特:
public function get_background_by_id($background_id) {
$this->db->select('background_name');
$this->db->from('background');
$this->db->where('background_id', $background_id);
$query = $this->db->get();
return $query->result_array();
}
我的观点
<div id="background-select">
<?php
$count = 0;
if(isset($special)) {
foreach ($special as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
if(isset($generic)) {
foreach ($generic as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
if(isset($user_background)) {
foreach ($user_background as $row) {
$count ++;
print '<div class="select">';
print "<a class='background_btn' href='index.php/home/set_background/".$row['background_id']."'>$count</a>";
print '</div>';
if($count == 1) {
$background = $row['background_name'];
}
}
}
?>
</div>
<div id="wrapper" style=<?php echo"background:url(/media/uploads/backgrounds/".$background.");";?>>
视图最初在这里加载
public function index() {
// $this->output->enable_profiler(TRUE);
$data = array();
if($query = $this->category_model->get_all_online()) {
$data['main_menu'] = $query;
}
$this->load->model('image_model');
/*
* Sort out the users backgrounds, basically do a check to see if there is a 'special' background
* if there is not a 'special' background then IF the user is logged in and has a background of there
* own show that one, if not show a generic one, if they are not logged in show a bang one
*/
$image = array();
if ($query = $this->image_model->get_special_backgrounds()) {
$image['special'] = $query;
} elseif(!isset($image['special']) && !isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_bang_background()) {
$image['generic'] = $query;
}
}
if(isset($this->session->userdata['user_id'])) {
if($query = $this->image_model->get_user_backgrounds($this->session->userdata['user_id'])) {
$image['user_background'] = $query;
}
}
$data = array_merge($data, $image);
$this->load->view('home/main_page.php', array_merge($data, $image));
}
希望有些人可以帮助谢谢
答案 0 :(得分:0)
这可能过于“开箱即用”并且不足以使您的代码变得有用,但这里有:
考虑到代码应该做什么的描述,你可以随时将你的PHP输出背景图像的列表放到视图上的JavaScript代码段中(非常粗略地伪编码:{{1 }}),然后让javascript动态创建客户端背景更改链接列表,每次点击只需使用JavaScript更改背景图片(<script><?php echo phpArrayToJavaScriptArray($images, $varName); ?></script>
设置<a href="javascript:changeBackgroundImage('url')">
的背景图片)。
不需要Ajax。
考虑到这个概念,我可以匆忙进行最简单的代码调整:
不是将ID传递给视图,而是将URL传递给视图(当然,您必须相应地调整查询),然后更改:
document.getElementById('wrapper')
类似
"<a
class='background_btn'
href='index.php/home/set_background/" . $row['background_id'] . "'>"
. $count
. "</a>"
JavaScript函数将是这样的:
"<a
class='background_btn'
href=\"javascript:changeBackgroundImage('"
. htmlspecialchars($row['background_url'], ENT_QUOTES, 'utf-8')
. "')\">"
. $count
. "</a>"
介意,其中很多仍然是伪代码,我怀疑它是否已经开箱即用,我的JS非常生疏,这意味着分享的想法不仅仅是一个彻头彻尾的解决方案。 :)但我希望它能帮助你解决问题!
这种方法对你来说是否可行?