Answer the question
In order to leave comments, you need to log in
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'];
}
}
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');
<? foreach ($article as $article): ?>
<a href="article.php?id=<?=$article['id_article']?>">
<?=$article['title']?>
</a> <br />
<?=$article['content']?><br /><br />
<? endforeach ?>
<? 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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question