php/mysql search engine

Ai o întrebare legată de PHP? Incercăm să îi găsim soluţie. Sau poate doar vrei să publici un cod interesant.

Moderatori: Zamolxe, Moderatori


sorin21us
PHPRomania Supporter
Mesaje: 6
Membru din: Lun Apr 20, 2009 7:33 am

Mesajde sorin21us » Lun Apr 20, 2009 8:29 am

Am uitat sa pun scriptul :)


<?php
include_once("mysql_connect.php");
if(isset($_POST['submit'])){
$problem = FALSE;
if(empty($_POST['search'])){
$problem = TRUE;
echo "<p style=\"color:#FF0000;\">Please enter a search term!</p>";
}

if(!$problem){
$text = $_POST['search'];

$query = "SELECT * from news_posts WHERE title LIKE '%$text%' OR post LIKE '%$text%' OR author LIKE '%$text%'";
$result = @mysql_query($query);
$query2 = "SELECT id, title, author, post, DATE_FORMAT(date, '%M %d, %Y') as sd FROM news_posts ORDER BY id DESC limit 15";
$result2 = @mysql_query($query);

if($result && $result2){
echo "<h1>found " . mysql_num_rows($result) . " results</h1><br />";
echo "<ul>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$mess=$row['post'];
$view = 'page.php?id=' . $row['id'];
echo '<li>' . $row['id'] . ' - <a href="' . $view . '">' . $row['title'] . '</a> - ' . $row['author'] . '</li>';
}
echo "</ul><br />
<a href=\"search.php\">Search Again?</a>";
}
else
{
echo "No posts matched your query";
}
}
}
else
{
?>
<form id="search" action="search.php?search" method="post">
Keyword: <input type="text" name="search" size="30" class="text" /><br />
<input type="submit" name="submit" value="Search" />
</form>
<?php
}
?>

Avatar utilizator
LordDaemon
Junior Member
Mesaje: 42
Membru din: Joi Iun 05, 2008 9:49 am
Localitate: Alba Iulia
Contact:

Mesajde LordDaemon » Mar Apr 21, 2009 11:47 pm


sorin21us
PHPRomania Supporter
Mesaje: 6
Membru din: Lun Apr 20, 2009 7:33 am

Mesajde sorin21us » Mie Apr 22, 2009 10:52 am

Multumesc pentru ajutor. Dupa ce am vazut mesajul de la tine mi-a venit ideia sa folosesc o functie. Cu aceasta functie nu se taie din vreun cuvant si se adauga la sfarsit 3 puncte.

function snippet($text, $chars, $dots = false)
{
$end_char = substr($text, $chars, 1);
if(preg_match('#\S#', $end_char))
{
$chars += strpos($text, ' ', $chars) - $chars;
}
$ret = ($dots) ? substr($text, 0, $chars) . '...' : substr($text, 0, $chars);
return $ret;
}
echo snippet($mess, 200, true);


Înapoi la “Cod PHP”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 18 vizitatori