Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to link to the right page of a topic in a search result?
    primarykey
    data
    text
    <p>I have a search function in a forum where the search results will be displayed 10 at the time. The user can then look at the next or previous 10 search results. The results show different topics where the searched words are to be found. Everything works like I want it to.</p> <p>The issue is when I want the user to be able to click a result and end up on the right page of that topic. For instance post nr 14 in a certain topic must be viewed on page 2 ( using <code>LIMIT 10,10</code> in the SQL query on the topic page). I send the <code>LIMIT</code> parameter as a $_GET in the link.</p> <p>How can I retrieve the row number of each topic in the results out of the total numbers of that specific topic when ordering by the date it was posted? Everything is always displayed in that order. I would like to use <code>$nr = $nr-1; //and then</code></p> <pre><code> $limit = floor($nr / 10) * 10; </code></pre> <p>on that number to be able to send the right <code>LIMIT</code> parameter with the link in the search result.</p> <p>Here's the PDO used to get the search results:</p> <pre><code>$query = 'SELECT t.topic_id, t.topic_cat, t.topic_subject, p.post_content, p.post_id, UNIX_TIMESTAMP(p.post_date) AS post_date FROM posts p LEFT JOIN topics t ON p.post_topic = t.topic_id WHERE p.post_content LIKE :search OR t.topic_subject LIKE :search ORDER BY UNIX_TIMESTAMP(p.post_date) DESC LIMIT :start, :size'; $statement = $db-&gt;prepare($query); $statement-&gt;bindValue(':search', '%'.$search.'%'); $statement-&gt;bindValue(':start', $start2, PDO::PARAM_INT); $statement-&gt;bindValue(':size', $pagesize, PDO::PARAM_INT); $statement-&gt;execute(); while ($row = $statement-&gt;fetch(PDO::FETCH_ASSOC)) { $array[$row['topic_subject']][] = $row['post_id']; $nr = count($array[$row['topic_subject']]) - 1; echo '&lt;div class="search_result"&gt;&lt;div class="search_topic"&gt;&lt;a href="topic_view.php?id=' . $row['topic_id'] . '&amp;amp;cat_id=' . $row['topic_cat'] . ' &amp;amp;start=' . floor($nr / 10) * 10 . '#anchor_' . $row['post_id'] . '"&gt;' . $row['topic_subject'] . '&lt;/a&gt;&lt;span style="float:right;color:#696969"&gt;' . date("M d, Y",$row['post_date']) . '&lt;/span&gt;&lt;/div&gt;&lt;div class="search_post"&gt;' . $row['post_content'] . '&lt;/div&gt;&lt;/div&gt;'; } } $statement-&gt;closeCursor(); </code></pre> <p>It is the <code>start</code> parameter in the link that I somehow need to grab in the query so I don't have to do a new DB call for each <code>post_id</code> in the <code>while loop</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload