A
A
Alexander Sisyukin2016-01-25 22:36:46
PHP
Alexander Sisyukin, 2016-01-25 22:36:46

Article announcement (working with an array)?

please help me, I have been walking all day and I don’t understand how to cut a string from a two-dimensional array and display it in a view, and so the code itself that we have in the model:

function articles_all()
{
    // Запрос.
    $query = "SELECT * FROM articles ORDER BY id_article DESC";
    $result = mysql_query($query);

    if (!$result)
        die(mysql_error());

    // Извлечение из БД.
    $n = mysql_num_rows($result);
    $articles = array();

    for ($i = 0; $i < $n; $i++)
    {
        $row = mysql_fetch_assoc($result);
        $articles[] = $row;
    }

    return $articles;
}


// Короткое описание статьи
//
function articles_intro($articles)
{
   

        if (strlen($article['content']) > 150)
        {
            return mb_substr($article['content'], 0, 150, 'UTF-8');

        } else
        {
            return $article['content'];
        }
    
}

in controller we have
include_once('startup.php');
include_once('model.php');

// Установка параметров, подключение к БД, запуск сессии.
startup();

$articles = articles_all();
// Пытаюсь вот так обрезать то есть перезаписать поле в массиве.
foreach ($articles as $article)
    $article['content'] = articles_intro($article);


// Кодировка.
header('Content-type: text/html; charset=utf-8');

// Вывод в шаблон.
include('theme/index.php');

well, and a twist:
<? foreach ($article as $article): ?>
      
        <a href="article.php?id=<?=$article['id_article']?>">
        <?=$article['title']?>	
        </a> <br />
                <?=$article['content']?><br /><br />
      
    <? endforeach ?>

how can I make it so that the view does not communicate with the model, and the processing takes place in the controller; I don’t want to insert a crutch into a view like
<? foreach ($article as $article): ?>
      
        <a href="article.php?id=<?=$article['id_article']?>">
        <?=$article['title']?>	
        </a> <br />
                <?=articles_intro($article['content'])?><br /><br />
      
    <? endforeach ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrzej Wielski, 2016-01-25
@wielski

You have a strange understanding of models, controllers, and views.
Use Laravel, don't waste your nerves.
Well, or at least OOP ...
And in the code that you provided - the view does not "communicate" with the model. In your "controller" you set the content value as the result of the articles_intro function, so in your "view" you should display this value.
May the gods forgive me for calling it these terms.

D
Denis, 2016-01-25
@prototype_denis

foreach ($articles as &$article) {
    $article['content'] = articles_intro($article);
}
unset($article);

PS php.net/manual/ru/control-structures.foreach.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question