Answer the question
In order to leave comments, you need to log in
How to combine in a loop and check for a match?
Hello.
// подключаюсь к db и получаю все записи с переданным id
$connect = $db->query("SELECT * FROM `o_post` WHERE `id` = '$an_id'");
$get = $db->get_row($connect);
// далее создаю массив, для работы с ним в цикле
$categories = explode(',', $get['category']);
// здесь я вывожу все id, которые принадлежат посту
foreach ($categories as $id) {
echo $id . '<br>';
}
// а здесь получаю все записи, которые имеют вторую категорию и вывожу рандомно не более пяти записей
$n = 2; // указал вручную, чтобы работало
$all = $db->query("SELECT * FROM `o_post` WHERE `category` LIKE '%,$n,%' OR `category` LIKE '%,$n' OR `category` LIKE '$n,%' ORDER BY RAND() LIMIT 5");
$from = $db->get_row($all);
while ($from) {
echo $from['title'] . '<br>';
$from = $db->get_row($all);
}
How to loop through the resulting array and display from the table a maximum of 5 records whose post id is equal to the one sent ($an_id) and in the category column there are at least two matches in the array (it should be taken into account that categories are stored in the table separated by commas )?
Answer the question
In order to leave comments, you need to log in
The crooked organization of records makes it inconvenient and slow to work with your database. if you have category 2, 12 and 20 then $n = 2 will select all the categories I listed. (homework - think about why it will happen)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question