简单的博客和分类关联代码(20140509更新)
关联代码,包括修改,分页,获取,增加,删除,修改,由于我是新手,写的不是很好,求高手指点修正:)
控制器代码(是Action里的 IndexAction.class.php):<?php <br />
class IndexAction extends Action {<br>
//实例化博客,还有关联分类在Model里<br>
public function index(){<br>
//实例公共模型类<br>
$Blog=D('Blog');<br>
//调用分页类<br>
import('ORG.Util.Page');<br>
//统计分页<br>
$count = $Blog->count();<br>
//创建分页数<br>
$Page = new Page($count,5);<br>
//分页输出<br>
$show = $Page->show();<br>
//赋值给关联查询,并且倒序输出<br>
$list=$Blog->relation(true)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();<br>
//赋值给列表标签<br>
$this->assign('list',$list);<br>
//赋值给分页标签<br>
$this->assign('page',$show);<br>
//创建视图<br>
$this->display();<br>
}<br>
<br>
//删除博客<br>
public function del(){<br>
// 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/del/id/{$x.id}<br>
//视图$_GET提交ID<br>
$id =$_GET['id'];<br>
//其中ID传值,如果不写$where,而是$id 会把整个数据<br>
$where = array('id' => $id);<br>
//判断博客删除<br>
if (M('Blog')->where($id)->delete()) {<br>
$this->success('删除成功');<br>
} else {<br>
$this->error('删除失败'); <br>
} <br>
<br>
}<br>
<br>
//增加博客<br>
Public function add () {<br>
//显示修改数据<br>
$this->cate =$cate = M('cate')->select();<br>
//组装主表数据<br>
$id = $_POST['id'];<br>
$cid = $_POST['cid'];<br>
//tutle博客标题 content博客内容 cid 父级分类关联 cate<br>
$data = array(<br>
'title' => $_POST['title'],<br>
'content' => $_POST['content'],<br>
'cid' => $cid,<br>
);<br>
//增加新数据<br>
if ($bid = M('blog')->add($data)) {<br>
if (isset($_POST['cid'])) {<br>
$this->success('添加成功','index');<br>
} else {<br>
$this->error('添加失败'); <br>
} <br>
} <br>
//创建增加视图 <br>
$this->display();<br>
}<br>
<br>
//修改<br>
Public function mod () {<br>
// 在模版里修改地址是 __APP__/你的文件(INDEX或者ADMIN)/mod/id/{$x.id}<br>
$id=$_GET['id'];<br>
if(!empty($id)){<br>
//获取博客文章标题和内容<br>
$art=M('Blog');<br>
$date=$art->getById($id);<br>
//赋值给博客标签datas<br>
$this->assign('datas',$date);<br>
//获取分类<br>
$cat=M('cate');<br>
$list=$cat->select();<br>
//复制给分类标签clist<br>
$this->assign('clist',$list);<br>
}<br>
//创建修改模版视图<br>
$this->display();<br>
}<br>
<br>
//修改提交处理<br>
public function update(){<br>
//create方法<br>
$Form= M('blog');<br>
if($Form->create()) {<br>
$result = $Form->save();<br>
if($result) {<br>
//如果修改成功,跳转到首页<br>
$this->success('操作成功!','index');<br>
}else{<br>
//否则修改错误<br>
$this->error('写入错误!');<br>
} <br>
//否则系统异常错误<br>
}else{<br>
$this->error($Form->getError());<br>
}<br>
}<br>
}
模型代码(是Model里的 ,文件名BlogModel.class.php):<?php <br />
class BlogModel extends RelationModel{<br>
protected $_link = array(<br>
<br>
'Cate'=>array(<br>
'mapping_type'=> BELONGS_TO,<br>
'class_name'=>'Cate',<br>
'mapping_name'=>'Cate',<br>
'foreign_key'=>'cid',<br>
//只获取关联里name内容<br>
'mapping_fields'=>'name',<br>
//把关联的name拿到父级上<br>
'as_fields'=>'name',<br>
//'as_fields' =>'id:uid,title,name',<br>
<br>
),<br>
<br>
);}<br>
<br>
?>
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (
