Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:09:58+00:00 2026-06-14T20:09:58+00:00

I want to have a php page that display multiple keywords (checkboxes) for example

  • 0

I want to have a php page that display multiple keywords (checkboxes) for example areas such as Narberth, Pembrokeshire, Carmarthenshire etc as well as other things.

So far i have created a way to do this and display the data separately in tables. What I’m having trouble with is showing the rows with multiple keywords associated with them first.

So if the user searched “Narberth” and “Pembrokeshire” the results would show any documents with both those keywords in first then anything with just “narberth” and just “pembrokshire” afterwords.

I listed my code for what I’ve worked out so far :

<form method="post">
<table border="1px solid" width="60%">
<tr>
<td><input type="checkbox" name="keywords[]" value="1">Narberth </td>
<td><input type="checkbox" name="keywords[]" value="2">James Brothers </td>
<td><input type="checkbox" name="keywords[]" value="3">Mabinogion </td>
</tr>
<tr>
<td><input type="checkbox" name="keywords[]" value="4">Pembrokeshire </td>
<td><input type="checkbox" name="keywords[]" value="5">Pubs </td>
<td><input type="checkbox" name="keywords[]" value="6">Coastal Paths </td>
</tr>
</table>
<br>
<input type="submit" name = "submit">

<?php
if (isset($_POST['submit'])) {

foreach( $_REQUEST['keywords'] as $keywordID )
{
$result = mysql_query("SELECT * FROM tbl_index m
inner join tbl_filekeywords r on m.file_id = r.file_id
where r.keyword_id = '".$keywordID."'");

$keywordname = mysql_query ("SELECT keyword FROM tbl_keywords WHERE keyword_id = '".$keywordID."'");
$KeywordName = mysql_fetch_row($keywordname);
$KeywordName = $KeywordName[0];

echo "<table border='1' width='50%'>
<th colspan='2'>".$KeywordName."</th>
<tr>
<th>File ID</th>
<th>Filename</th>
</tr>";

while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['file_id'] . "</td>";
echo "<td>" . $row['file'] . "</td>";
echo "</tr>";
}
echo "</table><br>";
mysql_free_result($result);
}
}?>

P.S There is another alternative that i developed that lumps all the files into one table which is fine but i still want to stop the same files appearing multiple times for each keyword and would rather the ones with more than one keyword to be prioritized and displayed on top.

<?php
if(isset($_POST['keywords']) && !empty($_POST['keywords'])){
foreach($_POST['keywords'] as $key=>$value){
if($value==1) $keywords[] = "keyword_id='".mysql_escape_string($key)."'";
}
$keywords = implode(' OR ', $keywords);
}
$query = "SELECT * FROM tbl_index m inner join tbl_filekeywords r on m.file_id = r.file_id WHERE $keywords";
$result = mysql_query($query);

echo "<table border='1' width='50%'>
<tr>
<th>File ID</th>
<th>Filename</th>
<th>Keyword_ID</th>
</tr>";

while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['file_id'] . "</td>";
echo "<td>" . $row['file'] . "</td>";
echo "<td>" . $row['keyword_id'] . "</td>";
echo "</tr>";
}
echo "</table><br>";
mysql_free_result($result);
?>

And finally the tables are set up like:

tbl_index : file_id, file

tbl_keywords : keyword_id, keyword

tbl_filekeywords : file_id, keyword_id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:09:59+00:00Added an answer on June 14, 2026 at 8:09 pm

    First of all, wellcome to SO. Post answer delay is due to non synthesized question. In next questions remember to include only relevant code to question.

    Lets supose that tbl_index and tbl_filekeywords tables are in 1:N relationship.

    First of all, in php you can get $keywords as a string list of selected keywords Example:

    $keywords = " 1, 5, 12,  24"
    

    Now you can rank tbl_filekeywords in this way. We named rank view:

    select 
      r.file_id, 
      sum( case when r.keyword_id in ( $keywords  ) then 1 else 0 end ) as rank
    from
      tbl_filekeywords r
    group by 
      r.file_id
    

    Last step is to join rank view with tbl_index:

       select t.*
       from 
         tbl_index t left outer join
         ( HERE PREVIUS QUERY ) r
              on r.file_id = t.file_id
       order by
         coalesce( r.rank, 0 )
    

    Let us to know if this runs and it is a solution for you. If not, explain how can we improve answer!

    Edited due OP Comment

    I don’t have your database schema, but it seems that file_id is PK for tbl_index. If this is the case, the query don’t produce duplicates. Notice that first query, rank view, only has a row for each file_id due to group by clause. Because this, join don’t produce duplicates. For me the final query is:

       select t.*
       from 
         tbl_index t inner join                 <--No dups
         ( 
           select 
             r.file_id, 
             count(*) as rank
           from
             tbl_filekeywords r
           where
             r.keyword_id in ( $keywords  )
           group by 
             r.file_id                          <--No dups
         ) r2
              on r2.file_id = t.file_id
       order by
          r2.rank
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a php page that contains a table which i want to display
I am working with php and want to have a page that has a
i have a variable $cartTotal in cart.php page i want to use that same
Basically I have a single page on my site that I want any php
I want to redirect my browser to a PHP page such that when the
I have a PHP page where i want to open a file on a
I have a PHP page and I want to share some data between pages
I have my index.php page and just afrer my header i want to include
I have a string that I want to have PHP read as a piece
I have a page that I want to scrape that has the following format:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.