Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 11-01-2004, 02:08 AM   #1 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
totally newbie need help with searching from Mysql database

i have database ICT with table news and consist off id,title, abstraksi(news synopsys),content, datetime and photo and publish (news will be published or not). I want to divide the search result 10 result per page but it doesnt work. whats wrong with this program.. How to make search option which selected by user search by title. I have datetime format how to print only date or only news time from database. If i change the field datetime with field date and field time. How to sort lastest news .
<html>
<head><title></title></head>
<body>

<form action = search_news.php method = post>
</table>
<tr> <td >Search</td>
<td><input type = text name = keyword size = 20 ></td>
<td>

<select name=id class=textfield>";
<?php
echo "<option value = ".$arr_topic[$i][id];
if ($id == $arr_topic[$i][id]) {
echo " selected";
}
echo ">".$arr_topic[$i][kategori]."</option>";
}
echo "</select>


?>
</td>
<td><input type = submit name = submit value = Go ><td>
</tr>
</table>
</form>

<?php
$user = "root";
$host = "localhost";
$pass = "";
$connection = mysql_connect($host,$user,$pass);
$query = "select * from news where title like '%keyword%'
or abstraksi like '%keyword%'and publish == 'Y'";
$nb_result = mysql_query($query);
$nb_row_found = mysql_num_rows($nb_result);
$rowperpage = 10; //nb news per page
if (empty($offset)){
$offset = 0;
}
if ($nb_row_found==0){
echo "<b>$keyword</b> doesn't matched any news";
exit;
}
else{
echo "<b>$keyword</b><br> found ".$nb_row_found."";
}
//get result
$result = mysql_query("select * from news where title like '%$skeyword%'
or abstraksi like '%$keyword%'order by id limit $offset,$rowperpage");
echo "<table align = center><h2>News found : </h2>";
echo "<table border = 0 >";
while($row_result = mysql_fetch_arrray($result))
{ echo "<tr>";
echo "<td>";
echo "<a href =\"$self?id=$row_result[id]\ >".$row_result[title]."</a>";
echo $row_result[abstraksi];//abstraksi
echo "<td>";
echo "<tr><td align = right>";
echo "</td></tr>";
}
echo "</table>";
echo "<div align = center>";
if ($offset != 0){
$prevoffset = $offset-$rowperpage;
echo "<a href = $PHP_SELF?offset= $prevoffset>Prev</a><nobr>";
}
//count pages
$max_page = ceil($nb_row_found/$rowperpage);
for ($i = 1;$i <= $maxpage;$i++){
$newoffset = $rowperpage * ($i -1);
if ($offset != $newoffset){//print link to page
echo "<a href = $PHP_SELF?offset = $newoffset>$i</a></nobr>";
}
else{//print without link
echo " ".$i." ";
}
if ((!($offset/$rowperpage) + 1 == $max_page)&& $maxpage != 1 ){
$newoffset = $offset + $rowperpage;
echo "<a href = $PHP_SELF?offset = $newoffset>Next</a><nobr>";
echo "</div>";
}

}

?>

</body>
</html>
if13121 is offline   Reply With Quote
Old 11-01-2004, 04:00 AM   #2 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Quote:
How to sort lastest news .
Try adding an ORDER BY statement and sort by either the datetime field or maybe id.

eg:

PHP Code:
$query = <<<EOQ
SELECT *
FROM news
WHERE ((publish = 'Y') AND 
(title like '%keyword%' OR abstraksi like '%keyword%'))

ORDER BY datetime DESC
EOQ; 

You can also limit the number of results by using the LIMIT statement in your query.

-r
idx is offline   Reply With Quote
Old 11-01-2004, 12:57 PM   #3 (permalink)
if13121
Registered User
 
if13121's Avatar
 
Join Date: Oct 2004
Location: Bandung,Indonesia
Posts: 16
if13121 is on a distinguished road
sory there is some mistake inmy code. Here is the code:
PHP Code:
<html>
<head><title></title></head>
<body>

<form action = search_news.php method = post>
//this link doesn't work. I want, i can search from the search page. Pls help.. :confused: 
</table>
<tr> <td >Search</td>
<td><input type = text name = keyword size = 20 ></td>
<td>
</td>
<td><input type = submit name = submit value = Go ><td>
</tr>
</table>
</form>

<?php
$user 
"root";
$host "localhost";
$pass ""
$connection mysql_connect($host,$user,$pass);
$query "select * from news where title like '%$keyword%'
or abstraksi like '%$keyword%'and publish == 'Y'"
;
$nb_result mysql_query($query);
$nb_row_found mysql_num_rows($nb_result);
$rowperpage 10//nb news per page 
if (empty($offset)){
$offset 0;

if (
$nb_row_found==0){
echo 
"<b>$keyword</b> doesn't matched any news";
exit;
}
else{
echo 
"<b>$keyword</b><br> found ".$nb_row_found."";
}
//get result
$result mysql_query("select * from news where title like '%$skeyword%'
or abstraksi like '%$keyword%'order by id limit $offset,$rowperpage"
);
echo 
"<table align = center><h2>News found : </h2>";
echo 
"<table border = 0 >";
while(
$row_result mysql_fetch_array($result))
{ echo 
"<tr>";
echo 
"<td>";
echo 
"<a href = view($row_result[id]) >".$row_result[title]."</a>";
/* i want to make a function to view news content which id = $row_result[id] can i call it like this ???  :confused: */ 
echo $row_result[abstraksi];//abstraksi
echo "<td>";
echo 
"<tr><td align = right>";
echo 
"</td></tr>";
}
echo 
"</table>";
echo 
"<div align = center>";
if (
$offset != 0){
$prevoffset $offset-$rowperpage;
echo 
"<a href = $PHP_SELF?offset= $prevoffset>Prev</a><nobr>";
}
//count pages
/*i have get the result of the search but if the result more than 1 page i still don't get link to next page result whats wrong here?? pls help :confused: */
$max_page ceil($nb_row_found/$rowperpage);
for (
$i 1;$i <= $maxpage;$i++){
$newoffset $rowperpage * ($i -1);
if (
$offset != $newoffset){//print link to page
echo "<a href = $PHP_SELF?offset = $newoffset>$i</a></nobr>";
}
else{
//print without link
echo " ".$i." ";
}
if ((!(
$offset/$rowperpage) + == $max_page)&& $maxpage != ){
$newoffset $offset $rowperpage;
echo 
"<a href = $PHP_SELF?offset = $newoffset>Next</a><nobr>";
echo 
"</div>";
}

}

?>

</body>
</html>
// how to make an option search news by title or by content?  :( 
//thank you
in my database i have a field with datetime type how to print just the time or just the date from that type :confused:
if13121 is offline   Reply With Quote
Old 11-02-2004, 04:34 AM   #4 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
You may need to surround the action/method in quotes.

Code:
<form action="search_news.php" method="POST">
Quote:
in my database i have a field with datetime type how to print just the time or just the date from that type
I can't remember how the datetime type is formatted, but you're either going to have to use internal mysql functions (if there are any) or use PHP string functions to extract the portion of the datetime then display as needed.

http://dev.mysql.com/doc/mysql/en/DATETIME.html

So if the format is YYYYMMDDHHMMSS:
PHP Code:
$dt substr($row_result["datetime_fieldname"], 08);
// now $dt is "YYYYMMDD"
$tm substr($row_result["datetime_fieldname"], 8);
// $tm is HHMMSS 
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't see MySql database records in Mozilla monicao PHP 3 05-02-2004 05:19 PM
open another mysql database using phpMyAdmin infinite_root PHP 5 04-28-2004 08:04 PM
Methods of moving a database... DarkTwilkitri PHP 8 11-19-2003 05:02 AM
permissions in MySQL trevor PHP 3 01-10-2003 01:47 PM


All times are GMT -8. The time now is 04:08 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting