Home  >  Article  >  Backend Development  >  How to use PHP and Typecho to build a food sharing website

How to use PHP and Typecho to build a food sharing website

WBOY
WBOYOriginal
2023-07-20 22:49:581325browse

How to use PHP and Typecho to build a food sharing website

Introduction:
Food has become an indispensable part of people's lives. People not only enjoy the taste enjoyment brought by food, but are also willing to share it. Your own cooking experience and food experience. Building a food sharing website allows people to communicate, share, and obtain more information about food. This article will introduce how to use PHP and Typecho to build a food sharing website, and provide code examples for reference.

1. Introduction to Typecho
Typecho is a simple, fast, open source PHP blog engine. It has been favored by the majority of developers for its streamlined code, efficient performance and rich plug-in ecosystem. Typecho supports custom theme and plug-in development, which is very suitable for building various personal blogs and community websites.

2. Build environment preparation

  1. Server environment: PHP environment, MySQL database.
  2. Install Typecho: Upload the source code of Typecho to the server, create the database and complete the relevant configuration.

3. Establishing database tables
Food sharing websites need to store data such as users, articles and comments, so the corresponding table structure needs to be created in the MySQL database. The following is a sample code for creating a table:

CREATE TABLE `typecho_users` (
  `uid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(32) NOT NULL,
  `password` VARCHAR(64) NOT NULL,
  `mail` VARCHAR(64) NOT NULL,
  `screenName` VARCHAR(32) NOT NULL,
  `created` INT(10) UNSIGNED DEFAULT 0,
  `activated` INT(10) UNSIGNED DEFAULT 0,
  `group` VARCHAR(16) NOT NULL DEFAULT 'subscriber',
  PRIMARY KEY (`uid`)
) ENGINE = MyISAM DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

CREATE TABLE `typecho_contents` (
  `cid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` TEXT NOT NULL,
  `text` MEDIUMTEXT NOT NULL,
  `created` INT(10) UNSIGNED DEFAULT 0,
  `modified` INT(10) UNSIGNED DEFAULT 0,
  `type` VARCHAR(16) NOT NULL DEFAULT 'post',
  `status` VARCHAR(16) NOT NULL DEFAULT 'publish',
  `authorId` INT(10) UNSIGNED DEFAULT 0,
  `template` VARCHAR(32) DEFAULT NULL,
  PRIMARY KEY (`cid`),
  KEY `type` (`type`),
  KEY `status` (`status`),
  KEY `authorId` (`authorId`),
  FULLTEXT KEY `text` (`text`)
) ENGINE = MyISAM DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

CREATE TABLE `typecho_comments` (
  `coid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `cid` INT(10) UNSIGNED DEFAULT 0,
  `created` INT(10) UNSIGNED DEFAULT 0,
  `author` VARCHAR(32) NOT NULL,
  `authorId` INT(10) UNSIGNED DEFAULT 0,
  `ownerId` INT(10) UNSIGNED DEFAULT 0,
  `mail` VARCHAR(64) DEFAULT NULL,
  `url` VARCHAR(64) DEFAULT NULL,
  `ip` VARCHAR(64) DEFAULT NULL,
  `agent` VARCHAR(255) DEFAULT NULL,
  `text` MEDIUMTEXT NOT NULL,
  `type` VARCHAR(16) NOT NULL DEFAULT 'comment',
  `status` VARCHAR(16) NOT NULL DEFAULT 'approved',
  `parent` INT(10) UNSIGNED DEFAULT 0,
  PRIMARY KEY (`coid`),
  KEY `created` (`created`),
  KEY `authorId` (`authorId`),
  KEY `ownerId` (`ownerId`),
  KEY `status` (`status`),
  KEY `cid` (`cid`)
) ENGINE = MyISAM DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

CREATE TABLE `typecho_relationships` (
  `cid` INT(10) UNSIGNED NOT NULL DEFAULT 0,
  `mid` INT(10) UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`cid`,`mid`),
  KEY `mid` (`mid`)
) ENGINE = MyISAM DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;

4. Create a theme template
Typecho's theme template determines the appearance and layout of the food sharing website. The following is a simple sample theme template code:

<?php
/**
 * 美食分享主题模板
 *
 * @package custom
 */

if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('header.php');
?>

<div class="content">
  <?php while($this->next()): ?>
    <article class="post">
      <h2 class="title"><a href="<?php $this->permalink() ?>"><?php $this->title() ?></a></h2>
      <p class="meta"><?php $this->date('F j, Y'); ?></p>
      <div class="entry">
        <?php $this->content('- 阅读全文 -'); ?>
      </div>
    </article>
  <?php endwhile; ?>
</div>

<?php $this->need('footer.php'); ?>

5. Front page display
Food sharing websites need to display food pictures and descriptions uploaded by users. The following code is an example of front page display:

<?php if ($this->is('post')): ?>
  <h2 class="post-title"><?php $this->title() ?></h2>
  <div class="post-meta">
    <span class="time"><?php $this->date('F j, Y'); ?></span>
  </div>
  <div class="post-content">
    <?php $this->content(); ?>
  </div>
<?php else: ?>
  <?php $this->widget('Widget_Archive@index', 'pageSize=10&type=post')->to($posts); ?>
  <?php while($posts->next()): ?>
    <div class="post">
      <h2 class="post-title"><a href="<?php $posts->permalink() ?>"><?php $posts->title() ?></a></h2>
      <p class="post-meta"><span class="time"><?php $posts->date('F j, Y'); ?></span></p>
    </div>
  <?php endwhile; ?>
<?php endif; ?>

6. User registration and login
In order to allow users to post their own food sharing, we need to provide user registration and login functions. The following code is an example of user registration and login:

<?php if($this->user->hasLogin()): ?>
  <!-- 用户已登录,显示用户信息和注销按钮 -->
  <p><?php $this->user->screenName(); ?>,欢迎回来!</p>
  <a href="<?php $this->options->logoutUrl(); ?>">注销</a>
<?php else: ?>
  <!-- 用户未登录,显示登录表单 -->
  <form method="post" action="<?php $this->options->loginAction(); ?>">
    <p><label for="name">用户名:</label><input type="text" name="name" id="name" /></p>
    <p><label for="password">密码:</label><input type="password" name="password" id="password" /></p>
    <p><button type="submit">登录</button></p>
  </form>
<?php endif; ?>

7. Implementation of food sharing function
After the user logs in, we need to provide a form for users to post food sharing. The following code is an example of the food sharing function:

<?php if($this->user->hasLogin()): ?>
  <!-- 用户已登录,显示美食分享表单 -->
  <form method="post" action="<?php $this->options->index('/action/food-share'); ?>">
    <p><label for="title">标题:</label><input type="text" name="title" id="title" /></p>
    <p><label for="content">内容:</label><textarea name="content" id="content" rows="5"></textarea></p>
    <p><label for="image">图片:</label><input type="file" name="image" id="image" /></p>
    <p><button type="submit">分享</button></p>
  </form>
<?php else: ?>
  <p>请先<a href="<?php $this->options->loginUrl(); ?>">登录</a>后再分享美食。</p>
<?php endif; ?>

8. Summary
It is not complicated to build a food sharing website through PHP and Typecho. You only need to complete environment preparation, establish database tables, create theme templates and implement related Just function. Through the introduction of this article, I hope it can help readers initially understand the basic steps and sample codes for using Typecho to develop a food sharing website. If necessary, you can extend and customize the sample code according to actual needs to meet more personalized needs.

References:

  1. Typecho official documentation: https://typecho.org/
  2. Typecho communication community: https://bbs.haobird.com/

The above is a brief introduction to using PHP and Typecho to create a food sharing website. I hope it will be helpful to readers. Wish you build a unique food sharing website!

The above is the detailed content of How to use PHP and Typecho to build a food sharing website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn