📜 ⬆️ ⬇️

Flea market from scratch. Part 1

So, as I promised, we begin a series of articles on the development of a flea market at Codeigniter.
Together we will step by step develop a flea market.

1.1 Start


Let's get ready first. What you need to work?
  1. Of course, Apache + Mysql + PHP = Denwer (for beginners - super)
  2. Editor - in my case, this is a simple PsPad notepad editor with syntax highlighting. I know that there is a heaped Zend Studio and Eclipse - we will not complicate anything yet
  3. A bunch of IE 6 + Opera + Firefox - you need to make the site cross-browser

For this site I reserved a domain baraholka.rv.ua , because I live in Ukraine, in the city of Rivne.

For a normal understanding you need to be familiar with Php, Mysql, CSS, HTML, JS (jQuery) and still with Codeigniter.

We assume that we already have a template. And although in my case, not a super designer - for training fit.
')
I think the installation denwer can be skipped. Now in the home folder where you installed Denwer, create a folder baraholka.local. In her folder www.
You also need to put himself Codeigniter. Go to the site codeigniter.com , download the latest version.
Unpack the contents of the archive in the folder www.

From now on, in articles, I’ll replace the path to the www folder with WEB_ROOT, that is, the default is Z: \ home \ baraholka.local \ www
Also, the abbreviated form of Codeigniter - CI will sometimes be used.

Restart Apache, and try to log in to baraholka.local - if it worked, then everything is ok.
So, I have created such a template - baraholka.rv.ua/lesson/1/template.zip . We unpack archive also in WEB _ROOT.
What do we have now? Template and Codeigniter installed.

1.2 Configuring Codeigniter



Now, we need to prepare Codeigniter settings for proper operation. The file WEB_ROOT / system / application / config contains all the main configuration files.
  1. Rule autoload.php
    $autoload['libraries'] = array('database', 'session'); - . .
  2. Rule config.php
    $config['base_url'] = "http://baraholka.local/"; - ,
    $config['index_page'] = ""; - URL, .htaccess +
    $config['charset'] = "Windows-1251"; - UTF-8,

  3. Rule database.php
    $db['default']['hostname'] = "localhost";
    $db['default']['username'] = "root";
    $db['default']['password'] = "";
    $db['default']['database'] = "baraholka";
    “baraholka” . – Denwer`a
    $db['default']['char_set'] = "cp1251";
    $db['default']['dbcollat'] = "cp1251_general_ci";
    1251,

  4. Rules of routes.php
    $route['default_controller'] = "main"; - , welcome.


Also, to remove the index.php from the URL, we need to add .htaccess to the WEB_ROOT folder
We download it baraholka.rv.ua/lesson/1/htaccess . Rename to .htaccess

Who did not understand with some values ​​- please in the documentation - codeigniter.com/user_guide

1.3 Default controller and template slicing


Now you need to cut our template into the main parts - header, footer. Someone may ask:
“Why do this? You can just create a separate template for each new controller. ”
And if you want to change the logo? Will you change every page?
So let's create a new controller - WEB_ROOT / system / application / controllers / main.php
He will have this code:
 <?php class Main extends Controller { function Main() { parent::Controller(); } function index() { $this->load->view('header'); $this->load->view('main/index'); $this->load->view('footer'); } } ?> 
<?php class Main extends Controller { function Main() { parent::Controller(); } function index() { $this->load->view('header'); $this->load->view('main/index'); $this->load->view('footer'); } } ?>

As you can see from the code, we have to split the template so that when we connect header + footer we get a completely original template. And the border between them is the place where we will upload our templates. Thus, we will have this technique:
We always connect header and footer. Between them is a file that is in the folder with the name of the current controller and in the file with the name of the action. Such a structure is quite convenient and does not confuse.

Now you need to still share the template.
We create files header.php and footer.php and main / index.php in
WEB_ROOT / system / application / views / footer.php
WEB_ROOT / system / application / views / header.php
WEB_ROOT / system / application / views / main / index.php

Now we take our template, which I prepared and open it with the editor. Select all the contents up to the line.
<div id="center" class="column" >

Inclusive. All moved to the file header.php and save.
Now we are looking for the closing tag of this div (to make it easier to find, use editors with code highlighting)
And everything from it to the end is copied to footer.php.
In the file main / index.php we write simply TEST.
Now if you load baraholka.local, you should see our template, but now it is displayed using PHP.
It would seem that all is well. But look - in footer.php we keep the templates of the left and right columns, which is not correct. Therefore, we cut their contents from the footer
from <div id="left" class="column">
<div id="left" class="column">

before closing tag. Similarly, with the right column. The cut data is placed in left.php and right.php respectively. In place of the cutout we write:
<?$this->load->view('left');?>
and
<?$this->load->view('right');?>

respectively.

In Ukraine, popular as the Ukrainian language, and Russian. You don't want to offend any of the users, so you need to support two languages. The very idea is this - in the file we keep a set of predefined expression names. And we use them from different files depending on the current language. I liked the technical implementation of the person with the nickname mihailt - habrahabr.ru/blogs/codeigniter/30521 . I will take only part of his example - the implementation of multilingualism.
Extract from his article:
To begin, let's prepare our application:
expanding routes:
Open routes.php (/system/application/config/routes.php)
add the following lines:
$route['(ru|ua)'] = $route['default_controller'];
$route['(ru|ua)/(.+)'] = "$2";


Thus, now we can access any method, any controller in 3 ways:
baraholka.local / controller / action
http: // baraholka.local / en / controller / action
http: // baraholka.local / ua / controller / action

We also create 2 language files main_lang.php (/system/application/language/english/main_lang.php and /system/application/language/russian/main_lang.php)

Create the file My_Controller.php (/system/application/libraries/My_Controller.php)

 <? php
 class MY_Controller extends Controller {

	 var $ language;
	
	 public function MY_Controller ()
	 {
      	 parent :: Controller ();
      	 // $ this-> output-> enable_profiler (true);
      	 // define language
      	 $ lang = $ this-> uri-> segment (1);

      	 if ($ lang == 'ru')
		   {
        	 $ this-> language = $ lang;
      	 }
      	 else 
		 {
         	 $ this-> language = 'ua';
      	 }

      	 // load the desired language
      
      	 switch ($ lang)
		 {
      	
          case 'ru':
         
	          $ this-> lang-> load ('main', 'ru');
	          $ this-> config-> set_item ('language', 'ru');
	          break;

          default:
         
	          $ this-> lang-> load ('main', 'ua');
	          $ this-> config-> set_item ('language', 'ua');
	          break;

      	 }

     }
 }






Change our main controller. Now it must inherit from MY_Controller, not Controller.
Test our page. She must respond to
  1. baraholka.local / main / index
  2. baraholka.local / ua / main / index
  3. baraholka.local / ru / main / index


We turn to the last chapter in this part.

1.4 Database Structure



Now we need to think a little bit of the database structure.
First you need to create the database itself. I called her baraholka.
Since I plan to display a list of sections and categories in the left column, we need to describe them in the database.
I chose the table names cat_main and cat_sub.

cat_main // partition table


cat_sub // category table


I prepared dumps of these two tables, so all you have to do is execute the queries
baraholka.rv.ua/lesson/1/cat_main.sql
baraholka.rv.ua/lesson/1/cat_sub.sql

This part is all. In the next part, we will add a sidebar of categories using jQuery. And also we will make user authorization.

The series of articles "Flea market from scratch"
Part 1

Source: https://habr.com/ru/post/41620/


All Articles