V
V
vaflya2015-10-21 11:58:33
PHP
vaflya, 2015-10-21 11:58:33

Website php + AJAX, have you arrived?

Throughout the week, the guys helped me, answering my questions (thanks again Alexey Skobkin , Andrey Surzhikov , MaEcTPo !).

I understood everything how it works and it works, but now the problem is in the implementation. After a couple of hours, it just became hard to look at the code, I even heard Satan screaming - "You are the best!".

In general code:

<?php

function news(){
    $data = '';
    $data .= '<div style="color: #23374f; font-weight: bold; font-size: 15px; padding: 0 0 20px 10px;">Политика</div><div>';

    $sql = mysql_query("SELECT news_id, title, description, photo FROM news WHERE category='1' ORDER BY news_id DESC LIMIT 10") or die(mysql_error());  
    $kol = mysql_num_rows($sql);
    $margin = 0;
    for ($i=1; $i <= $kol; $i++) {
        $margin++;
        $news = mysql_fetch_assoc($sql);
        if ($margin == 5) {$data .= '<div class="card news fl" style="margin-right: 0;">'; $margin = 0;}
        else {$data .=  '<div class="card news fl">';}
        $data .=  '<div class="newsPhoto">';
        $data .=  '<img src="'.getImgUrl($news['photo']).'">';
        $data .=  '</div>';
        $data .=  '<div class="card_content">';
        $data .=  '<div class="newsTitle">'.$news['title'].'</div>';
        $data .=  '<div class="newsText">'.$news['description'].'</div>';
        $data .=  '</div>';
        $data .=  '</div>';
    }    
        
 
    $data .= '</div>';
    return $data;
}

function news_menu(){   
        $data ='<div class="card" style="height: 30px;">
            <a href="#">Город</a>
            <a href="#">Происшествия</a>
            <a href="#">Закон</a>
            <a href="#">Мероприятия</a>
            <a href="#">Наука и техника</a>
            <a href="#">Игры</a>
            <a href="#">Авто</a>
            <a href="./addNews">[Добавить новость]</a>
        </div>';
    return $data;
            
}

if (isAJAX() == false) {
echo head('Новости',array('common.css', 'news.css'),array('common.js'));
echo body();
echo news_menu();
echo news();
echo footer();
}

else {
    $data = news_menu().news();
    $dataAJAX =  array ('Новости',array('common.css', 'news.css'),array('common.js'),$data);
    echo json_encode($dataAJAX);
}
?>


How can this be brought to a divine form? I had to put the entire HTML code of the page in quotes in order to give the AJAX request to json or stupidly display it (by direct request of the page).

ps Mom, why am I ugly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Skobkin, 2015-10-21
@vaflya

There is very, very much that can be brought to the divine form. You should probably try some not very complex MVC framework to get started.
So far, all the code is a mess, a little easier to maintain than if everything was in one file, but still much more complicated than the solutions already worked out.

$data .=  '<div class="newsPhoto">';
        $data .=  '<img src="'.getImgUrl($news['photo']).'">';
        $data .=  '</div>';
        // ...

In general, if you really want to do all this in PHP, you can either use Heredoc / Nowdoc, or simply write it in quotes without unnecessary concatenations of the code with itself. Although it is better, of course, to do all this in the View layer (see MVC) - after trying it, you will quickly understand how cool it is when logic (data operations) and presentation (data output) are separated.
Use PDO and prepared queries with parameter binding.
And you also have single quotes everywhere (which is good), but here you have double quotes. What for? A single quote can be escaped with a backslash.
if (isAJAX() == false) {
echo head('Новости',array('common.css', 'news.css'),array('common.js'));
echo body();
echo news_menu();
echo news();
echo footer();
}

This is very nice. You almost came to the templates :)
I can recommend that you do three things to gain an understanding of how the code looks better:
1. Familiarize yourself with PSR-2 at least.
2. Read and try to understand Php The Right Way as much as possible .
3. Rewrite all this on some more or less modern, but not yet very complex framework that has MVC out of the box. In memory, for the time being, pops up, for example, FuelPHP .
Well, then, as you figure it out, it's just to gain experience, watch how others do it (open source is always helpful), try to do some small things for yourself in order to figure something out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question