FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Build an online dictionary by PHP/MySQL





puccavn
create a database with name: dict
and run these query

Code:
CREATE TABLE `words` (
 `id` tinyint(4) NOT NULL auto_increment,
 `eng` varchar(255) NOT NULL default '',
 `vie` varchar(255) NOT NULL default '',
 PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;


Then create new file with name: dbconn.inc, copy codes below---> save

Code:
<?php     
    $link = mysql_connect ("localhost", "yourusernamehere", "yourpasshere")
    or die ("Could not connect to MySQL Database");
    mysql_select_db("dict", $link);
?>


Copy codes below to a text edition

Code:

<?php
echo("<a href=admin.php>ACP</a><br>");

if(isset($_GET['act']))
{
$do=$_GET['act'];
switch($do)
{
 case 'form': show_form();break;
 case 'sm' : submited();break;
 default : show_form; break;
}

}
else
{
show_form();
}

function show_form()
{
?>
<form name=frm method=POST action="index.php?act=sm">

Word : <input type=text name="word"> <input type=submit name="sbm" value="Submit">
</form>
<?

}
function submited()
{

require("dbconn.inc");
//if(isset($_POST['sbm']))
//{
 $word=$_POST['word'];
 
 $sql="select * from words where eng like '".$word."'";
 //echo($sql);
 $result=mysql_query($sql,$link);
 if(@mysql_num_rows($result)!=0)
 {
  while($rows=mysql_fetch_array($result))
  {
   $vie=$rows["vie"];
  }
  echo("Word<b>$word</b> in vietnamese is : <b>$vie</b><br>");
?>
<form name=frm method=POST action="index.php?act=sm">

Word : <input type=text name="word"> <input type="submit" name="sbm" value="Submit">
</form>
<?
 }
 else
  echo("Don't know that word !! My database is not update!");
//}
mysql_close($link);

}
?>


save with name: index.php

Copy this code to text editor:
Code:
<?php
echo("<a href=index.php>Home</a><br>");
if(isset($_GET['act']))
{
$do=$_GET['act'];
switch($do)
{
 case 'form': show_form(); break;
 case 'sm': submited(); break;
 default: show_form();
}

}
else
show_form();
function show_form()
{
?>
<form name=frm method=POST action=admin.php?act=sm>
English: <input type=text name="eng"><br>
Vietnam: <input type=text name="vie"><br>
<input type=submit name=sm value="Submit">
</form>
<?
}
function submited()
{
require("dbconn.inc");
if(isset($_POST['sm']))
{
 $eng=$_POST['eng'];
 $vie=$_POST['vie'];
 $sql="Insert into words(eng, vie) values('".$eng."','".$vie."')";
 $res=mysql_query($sql,$link);
 //header("Location: admin.php");
?>
<script language=Javascript>window.location="admin.php";</script>
<?

}
else
 echo("Insert data !!");
mysql_close($link);
}
?>



Save with name admin.php

upload these three files to your host
admin.php to insert more words into your db

base on these code you can make more dict, exp: fr-vie
comp...
and create more field forexp: adjective, noun, verb...

sorry for my bad english ( i'm from Vietnam )
Very Happy Very Happy
thaonguyenxanh
Thank you very much
the25thelement
Thank you very much for the code.
I've managed to insert this code into my template. I have tried to do the same for the results too but I couldn't do it at all.

Can any one please help me how to show the results in the same template I am using after the user click submit to the word.

Thank you in advance.

Regard's
theaavi
Thank you very much for share this code.

can you share three field code, I mean if i am post field A data then fetch with field B or C Data or if I am entered in Field B Something then fetch field A or C I hope you Get it my point. if you do please share with us. and sry for my Bad english.

Thanks
Sykox
@puccavn, Thank you so much!
I was searching for something like this for ages.

I am building a online bilingual dictionary and have run into lots of trouble...

Unfortunatly your code doesnt work! It shows errors and since i am new to php I have no idea how to modify it. In case you'd like to know here are the errors for:

index.php: Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\DICT\index.php on line 65

admin.php: Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\DICT\admin.php on line 45

So I am pretty much stuck up!

Here is my code
Code:
<?php
   if(isset($_POST['submit'])){
   if(isset($_GET['go'])){
   if(preg_match("/[A-Z  | a-z]+/", $_POST['name'])){
   $name=$_POST['name'];
   //connect  to the database
   $db=mysql_connect("localhost", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error()); 
   //-select  the database to use
   $mydb=mysql_select_db("kachhi");
   //-query  the database table
   $sql="SELECT  ID, eWord, kWord FROM english WHERE eWord LIKE '%" . $name .  "%' OR kWord LIKE '%" . $name ."%'";
   //-run  the query against the mysql query function
   $result=mysql_query($sql, $db) or die ('Unable to run query:'.mysql_error());
   //$result=mysql_query($sql);
//-create  while loop and loop through result set
     while($row=mysql_fetch_array($result)){
          $eWord  =$row['eWord'];
          $kWord=$row['kWord'];
          $ID=$row['ID'];
     //-display the result of the array
  //echo "<ul>\n";
  //echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$eWord . " " . $kWord .  "</a></li>\n";
   //echo     "English Word : " . $eWord .  "\n" . "kachhi Word : " . "<div style ='font:19px/21px kachhiregular;color:#ff0000'>" . $kWord ."</div>"; 
 // echo "</ul>";
 

echo "<center>";

//table
echo "<table>";
echo "<tr>";
echo "<td>$eWord</td>";
echo "<td><div style ='font:19px/21px kachhiregular;'>$kWord</div></td>";
echo "</tr>";
// Close the table
echo "</table>";
//table

echo "<center>";
  }
     }
  else{
     echo  "<p>Please enter a search query</p>";
     }
     }
  }
   ?>
Related topics
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.