设为首页 收藏本站
查看: 410|回复: 0

[经验分享] php网站建立

[复制链接]
发表于 2017-3-20 12:42:59 | 显示全部楼层 |阅读模式
网站建立
码代码前应该首先确定这个网站的数据库需要哪些表单,每一个表单有哪些信息,比如你要展示产品,你就要一个展示产品封面的表单tb_album(以此为例)等等,tb_album里面需要有idtypepricealbum_urlintro等信息。然后在控制器tb_album里面写相应的方法,model里面写数据库操作,view里面写展示界面。
//控制器tb_album
<?php
class Tb_album extends CI_Controller{
var $flag='';
var $type='';
function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('Album_model','',TRUE);
    }
function show($flag){
if ($flag!='2'||$flag!='3'||$flag!='4') {
$album=$this->Album_model->getAll();
$data['album']=$album;
}
if($flag=='2'){
$album=$this->Album_model->getClass();
$data['album']=$album;
}
if($flag=='3'){
$album=$this->Album_model->getLovers();
$data['album']=$album;
}
if($flag=='4'){
$album=$this->Album_model->getOneself();
$data['album']=$album;
}
$this->load->view('album/show_pic',$data);
}
function album_add(){
$this->load->view('album/show_add');
}
function album_do_add(){
$data['type'] = $this->input->post('type');
$data['price'] = $this->input->post('price');
        $data['intro'] = $this->input->post('intro');
        
        $config['upload_path'] ='./source/uploads/';
    $config['file_name']=uniqid();
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
    $this->load->library('upload',$config);
    if (!$this->upload->do_upload('userfile'))
    {
   $error = array('error' => $this->upload->display_errors());
    }else{
   $arr=$this->upload->data();
   $path='source/uploads/'.$arr['file_name'];
    }
        $data['album_url']=$path;
$this->Album_model->album_insert($data);
redirect("tb_album/album_add","refresh");
}
function album_delete($id,$type){
$this->Album_model->album_delete($id);
redirect("tb_album/show/$type","refresh");
}
function album_updata($id){
$que=$this->Album_model->getByid($id);
$data['result']=$que;
        $this->load->view('album/show_updata',$data);
}
function album_do_updata(){
$id = $this->input->post('id');
$data['type'] = $this->input->post('type');
$data['price'] = $this->input->post('price');
        $data['intro'] = $this->input->post('intro');
        
        $config['upload_path'] ='./source/uploads/';
    $config['file_name']=uniqid();
    $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
    $this->load->library('upload',$config);
    if (!$this->upload->do_upload('userfile'))
    {
   $error = array('error'=> $this->upload->display_errors());
    }else{
   $arr=$this->upload->data();
   $path='source/uploads/'.$arr['file_name'];
    }
        $data['album_url']=$path;
        $this->Album_model->album_updata($id,$data);
        redirect('tb_album/show/1','refresh');
}
}
?>
控制器里面写的就是之前写的数据库的增、删、改,只不过之前写的没用到model,这次是将数据库写到model里面,然后$this->Album_model->方法名();加载model,这样写的话更加规范,网站比较大时后期维护更加容易。
然后views里面新建一个album文件夹,里面实现界面展示,拿show_pic为例。
<!DOCTYPE html>
<html>
<head>
<title>电子产品列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="<?php echo  base_url()?>source/css/contract.css">
</head>
  <body>
<table>
      
<th>ID</th>
<th>类型</th>
<th>价格</th>
<th>文字介绍</th>
<th>照片</th>
  
<th colspan="2" style="text-align: center;width: 100px">操作</th>
  
<?php foreach ($album as $item):?>
  <tr>
   <td name="id" style="text-align: center;"><?php echo $item->id?></td>
  <td name="type" width="300px" style="text-align: center;"><?php echo  $item->type?></td>
  <td name="price" width="300px" style="text-align: center;"><?php echo    $item->price?></td>
  <td name="intro" width="400px" style="text-align: center;">
      <div style="text-align: center;width:200px"><?php echo $item->intro?></div></td>
<td class="photo" style="text-align: center;"><a href="<?php if($item->album_url!=''){echo '';}else {echo site_url("uploads/upload/$item->type");}?>"><img width="100" height="100" alt="" src="<?php echo  base_url().$item->album_url?> "></a></td>
      <td style="text-align: center;width: 100px"><a font-family: 宋体;">确定修改?')" href="<?php echo site_url("tb_album/album_updata/$item->id")?>">修改</a></td>
      <td width="100px"><a font-family: 宋体;">确定删除?')" href="<?php echo site_url("tb_album/album_delete/$item->id/type");?>">删除</a></td>
  </tr>
  <?php endforeach;?>
  </table>
  
  </body>
</html>
然后其他的view都是类似的。
这样的话,一个小模块就做好了,然后再根据网站的功能需求,再添加模块就好了。
各个模块做好后,整个php后台就搭好了,然后就是和前端整合了,整合的话就需要写一个controller加载前端的view,再在view里面修改对应的内容即可。
在之后就是上线了,什么申请虚拟主机、购买域名等等.....
问题总结
1<?php echo site_url('tb_album/album_do_add')?>使用这个方法需要在config/autoload下面将$autoload['helper'] = array('url');改为$autoload['helper'] = array('url');
2、不知道为什么这两句不能连着用,同时使用就报错,redirect前面不能有输出?
echo '<script language="JavaScript">'.'alert("删除成功!")'.'</script>';
redirect("product/product_showlist","refresh");
目前只想到这两个,,,
 
 
由于刚学php不久,很多错误难免,欢迎留言指正

【完】
<!--EndFragment-->

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-352405-1-1.html 上篇帖子: PHP入门教程 下篇帖子: PHP变量命名建议
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表