search a different database

Get help with installation and running phpwcms here. Please do not post bug reports or feature requests here.
Post Reply
le_fou
Posts: 6
Joined: Wed 21. Mar 2007, 17:56

search a different database

Post by le_fou »

Hi folks, i wanna support my visitors with an additional search-form to work with a different database called bibliographie:

In wcms i have included the search-php by

[PHP]
include('search.php');
[/PHP]

which works well. The search.php looks like this:

Code: Select all

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("bibliographie") or die(mysql_error());

if(isset($_GET['s'])){

echo "<title>Suche nach: \"".$_GET['s']."\"</title>Suche nach: \"".$_GET['s']."\"<br><hr>";
$q = mysql_query("SELECT * FROM `literaturrecherche` WHERE `Titel` LIKE '%$_GET[s]%' OR `Autor` LIKE '%$_GET[s]%' OR `ln` LIKE '%$_GET[s]%' OR `Hrsg` LIKE '%$_GET[s]%' OR `Jahr` LIKE '%$_GET[s]%' OR `Untertitel` LIKE '%$_GET[s]%' OR `Ort_Verlag` LIKE '%$_GET[s]%' OR `Land` LIKE '%$_GET[s]%' OR `Abstract` LIKE '%$_GET[s]%' OR `Schlagworte` LIKE '%$_GET[s]%' OR `Art_Sprache` LIKE '%$_GET[s]%'") or die(mysql_error());

if(!is_array(mysql_fetch_array($q))){

echo "<br><BR>Es wurde kein Datensatz gefunden.";
}else{

$q = mysql_query("SELECT * FROM `literaturrecherche` WHERE `Titel` LIKE '%$_GET[s]%' OR `Autor` LIKE '%$_GET[s]%' OR `ln` LIKE '%$_GET[s]%' OR `Hrsg` LIKE '%$_GET[s]%' OR `Jahr` LIKE '%$_GET[s]%' OR `Untertitel` LIKE '%$_GET[s]%' OR `Ort_Verlag` LIKE '%$_GET[s]%' OR `Land` LIKE '%$_GET[s]%' OR `Abstract` LIKE '%$_GET[s]%' OR `Schlagworte` LIKE '%$_GET[s]%' OR `Art_Sprache` LIKE '%$_GET[s]%'") or die(mysql_error());
while($r = mysql_fetch_array($q)){
echo "<strong>".$r['Autor']." ".$r['Jahr']."</strong><br>".$r['Hrsg']."<br>".$r['Titel']."<br>".$r['Untertitel']."<br>".$r['ln']."<br>".$r['Ort_Verlag']."<br>".$r['Abstract']."<br>".$r['Schlagworte']."<br>".$r['Art_Sprache']."<br><hr>";
}}
}else{

[b]echo "<form method=get action=index.php><input type=text name=";echo"id=0,166,0,0,1,0&s;";echo"><input type=submit value=\"Search\"></form><br>";
}[/b]
?>

my problem is, that the searchform answers with a new link in the form of search.php?s=searchterm which is incompatible with the wcms pageform (e.g.) index.php?id=0,166,0,0,1,0. By handtyping the URL index.php?id=0,166,0,0,1,0&s=searchterm i get the correct results, thats why i tried to displace the "s" by "index.php?id=0,166,0,0,1,0" into the code (i separated it in my code with additionally echo commands to make it more visible for my own -> Bold) but in action it echos id%3D0%2C166%2C0%2C0%2C1%2C0%26s%C3%84=searchterm.

As i am not so familiar with php coding, i dont know how to enter the correct form. I know its possible to "translate" the sign (e.g.) "=" by "3D" in hex (which is proceeded by the form), but how to enter it into the code?

Mybe there is a very simple conclusion i didnt find yet.

Many thanx in advance!!
Last edited by le_fou on Fri 23. Mar 2007, 12:32, edited 2 times in total.
le_fou
Posts: 6
Joined: Wed 21. Mar 2007, 17:56

Post by le_fou »

even by adding a new function hex2str

Code: Select all

function hex2str($hex)
{
  for($i=0;$i<strlen($hex);$i+=2)
  {
    $str.=chr(hexdec(substr($hex,$i,2)));
  }
  return $str;
}
and later the command

Code: Select all

echo hex2str("3D");
e.g. for printing the "=" it returns a "%3D" as URL.
le_fou
Posts: 6
Joined: Wed 21. Mar 2007, 17:56

Post by le_fou »

also trying

Code: Select all

$komplett = "id%3D0%2C166%2C0%2C0%2C1%2C0%26s";
$komplett2 = "id=0,166,0,0,1,0&s";
echo urlencode($komplett);
//echo urldecode($komplett);
with both komplett and komplett2 didnt return a usable answer.
le_fou
Posts: 6
Joined: Wed 21. Mar 2007, 17:56

Post by le_fou »

to answer a solve by myself: i am now using

Code: Select all

<html>
<form method="get" action="index.php">
<input type="hidden" name="id" value="0,166,0,0,1,0">
<input type="text" name="s">
<input type="submit" value="Search">
</form>
</html>
and it works!
le_fou
Posts: 6
Joined: Wed 21. Mar 2007, 17:56

Post by le_fou »

So, for those who'd like to search a different database using this script, I will clearly post it here.

the search-script itself is called search.php

Code: Select all

<?php

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("bibliographie") or die(mysql_error());

if(isset($_GET['s'])){

echo "<form method=get action=index.php>
	  <input type=hidden name=id value=0,166,0,0,1,0>
	  <input type=hidden name=datenbank>
	  <input type=text name=s>
	  <input type=submit value=Search>
	  </form>";
echo "<br>Suche nach: \"".$_GET['s']."\"<br><hr>";

$q = mysql_query("SELECT * FROM `literaturrecherche` WHERE `Titel` LIKE '%$_GET[s]%' OR `Autor` LIKE '%$_GET[s]%' OR `ln` LIKE '%$_GET[s]%' OR `Hrsg` LIKE '%$_GET[s]%' OR `Jahr` LIKE '%$_GET[s]%' OR `Untertitel` LIKE '%$_GET[s]%' OR `Ort_Verlag` LIKE '%$_GET[s]%' OR `Land` LIKE '%$_GET[s]%' OR `Abstract` LIKE '%$_GET[s]%' OR `Schlagworte` LIKE '%$_GET[s]%' OR `Art_Sprache` LIKE '%$_GET[s]%'") or die(mysql_error());

if(!is_array(mysql_fetch_array($q))){

echo "<BR>Es wurde kein Datensatz gefunden.<br><hr>";
}else{

$q = mysql_query("SELECT * FROM `literaturrecherche` WHERE `Titel` LIKE '%$_GET[s]%' OR `Autor` LIKE '%$_GET[s]%' OR `ln` LIKE '%$_GET[s]%' OR `Hrsg` LIKE '%$_GET[s]%' OR `Jahr` LIKE '%$_GET[s]%' OR `Untertitel` LIKE '%$_GET[s]%' OR `Ort_Verlag` LIKE '%$_GET[s]%' OR `Land` LIKE '%$_GET[s]%' OR `Abstract` LIKE '%$_GET[s]%' OR `Schlagworte` LIKE '%$_GET[s]%' OR `Art_Sprache` LIKE '%$_GET[s]%'") or die(mysql_error());
while($r = mysql_fetch_array($q)){
echo "<strong>".$r['Autor']." ".$r['Jahr']."</strong><br>".$r['Hrsg']."<br>".$r['Titel']."<br>".$r['Untertitel']."<br>".$r['ln']."<br>".$r['Ort_Verlag']."<br>".$r['Abstract']."<br>".$r['Schlagworte']."<br>".$r['Art_Sprache']."<br><hr>";

}}
}else{
echo "<form method=get action=index.php>
	  <input type=hidden name=id value=0,166,0,0,1,0>
	  <input type=hidden name=datenbank>
	  <input type=text name=s>
	  <input type=submit value=Search>
	  </form>";
}
?>
which is included in one of my wcms-sites by using

Code: Select all

[PHP]
include('search.php'); 
[/PHP]
To make it work, you just need to displace my "0,166,0,0,1,0" by your ID. And, of course, change the database, table and the fields which you wanna search.
Post Reply