php网站建立

网站建立

码代码前应该首先确定这个网站的数据库需要哪些表单,每一个表单有哪些信息,比如你要展示产品,你就要一个展示产品封面的表单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);

    }

扫描二维码关注公众号,回复: 840715 查看本文章

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 onclick="return confirm('确定修改?')" href="<?php echo site_url("tb_album/album_updata/$item->id")?>">修改</a></td>

      <td width="100px"><a onclick="return confirm('确定删除?')" 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-->

猜你喜欢

转载自2534948091.iteye.com/blog/2208005