Hello everyone, such a problem, I have the code:

HTML:

<button id="more-news">Загрузить еще</button> <script> var lng = '<?=$lang?>'; var last_id = '<?php $arr = end($front_page_news); echo $arr['id']; ?>'; </script> <div id="responsecontainer"> </div> 

Js:

 $("#more-news").click(function(e) { e.preventDefault(); $.ajax({ //create an ajax request to load_page.php type: "GET", url: "/getnews.php", dataType: "html", //expect html to be returned data: { lang: lng, last_id: last_id, limit: $(this).attr('data-lim'), ids: dataN }, success: function(response){ var content = document.getElementById("responsecontainer"); content.innerHTML = content.innerHTML+response; //$("#responsecontainer").html(response); $('#more-news').attr('data-lim', Number($('#more-news').attr('data-lim')) + 10); //alert(response); } }); 

PHP:

 $q = $db->query('SELECT * FROM news WHERE id NOT IN (" . implode(\',\', $ids) . ") ORDER BY date_show DESC LIMIT '.$lim.' OFFSET 50'); //$q = $db->query('SELECT id, title_ru, title_md, body_ru, body_md, is_video, thumb_ru, thumb_md, date_show,title_ru, title_md //FROM news GROUP BY id'); $lang = (isset($_GET['lang']) && $_GET['lang'] == 'ru') ? 'ru' : 'md'; $isHidden = false; $cnt = 1; if($q->num_rows > 0): foreach ($q as $item): $dtNews = new \DateTime($item['date_show']); // if (in_array($item['id'], $ids) || $dtNews > $dtNow) { // $isHidden = true; // break; // } $qs = $db->query("SELECT * FROM news_categories WHERE news_id = " . $item['id']); if($qs->num_rows > 0) { $dd = $qs->fetch_assoc(); $qss = $db->query("SELECT * FROM categories WHERE id = " . $dd['categories_id']); $dd2 = $qss->fetch_assoc(); if(in_array('Скрытые новости', $dd2) || in_array('Hidden News', $dd2)) { $isHidden = true; break; } } $dtShow = $dtNews->format("Ymd"); //$dtShow = \Noitools\Utils::timeFromPost($item['date_show'], $lang); if($item['active'] == '1'): if($item['body_' . $lang] != ''): if(!$isHidden): ?> <div class="article-item" data-news="<?= $item['id']; ?>"> <a href="/<?=$lang?>/news_id/<?=$item['id']?>" class="link-img-wrap<?= ($item['is_video']) ? ' video-icon-medium' : ''; ?>"> <img src="/uploads/newsthumbs/760_500/<?=$item['id']?>.jpg" alt=""> </a> <div class="details-post"> <i><?=$dtShow?></i> <a href="news_cat/<?= $dd2['id'] ?>" class="standard-hover-link"><?= $dd2['title_' . $lang] ?></a> </div> <h3><a href="/<?=$lang?>/news_id/<?=$item['id']?>"><span class="detail-title"><?=$dtShow?></span> <?= $item['title_' . $lang] ?></a></h3> <p class="small-text"> <?= mb_substr(strip_tags($item['body_' . $lang]), 0, 180, 'utf-8') ?> </p> </div> <?php endif; endif; endif; $cnt++; endforeach; endif; ?> 

For some reason, I have duplicated records from the database, that is, I press the button and the same records are unloaded. How can I solve the problem?

  • I can advise redbeanphp or another orm libu. 2017 - PDO is used only for low-level connection to the database. - Steve

2 answers 2

Do you want to implement AJAX loading by n records when clicking on the "button"? Then the approach and solution are fundamentally wrong. It is better to read the articles on the same blogs, because the solution is not so trivial, but if in brief:

  1. The "button" should contain a link to page navigation, which will change every time through JS, for example, news.php? P = 2).
  2. Clicking on it leaves a request to the server via ajax'a.
  3. The server takes the value transferred to it and limits the request for receiving data from the table, with mandatory checking for the last record in it, otherwise you can endlessly press the "button" at the end in the hope of getting another portion of records.
  4. It then prints the output of the resulting values, which js displays on the page under the previously loaded records.
  • $ lim variable so there is and it changes, look more closely at the code, but for some reason LIMIT $ lim does not work - BlogSER
  • in the sense of not loading the data - BlogSER
  • LIMIT takes 2 values ​​- the starting position and the total number of records required for output. You also get that he returns $lim records every time, but js does not change it for you (or you do not receive it updated and do not transfer it to the script again) - DaemonHK

Because

  1. the value of the last_id variable last_id assigned once - at the initial page load. You should update it after each $.ajax ;

  2. the last_id parameter coming to the server in the ajax call, php code is not used in any way.

  • How can I do that? - BlogSER
  • The fact of the matter is that it is not used, I removed it, because there is no place to use it - BlogSER