Forum Moderators: coopster
here's the script:
<?php
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT shops.name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query)) {
echo "Sorry, no matching results.";
exit();
}
while ($record = mysql_fetch_assoc($query)) {
while (list($fieldname, $fieldvalue) = each ($record)) {
echo $fieldname.": <b>".$fieldvalue."</b><br>";
}
echo "<br>";
}
?>
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT shops.name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results.";
exit();
}
else
{
while ($record = mysql_fetch_assoc($query)) {
echo "Name";
}
}
echo "Sorry, no matching results.";
exit();
}
else
{
while ($row = mysql_fetch_array($query)) {
echo $row['name'];
}
}
It would look something like that, now just take variable I have inserted and alter it to fit the rest of your column names.
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results.";
exit();
}
else
{
while ($row = mysql_fetch_array($query)) {
echo $row['name'];
}
}
?>
all i want to do is, when i search for a name i have in my database, then i get it out on my website.. however, if i do not have that name in the database i want it to echo out Sorry, no search results...
and when the name is echoed out, it should be something like: Name <bold>the name i searched for</bold>
if possible, could you like maybie make the <bold>name i searched for</bold> to a link?
that's all...
now, when i search for test i just get up Test on the screen.. but when i search for blabla i get Test up again. I don't get Sorry, no search results....
that's all i want
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name regexp '$search'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results for the term $search";
exit();
}
else
{
while ($row = mysql_fetch_array($query)) {
echo 'Name: <b><a href="">'.$row['name'].'</a></b>';
}
}
?>
Use that code, change the search variable on your form to $search, this way there is no confusion, input where you would like the variables to link to and you should be ok.
in the form where i type in the search?
here's the form..
<form action="sok.php" method="post">
<center><div class="style1" style="padding:10 ">Søk etter Film tittel
<input type="text" style="margin:6px;width: 100px;" name="name" class="input">
<br>
<input type="submit" value="Start søk" name="Søk" class="knapp" />
</center><br>
</form>
<?php
$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name regexp '$search'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results for the term $search";
exit();
}
else
{
while ($row = mysql_fetch_array($query)) {
echo 'Name: <b><a href="">'.$row['name'].'</a></b><br>';
}
}
?>
<?php// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Skriv inn en film tittel, eller en del av navnet...</p>";
exit;
}
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","user","pass"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("vinyldealers") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select name from shops where shops.name like \"%$trimmed%\"
order by shops.name"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, offer a google search as an alternative
if ($numrows == 0)
{
echo "";
echo "";
// google
echo "";
}
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>Ditt søk for: <b> $var </b> </p>";
// begin to show results set
// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["name"];
echo "Navn: <b><a href=$row[name]>$row[name]</a></b>";
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "";
}
// calculate number of pages needing links
$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// not last page so give NEXT link
$news=$s+$limit;
echo "";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Resultater, $a av $numrows</p>";
?>
<br><br><br><p>Nytt søk</p>
<form name="form" action="test2.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
[edited by: jatar_k at 1:41 am (utc) on Sep. 22, 2005]
[edit reason] generalized user/pass [/edit]
CREATE TABLE shops (
id int not null auto_increment,
name varchar(25) not null,
url varchar(25) not null
#...
PRIMARY KEY(id));
and when you do the search:
$query = "SELECT name, url FROM shops WHERE shops.name LIKE '%$trimmed%'
ORDER BY shops.name";
then you can make the link as
echo "Navn: <b><a href=".$row['url'].">".$row['name']."</a></b>";
Best regards
Michal Cibor
CREATE TABLE shops (
id int not null auto_increment,
name varchar(25) not null,
url varchar(25) not null,
#...
PRIMARY KEY(id));
Or if the table is already there, you can add a new column:
ALTER TABLE shops ADD url varchar(25) not null AFTER name;
Best regards
Michal Cibor
name ; url
test ; test.php
boondock saints ; boondock_saints.php
Do you see now?
It's more flexible solution, as you may wish to someday change the links to another dir, or whatever.
To automatize the process
$name = "test";
$url = str_replace(" ", "_", $name).".php";//url = test.php
$name = "boondock saints";
$url = str_replace(" ", "_", $name).".php";//url = boondock_saints.php
Best regards
Michal Cibor