GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2004, 05:06
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
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>
  #2  
Old 01-Nov-2004, 15:50
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
sorry I should deleted some of thesource code. this is the source 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..
</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 ??? */
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*/
$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>
// how to make an option search news by title or by content?
//thank you
  #3  
Old 01-Nov-2004, 16:00
if13121 if13121 is offline
New Member
 
Join Date: Oct 2004
Posts: 27
if13121 is on a distinguished road
there is still mistakes keyword should be $keyword.
Please help
  #4  
Old 05-Dec-2004, 10:22
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Cool

You had a few other typos in your code. Your general idea of how you needed to go about this project was good. You should have made use of $_POST and $_GET variables to get your keyword and offset from the URL, so that these values are carried on from view to view as you click on the links provided by your search page. Also, you need to be careful to use quotes in your HTML code where they should be used - I had to make ALOT of fixes in this area, in order to get your code to work. You must not put spaces in your URLs - especially when trying to set variables like 'offset' for your PHP code to retrieve using $_GET.

One last item, be careful when writing MySQL queries that you don't use the double equal sign - it doesn't work. Just use a single equal sign when checking a condition.

Anyway, there were so many things to fix in the code, here's a posting of your code, corrected. I put comments with __JM__ where I made changes:
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..
</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 = ""; 
$database = "testdb"; // __JM__ added to select the database with the news table


if (isset($_GET['keyword'])) {
     $keyword=$_GET['keyword'];
} else {
    $keyword=$_POST['keyword']; // __JM__ added
}

$offset=$_GET['offset']; // __JM__ added
$connection = mysql_connect($host,$user,$pass);
mysql_select_db($database); // __JM__ added in case you have more than one database
$query = "select * from news where title like '%$keyword%'or abstraksi like '%$keyword%'and publish ='Y'"; //__JM__ query had two '=' signs for publish equals
$nb_result = mysql_query($query);
$nb_row_found = mysql_num_rows($nb_result);
$rowperpage = 1; //nb news per page __JM__ changed to 1 
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 '%$keyword%'or abstraksi like '%$keyword%'order by id limit $offset,$rowperpage"); // __JM__ typo was here
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 ??? */ 
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&keyword=$keyword>Prev</a>&nbsp;<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*/
$max_page = ceil($nb_row_found/$rowperpage);

for ($i = 1;$i <= $max_page;$i++){ // typo here.... $maxpage should be $max_page

$newoffset = $rowperpage * ($i -1);

if ($offset != $newoffset){//print link to page
echo "<a href ='$PHP_SELF?offset=$newoffset&keyword=$keyword'>$i</a>&nbsp;&nbsp;&nbsp;</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&keyword=$keyword'>Next</a><nobr>";
echo "</div>";
}

}

?>

</body>
</html> 

  #5  
Old 05-Dec-2004, 10:26
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
By the way, in the above code, where you see [ENT_0], replace those with ampersands (just do a search & replace). I don't know why this is happening here - probably just a small bug in the forum code that can be fixed up.
  #6  
Old 05-Dec-2004, 16:10
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Aaargh!

Noted...
  #7  
Old 05-Jan-2005, 01:34
JUNK KED JUNK KED is offline
Awaiting Email Confirmation
 
Join Date: Oct 2003
Location: uk&ireland
Posts: 85
JUNK KED will become famous soon enough

Did you get your script to work?


I spotted other typos in your html

All html variables should be somename="quoted" or else(depending on the users browser )the form will not submit them to the script in the first place.

your script won't give the right output if it doesn't get the right input.

Drop us a line and let us know if you got anywhere.
Last edited by JUNK KED : 05-Jan-2005 at 02:52. Reason: correction
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
cgi txt database to mysql erhanharputlu MySQL / PHP Forum 0 27-Sep-2004 02:30
Macromedia DWMX and PHP MySQL setup soulja90 MySQL / PHP Forum 1 17-Mar-2004 05:50
Grouping data from MySQL with PHP - Newbie question. giobbi MySQL / PHP Forum 12 27-Feb-2004 01:34
Windows: From only £20p/y,Linux: from $10p/m. ASP, ASP.NET, PHP, Free MySQL, +More EyotaHosts Web Hosting Advertisements & Offers 0 28-Jun-2003 14:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.