Searching by ID hack?

In this board you can talk about general questions about phpMyFAQ

Moderator: Thorsten

Post Reply
monkeybot
Posts: 22
Joined: Mon Nov 29, 2004 9:58 pm

Searching by ID hack?

Post by monkeybot »

Thorsten:

You mention you had a hack to search by ID # or said it was easy. Can you share it with us? (admin and public)

Thanks.

Our PMF Board is close to be public! :D
Thorsten
Posts: 15727
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

sure... but I didn't had the time to do it...

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
monkeybot
Posts: 22
Joined: Mon Nov 29, 2004 9:58 pm

Post by monkeybot »

Here is the ID search hack we implemented in our PMF:

In the functions.php find: (line 1261 to 1320)

if (0 == $num) {

$keys = preg_split("/\s+/", $begriff);
$numKeys = count($keys);
$where = '';
for ($i = 0; $i < $numKeys; $i++) {

if (strlen($where) != 0 ) {
$where = $where." OR ";
}

$where = $where.'('.SQLPREFIX."faqdata.thema LIKE '%".$keys[$i]."%' OR ".SQLPREFIX."faqdata.content LIKE '%".$keys[$i]."%' OR ".SQLPREFIX."faqdata.keywords LIKE '%".$keys[$i]."%') AND ".SQLPREFIX.'faqdata.active = \'yes\'';
}

$where = " WHERE (".$where.") AND active = 'yes'";
$query = 'SELECT '.SQLPREFIX.'faqdata.id AS id, '.SQLPREFIX.'faqdata.lang AS lang, '.SQLPREFIX.'faqcategoryrelations.category_id AS category_id, '.SQLPREFIX.'faqdata.thema AS thema, '.SQLPREFIX.'faqdata.content AS content FROM '.SQLPREFIX.'faqdata LEFT JOIN '.SQLPREFIX.'faqcategoryrelations ON '.SQLPREFIX.'faqdata.id = '.SQLPREFIX.'faqcategoryrelations.record_id AND '.SQLPREFIX.'faqdata.lang = '.SQLPREFIX.'faqcategoryrelations.record_lang '.$where;
$result = $db->query($query);
$num = $db->num_rows($result);
}

$pages = ceil($num / $PMF_CONF["numRecordsPage"]);
$last = $seite * $PMF_CONF["numRecordsPage"];
$first = $last - $PMF_CONF["numRecordsPage"];
if ($last > $num) {
$last = $num;
}

if ($num > 0) {
if ($num == "1") {
$output .= "<p>".$num.$PMF_LANG["msgSearchAmount"]."</p>\n";
} else {
$output .= "<p>".$num.$PMF_LANG["msgSearchAmounts"]."</p>\n";
}
if ($pages > 1) {
$output .= "<p><strong>".$PMF_LANG["msgPage"].$seite." ".$PMF_LANG["msgVoteFrom"]." ".$pages.$PMF_LANG["msgPages"]."</strong></p>";
}
$output .= "<ul class=\"phpmyfaq_ul\">\n";

$counter = 0;
$displayedCounter = 0;
while (($row = $db->fetch_object($result)) && $displayedCounter < $PMF_CONF['numRecordsPage']) {
$counter ++;
if ($counter <= $first) {
continue;
}
$displayedCounter++;

$rubriktext = $tree->getPath($row->category_id);
$thema = PMF_htmlentities(chopString($row->thema, 15),ENT_NOQUOTES, $PMF_LANG['metaCharset']);
$content = stripslashes(chopString(strip_tags($row->content), 25));
$begriff = str_replace(array('^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']'), '', $begriff);
$begriff = preg_quote($begriff, '/');
if (strlen($begriff) > 1) {
$thema = preg_replace_callback('/(((href|src|title|alt|class|style|id|name)="[^"]*)?'.$begriff.'(?(1).*"))/mis', "highlight_no_links", $thema);
$content = preg_replace_callback('/(((href|src|title|alt|class|style|id|name)="[^"]*)?'.$begriff.'(?(1).*"))/mis', "highlight_no_links", $content);
}
$output .= "<li><strong>".$rubriktext."</strong>: <a href=\"".$_SERVER["PHP_SELF"]."?".$sids."action=artikel&cat=".$row->category_id."&id=".$row->id."&highlight=".$begriff."\">".stripslashes($thema)."...</a><br /><div style=\"font-size: 10px;\"><strong>".$PMF_LANG["msgSearchContent"]."</strong> ".stripslashes($content)."...</div><br /></li>\n";
}
$output .= "</ul>\n";
}


And replace it with: (Red is what has been modified)


if (0 == $num) {

$keys = preg_split("/\s+/", $begriff);
$numKeys = count($keys);
$where = '';
for ($i = 0; $i < $numKeys; $i++) {

if (strlen($where) != 0 ) {
$where = $where." OR ";
}

$where = $where.'('.SQLPREFIX."faqdata.thema LIKE '%".$keys[$i]."%' OR ".SQLPREFIX."faqdata.content LIKE '%".$keys[$i]."%' OR ".SQLPREFIX."faqdata.keywords LIKE '%".$keys[$i]."%') AND ".SQLPREFIX.'faqdata.active = \'yes\'';
}

if (is_numeric($begriff))
{
$where = " WHERE faqdata.id =" .$begriff. " AND active = 'yes'";
$query="SELECT faqdata.id AS id, faqdata.lang AS lang, 0 AS category_id, faqdata.thema AS thema, faqdata.content AS content FROM faqdata " .$where;
}
else
{
$where = " WHERE (".$where.") AND active = 'yes'";
$query = 'SELECT '.SQLPREFIX.'faqdata.id AS id, '.SQLPREFIX.'faqdata.lang AS lang, '.SQLPREFIX.'faqcategoryrelations.category_id AS category_id, '.SQLPREFIX.'faqdata.thema AS thema, '.SQLPREFIX.'faqdata.content AS content FROM '.SQLPREFIX.'faqdata LEFT JOIN '.SQLPREFIX.'faqcategoryrelations ON '.SQLPREFIX.'faqdata.id = '.SQLPREFIX.'faqcategoryrelations.record_id AND '.SQLPREFIX.'faqdata.lang = '.SQLPREFIX.'faqcategoryrelations.record_lang '.$where;
}


$result = $db->query($query);
$num = $db->num_rows($result);
}

$pages = ceil($num / $PMF_CONF["numRecordsPage"]);
$last = $seite * $PMF_CONF["numRecordsPage"];
$first = $last - $PMF_CONF["numRecordsPage"];
if ($last > $num) {
$last = $num;
}

if ($num > 0) {
if ($num == "1") {
$output .= "<p>".$num.$PMF_LANG["msgSearchAmount"]."</p>\n";
} else {
$output .= "<p>".$num.$PMF_LANG["msgSearchAmounts"]."</p>\n";
}
if ($pages > 1) {
$output .= "<p><strong>".$PMF_LANG["msgPage"].$seite." ".$PMF_LANG["msgVoteFrom"]." ".$pages.$PMF_LANG["msgPages"]."</strong></p>";
}
$output .= "<ul class=\"phpmyfaq_ul\">\n";

$counter = 0;
$displayedCounter = 0;
while (($row = $db->fetch_object($result)) && $displayedCounter < $PMF_CONF['numRecordsPage']) {
$counter ++;
if ($counter <= $first) {
continue;
}
$displayedCounter++;

$rubriktext = $tree->getPath($row->category_id);
$thema = PMF_htmlentities(chopString($row->thema, 15),ENT_NOQUOTES, $PMF_LANG['metaCharset']);
$content = stripslashes(chopString(strip_tags($row->content), 25));
$begriff = str_replace(array('^', '.', '?', '*', '+', '{', '}', '(', ')', '[', ']'), '', $begriff);
$begriff = preg_quote($begriff, '/');
if (strlen($begriff) > 1) {
$thema = preg_replace_callback('/(((href|src|title|alt|class|style|id|name)="[^"]*)?'.$begriff.'(?(1).*"))/mis', "highlight_no_links", $thema);
$content = preg_replace_callback('/(((href|src|title|alt|class|style|id|name)="[^"]*)?'.$begriff.'(?(1).*"))/mis', "highlight_no_links", $content);
}
if ($row->category_id==0)
$output .= "<li><strong>".$PMF_LANG["msgArticleId"].$row->id." </strong> <a href=\"".$_SERVER["PHP_SELF"]."?".$sids."action=artikel&cat=".$row->category_id."&id=".$row->id."&highlight=".$begriff."\">".stripslashes($thema)."...</a><br /><div style=\"font-size: 10px;\"><strong>".$PMF_LANG["msgSearchContent"]."</strong> ".stripslashes($content)."...</div><br /></li>\n";
else
$output .= "<li><strong>".$rubriktext."</strong>: <a href=\"".$_SERVER["PHP_SELF"]."?".$sids."action=artikel&cat=".$row->category_id."&id=".$row->id."&highlight=".$begriff."\">".stripslashes($thema)."...</a><br /><div style=\"font-size: 10px;\"><strong>".$PMF_LANG["msgSearchContent"]."</strong> ".stripslashes($content)."...</div><br /></li>\n";
}
$output .= "</ul>\n";
}


Basically if what you put on the search box is only a number, it looks for the particular article id. When the query is done it shows the article once even if it belongs to multiple categories. Also shows the article ID and strips the category as well.
If the search box has a character other than a number it follows the normal search.

To show the article ID in your articles you will have to add the following:

In the language_en.php
I added: $PMF_LANG["msgArticleId"] = "Article ID: ";
under // Contentpages

In the artikel.php I added the red line
$tpl->processTemplate ("writeContent", array(
"writeId" => $PMF_LANG["msgArticleId"].$id,
"writeRubrik" => $writeCategory,

In order for the article id to show: In the template artikel.tpl add
{writeId} whereever you want the article ID to show

Now I only need to do the search ID hack in the admin area. :?
Thorsten
Posts: 15727
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

thanks a lot... I'll try to implement that, too.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
monkeybot
Posts: 22
Joined: Mon Nov 29, 2004 9:58 pm

Post by monkeybot »

Now that you have implemented the hack "search for article id" for users, add this code to be able to find articles ID in the admin area:

Find the file admin/record.show.php and add the code in red. (line 50)

} else if (isset($_REQUEST["aktion"]) && $_REQUEST["aktion"] == "view" && isset($_REQUEST["suchbegriff"]) && $_REQUEST["suchbegriff"] != "") {
/**
* This function generates a result set based on ID search in the admin area
* @author Gary Marshall
* @since 2005-12-02
* Start
*/
$begriff = strip_tags($_REQUEST["suchbegriff"]);

if (is_numeric($begriff))
{
$result = $db->search(SQLPREFIX."faqdata",
array(SQLPREFIX."faqdata.id",
SQLPREFIX."faqdata.lang",
SQLPREFIX."faqdata.thema",
SQLPREFIX."faqdata.content"),
"",
"",
array(SQLPREFIX."faqdata.id"),
$begriff);
/**
* This function generates a result set based on ID search in the admin area
* @author Gary Marshall
* @since 2005-12-02
* End
*/

}
else
{

$result = $db->search(SQLPREFIX."faqdata",
array(SQLPREFIX."faqdata.id",
SQLPREFIX."faqdata.lang",
SQLPREFIX."faqcategoryrelations.category_id",
SQLPREFIX."faqdata.thema",
SQLPREFIX."faqdata.content"),
SQLPREFIX."faqcategoryrelations",
array(SQLPREFIX."faqdata.id = ".SQLPREFIX."faqcategoryrelations.record_id",
SQLPREFIX."faqdata.lang = ".SQLPREFIX."faqcategoryrelations.record_lang"),
array(SQLPREFIX."faqdata.thema",
SQLPREFIX."faqdata.content",
SQLPREFIX."faqdata.keywords"),
$begriff);
}

$laktion = "view";
$internalSearch = "&search=".$begriff;


In the Inc folder find the mysql.php and add the code in red:

if (!$this->version_check("4.0.1")) {
$against = "('".$string."')"; // Search with MySQL 3.23.23+
} else {
$against = "('".$string."' IN BOOLEAN MODE)"; // Search with MySQL 4.0.1+
}
/**
* This function generates a result set based on ID search in the admin area
* @author Gary Marshall
* @since 2005-12-02
* Start
*/
if ($table == 'faqdata' && is_numeric($string))
{
$query = "SELECT ".$fields." FROM ".$table." WHERE faqdata.id=".$string;
}
else
{
/**
* This function generates a result set based on ID search in the admin area
* @author Gary Marshall
* @since 2005-12-02
* End
*/

$query = "SELECT ".$fields." FROM ".$table.$joined." WHERE MATCH (".$match.") AGAINST ".$against;

if (!empty($where)) {
$query .= " AND (".$where.")";
}
}
return $this->query($query);
}

Don't forget the red bracket at the end or you will get an error.
Thorsten
Posts: 15727
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

the search by ID is added to phpMyFAQ 1.5.5 for the admin backend. I don't think it's useful in the frontend so I didn't added it there.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
monkeybot
Posts: 22
Joined: Mon Nov 29, 2004 9:58 pm

Post by monkeybot »

In our case is useful because the support representative can give a customer the ID number for the specific article hence speeding the support call. Also if there is an error or needs to update an specific article the customer can find the record quickly to advise the support center so it works both ways.
Its great that you added this hack and if somebody needs to do the other way around, it can always find it here. :wink: Thanks!
Thorsten
Posts: 15727
Joined: Tue Sep 25, 2001 11:14 am
Location: #phpmyfaq
Contact:

Post by Thorsten »

Hi,

okay, I understand. In your way of using phpMyFAQ your solution makes sense.

bye
Thorsten
phpMyFAQ Maintainer and Lead Developer
amazon.de Wishlist
Post Reply