Flea market from scratch. Part 1

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

    1.1 Getting Started


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

    For this site, I reserved the 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 will assume that we already have a template. And although in my case it’s not super designer - it’s suitable for training.

    I think the installation of denwer can be skipped. Now in the home folder where you installed Denwer, create the baraholka.local folder. There is a www folder in it.
    You also need to install Codeigniter itself. We go to codeigniter.com , download the latest version.
    Unpack the contents of the archive into the www folder.

    From now on, in the articles I will replace the path to the www folder with WEB_ROOT, that is, it’s standard Z: \ home \ baraholka.local \ www
    Also, the shortened form Codeigniter - CI will sometimes be used. We


    restart Apache and try to go to baraholka.local - if it worked, then everything is ok.
    So, I made up such a template - baraholka.rv.ua/lesson/1/template.zip . Unpack the archive also in WEB _ROOT.
    What do we have now? Template and installed Codeigniter.

    1.2 Configuring Codeigniter



    Now, we must prepare the Codeigniter settings for the correct operation. The WEB_ROOT / system / application / config file contains all the main configuration files.
    1. Edit autoload.php
      $autoload['libraries'] = array('database', 'session'); - мы все равно будем использовать базу данных и сессии. А остальное допишем потом.
    2. Edit config.php
      $config['base_url'] = "http://baraholka.local/"; - базовый урл, используется хелпером для создания ссылок
      $config['index_page'] = ""; - так как мы хотим иметь красивый URL, нужно будет еще добавить файлик .htaccess + изменить индексную страницу на пустую
      $config['charset'] = "Windows-1251"; - и хотя все гласят использовать UTF-8, мне пока это по душе

    3. Edit 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. Edit routes.php
      $route['default_controller'] = "main"; - как то более правильно, чем welcome.


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

    Whoever doesn’t figure out some of the values ​​- please go to the documentation - codeigniter.com/user_guide

    1.3 Default controller and template slicing


    Now we need to cut our template into the main parts - header, footer. Someone may ask:
    “Why do this? You can simply create a separate template for each new controller. ”
    And if you want to change the logo? Will you change each page?
    Therefore, let's create a new controller - WEB_ROOT / system / application / controllers / main.php
    It will have the following code: As you can see from the code, we must separate the template so that when connecting header + footer we get the completely original template. And the border between them is the place where we will upload our templates. Thus, we will have such a technique:
    load->view('header');
    		$this->load->view('main/index');
    		$this->load->view('footer');
    	}
    }
    ?>
    



    We always include 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 still need to divide the template.
    Create header.php and footer.php and main / index.php
    files 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 an editor. Select all the contents to the line


    Inclusive. We move everything to the header.php file and save it.
    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 main / index.php file, we simply write TEST.
    Now if you download baraholka.local, you should see our template, but now it is output using PHP.
    It would seem that all is well. But look - in footer.php we keep the left and right column templates, which is not right. Therefore, cut out their contents from the footer
    from


    to the closing tag. Similarly with the right column. The cut data is placed in left.php and right.php, respectively. At the place of the cutout we write: and, accordingly. In Ukraine, both the Ukrainian language and the Russian language are popular. I don’t want to offend any of the users, so I need to support two languages. The idea itself is this - in the file we hold a set of predefined expression names. And we use them from different files depending on the current language. I liked the technical implementation from a person with the nickname mihailt - habrahabr.ru/blogs/codeigniter/30521 . I will take only a part of his example - the implementation of multilingualism. Clipping from his article:
    load->view('left');?>

    load->view('right');?>





    To get started, we will prepare our application:
    expand the routes:
    Open routes.php (/system/application/config/routes.php)
    add the following lines: so now we can access any method, any controller in 3 ways: baraholka.local / controller / action http: // baraholka.local / ru / 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)
    $route['(ru|ua)'] = $route['default_controller'];
    $route['(ru|ua)/(.+)'] = "$2";











    output->enable_profiler(true);
          	// определяем язык
          	$lang = $this->uri->segment(1);
          	if($lang == 'ru')
    		  {
            	$this->language = $lang;
          	}
          	else 
    		{
             	$this->language = 'ua';
          	}
          	// подгружаем нужный язык
          	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 should inherit from MY_Controller, and not from 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


    Let's move on to the last chapter in this part.

    1.4 Database structure



    Now we need to think through at least a little 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 names of the cat_main and cat_sub tables.

    cat_main // partition table
    • INT main_id PK autoincrement // section id
    • VARCHAR (50) main_name_ua // section name in Ukrainian
    • VARCHAR (50) main_name_ru // section name in Russian


    cat_sub // category table
    • INT main_id // id of the section to which this category belongs
    • INT sub_id PK autoincrement // category id
    • VARCHAR (50) sub_name_ua // section name in Ukrainian
    • VARCHAR (50) sub_name_ru // section name in Russian


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

    That's all. In the next part, we will add a sidebar for categories using jQuery. And also do user authorization.

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


    Also popular now: