Very Simple search

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 2, 2013   (RSS)

By rui - April 2, 2013

Hi from Portugal,

I´m a designer... i don't speak your "language".

I´m trying to do a simple search, on my index.php i have the input.

<form action="search.php" type="post">
    <input type="text" name="q" id="search" />
    <input type="submit" value="search" />
</form>

My problem is, i dont know what to put on the search.php so i can see the result list. I have a table "music" and i want to search the "title" field.

Can someone help me? please.

tks

Rui

By gregThomas - April 2, 2013

Greetings from Canada,

First you need to make a slight alteration to your search form:

  <form action="scratch.php" type="post">
     <input type="text" name="title_keyword" id="search" />
     <input type="submit" value="search" />
  </form>

So I've changed the name of the text field to title_keyword. So that the getRecords function will know it should search the title field, and return any results where the searched word is somewhere in the field. Below is the code that should be in your search.php file:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsAdmin/lib/viewer_functions.php';
  $dirsToCheck = array('C:/wamp/www/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records from 'music'
  list($musics, $musicMetaData) = getRecords(array(
    'tableName'   => 'music',
    'loadUploads' => true,
    'allowSearch' => true,
  ));

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  </head>
  <body>
    <form action="scratch.php" type="post">
        <input type="text" name="title_keyword" id="search" />
        <input type="submit" value="search" />
    </form>
    
    <?php if($musics): ?>
      <ul>
      <?php foreach($musics as $music): ?>
        <li><a href="linkToDetailPage.php?num=<?php echo $music['num']; ?>" ><?php echo $music['title']; ?></a></li>
      <?php endforeach; ?>
      </ul>
    <?php endif; ?>
  </body>
</html>

So I've used a getRecords function to retrieve the results from the music section, I've set the allowSearch variable to true in it so that the section can be searched if any search data is posted to the page. The getRecords function will return an array of results which are stored in the variable $musics. In the main body of the page I've used a foreach loop to cycle through the $musics array and display each entry that is in it.

You can read more about using the getRecords function and carrying out searches here: http://www.interactivetools.com/docs/cmsbuilder/viewer_search.html

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com