This project is the final project of PHP class in my junior year. Homework has been handed in, now put up for your reference study!

I. Introduction of technology stack

Subject: Food blog

Front end: HTML, JS, CSS, Bootstrap, JQ

Back-end: PHP MVC

Database: mysql

The address of food interface invocation of this project: Food interface document

Github source: github.com/zoyoy1203/p…

A network backup source: pan.baidu.com/s/1CEHItobT…

Extraction code: JH36

Two: realize the function summary

  1. Log in, register, log out, verification code.
  2. API interface call: recipe recommendation, recipe classification, recipe classification details, recipe details.
  3. Personal information display: avatar, motto modification.
  4. Personal dynamic release display.
  5. Dynamic thumbs-up comment function.
  6. Dynamic display, search, keyword red function.
  7. User list display, add and delete friends function.
  8. Error message prompt function.

Three: overall structure

The project adopts a simple MVC structure.

Since I later declared the use of table structure data directly in the control layer, I later removed the Model layer. Leave only the Controller control layer and the View View layer.

Public (static style file) upload(for uploading images) util (there is only one vercode. PHP inside for drawing captcha images)

index.php
url
Controller

The project Controller file contains only one UserController class, which contains all of the project’s processing methods. Just follow the /phpProject/? A = Regis (a= followed by the corresponding call method) is the format for the request.

Iv. Exhibition of works

1. Log in to the registration function

The login page is always submitted to action=”/phpProject/? a=loginPost” action=”/phpProject/? a=regisPost”

Then use the corresponding method in the Controller file userController.php.

If an error occurs during the login and registration process, the corresponding error cause is displayed on the page:

Function requirements: 1. User registration: judge the same name, verification code verification, at least the user name and password fields. (Done!)

As you move your project, note the util folder in vercode.php

$font = "D:/xampp/htdocs/phpProject/public/font/segoepr.ttf"; // Path problem

Change the password to the path corresponding to the current PC. Otherwise, the verification code does not have fonts.

The four characters generated by the verification code are stored in $_SESSION[“code”] for subsequent judgment.

In addition, in order to solve the problem that random colors cause individual characters of the verification code to blend into the background color, the verification code on the login page is configured with the function of clicking the image of the verification code to switch characters.

The main code is as follows:

<img src="util/verCode.php" alt="I can't see it. Change it." onclick="javascript:newgdcode(this,this.src);" style="width: 100px; height:50px;"  alt=""/>

<script language="javascript">
    function newgdcode(obj,url) {
        obj.src = url+ '? nowtime=' + new Date().getTime();
        // After passing a random parameter, otherwise in IE7 and Firefox, do not refresh the image
    }
</script>
Copy the code

Function Requirements: 2. User login: in SESSION mode. (Done!)

After successful login, the login user name, user ID, and user profile picture address will be stored in SESSION respectively:

_SESSION[‘userid’] $_SESSION[‘avatar’]

The user name is displayed on the right of the menu bar.

Subpages under the home page and recipe page can be displayed without user login.

Other pages, such as: personal center, dynamic, friends list and other pages, require user login to display. If you have not logged in, the login page is displayed.

6. Use API interface to make a function, such as weather, recipe, film, train ticket query, etc. (done!)

2. The home page

(Call bean & Fruit Food data interface: interface document)

3. Recipe classification page, recipe page, recipe details page (call bean and fruit food data interface:Interface documentation)

4. Personal Center page

Function requirements: 5. View your own shares: Logged in users should be able to view their own shares. (Done!)

5. Dynamic pages

Function requirements: 4. Home page: display all the shares published by users, and each share shows the publisher and time. (complete)

if($_POST['text']) {if(strlen($_POST['text'< =])400){
        $content = $_POST['text'];
    }else{
        $errinfo = 'Comment content over 200 Chinese characters! ';
        $this -> news1($errinfo,$errinfo1);
        die; }}if(count($_FILES['img'] ['name') >2){
    $errinfo = 'Upload more than 3 pictures! ';
    $this -> news1($errinfo,$errinfo1);
    die;
}
Copy the code

7. All users can search shared content: Users can search shared content, and the list is displayed, the search keywords are bold or red. (4 marks)

Input box to enter the search content (search users can be filled without filling, or only query the corresponding user dynamic). Click the search button and submit the form action=”/phpProject/? a=searchNew”

Connect to the database in the searchNew method to query the corresponding dynamic:

// Dynamic query
$sql = "SELECT news.*,`user`.avatar,`user`.nickname FROM `user`,news WHERE `user`.id=news.user_id ORDER BY news.createtime DESC,news.id DESC";
Copy the code

If there is a search content keyword, replace the keyword on each dynamic content data query:

// Dynamic content keywords are red
$result['content'] = str_replace($s_content,"$s_content",$result['content']);
Copy the code

The main code of dynamic like comment function is as follows:

/ / thumb up
public function addLike(a) {
    $id = $_GET['id'];
    $like = $_GET['like'];
     // Use the like value to determine whether to execute or cancel the praise sentence
     if($like==1){
         $sql = "DELETE FROM like_news WHERE news_id= ".$id." AND user_id=".$_SESSION['userid'];
     }else if($like==0){
         $sql = "INSERT INTO like_news(news_id,user_id) VALUES(".$id.",".$_SESSION['userid'].")";
     }
    $res = mysqli_query($this->link,$sql);
    
}
// Add a comment
public function addcomments(a) {
    if(!empty($_POST)){
        $newid = $_POST['newid'];
        $userid = $_SESSION['userid'];
        $content = $_POST['content'];

        $sql =  "INSERT INTO `comment`(new_id,user_id,content) VALUES(".$newid.",".$userid.", "".$content."')"; // Insert user comment information into the comment table
        $res = mysqli_query($this->link,$sql); }} Where, in order to not refresh the page after the "like" comment and achieve good user experience, 'AJAX' is used to request data asynchronously. (Modify motto, add and delete friends and other functions have used this method) like JS code example as follows: $(".addlike").on("click".function(a){
            let uid = $(this).children(".id").text();
            let avatar = $(this).children(".avatar").text();
            let newsid =  $(this).children(".newsid").text();
            let like = $(this).children(".like").text();

            var that = $(this);
            $.get("/phpProject/? a=addLike",{id:newsid,like:like},function(data){

                if(like ==1) {// $(".uid:contains(id)").parent("avatar_img").remove();
                    console.log($(that.next()).find(".avatar_img>.uid:contains(uid)").text());
                    var dom = $(that.next()).find(".avatar_img .uid");
                    console.log(dom);
                    $.each(dom, function(key, val) {
                        console.log(val.innerHTML);
                        if(val.innerHTML == uid){
                            $(val).parent().remove(); }}); $(dom).parent("avatar_img").remove();

// uidDom.parent("avatar_img").remove();
                    that.children(".like").text("0");
                    that.children(".like_text").text("Thumb up");
                }
                if(like == 0) {var html = "";
                    html += "<div class='avatar_img'><div class='uid'style='display: none;' >";
                    html += uid;
                    html += ";
                    html += avatar;
                    html += "' ></div>";
                    that.next().append(html)
                    that.children(".like").text("1");
                    that.children(".like_text").text("Unlike"); }}); });Copy the code
6. Friends list page, more friends page (with view all users, add and delete friends function)