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);

    }

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

      <td width="100px"><a οnclick="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-->
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编写php网站建设实验指导书,一般可以按照以下步骤进行: 1. 引言:简要介绍php网站建设实验的目的、意义和背景,阐明学生实验的重要性和课程目标。 2. 实验目标:明确实验的具体目标,例如理解php网站建设的基本原理和技术要点,掌握常用的php编程技巧,培养网站建设的实际操作能力等。 3. 实验环境准备:详细说明学生需要配置的开发环境和相关软件工具,包括php集成开发环境(如WAMP、XAMPP等)、代码编辑器、浏览器等。 4. 实验内容:逐个介绍实验的具体内容和步骤,包括网站的框架搭建、数据库连接、数据操作、用户登录注册、页面设计与美化等。可结合具体实例进行讲解,给出实际操作步骤和示例代码。 5. 实验要求:明确学生在实验过程中需要达到的要求,例如掌握php基础知识和语法、熟悉数据库操作、实现网站的基本功能等。 6. 实验指导:给出操作指导和提示,引导学生按照正确的步骤完成实验,遇到问题时提供解决方案或常见错误的解决方法。 7. 实验扩展:提供一些扩展实验的想法和建议,鼓励学生进一步探索和改进已有的功能,增加更多的交互和实用性。 8. 总结与反思:总结实验的收获和经验,对学生的表现进行评价,并给出展望和建议,鼓励学生在php网站建设领域深入学习和实践。 9. 参考资料:列出参考资料和推荐阅读,在结束部分给出更多深入学习的资源和网站链接。 通过以上步骤编写php网站建设实验指导书,能够帮助学生理解和掌握php网站建设的基本技术和方法,促进他们的实践能力和创新思维的培养。同时,指导书应具体明确实验的目标和要求,提供具体的实验指导和支持,以便学生能够顺利进行实验并取得理想的实验效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值