Forum Moderators: coopster
I want you to help me those information to put those information in database.
The database should contain four tables
1)files containing id_file,name,url -->3 columns
2)file_word containing id_file,id_word-->2 columns
3)words containing id_word,word --->2 columns
4)meta_info containing id_file,type,content-->3 columns
I am attaching my code for further queries.
<?PHP
function recursive_directory($dir)
{
$arr = array();
if ($hld = opendir($dir))
{
while (false!== ($file = readdir($hld)))
{
if ($file!= "." && $file!= "..")
{
if (is_dir($dir. "/" . $file))
{
$arr = array_merge($arr, recursive_directory($dir. "/" . $file));
}
else
{
if((ereg(".html" , $file)) ¦¦ (ereg(".htm",$file)))
{
$path = $dir . "/" . $file;
$arr[] = realpath($path);
}
}
}
}
closedir($hld);
}
return $arr;
}
function superExplode($string,$st)
{
$i=0;
$arr[$i++]= strtok($string,$st);
while($tok = strtok($st))
$arr[$i++] = $tok;
return $arr;
}
function count_word($url)
{
if($file=file($url))
{
$tags = get_meta_tags($url);
echo "<h1>Meta Information</h1> ";
if(count($tags)!=0)
{
echo "Description: ".$tags['description']."<br>";
echo "Author: ".$tags['author']."<br>";
echo "Keywords: ".$tags['keywords']."<br>";
echo "Generator: ".$tags['generator']."<br>";
}
else
{
echo "No Meta Data<br>";
}
$fetch = implode('',$file);
$fetch = strtolower(strip_tags($fetch));
$arr_tok = superExplode($fetch,"\" ' : , ( ) { } [ ] ; #? .! \n -");
sort($arr_tok);
$temp = (array_count_values($arr_tok));
echo"<br>";
echo "<table border='0'><tr><th>Word</th><th>Count</th></tr>";
foreach($temp as $s => $r)
{
if($s!="")
echo "<tr><td>$s</td><td>$r</td></tr>";
}
echo "</table>";
}
else
{
echo "Try Again";
}
}
if(isset($_GET['path']))
{
$path=$_GET['path'];
$array = recursive_directory($path);
for($i=0;$i<count($array);$i++)
{
echo "</br>";
echo"Path :";
echo $array[$i]."<br>";
count_word($array[$i]);
}
}
echo "<html>";
echo "<body>";
echo "<form action=\"test.php\" method=\"get\">";
echo "</br>";
echo "Path of: <input type=\"text\" name=\"path\"/>";
echo "<input type=\"submit\" value=\"Enter\"/>";
echo " </form>";
echo "</body>";
echo "</html>";
?>
Well i have design 4 tables. Well actually i have just displayed the file name ..dont know what to do know...
files table should contain 3 fileds
1)id_file
2)name
3)url
file_word table should contain 2 fileds
1)file_word
2)id_word
words table should contain 2 fields
1)id_word
2)word
meta_info should contain 3 fields
1)id_file
2)type
3)content
thanks for your help
// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC));
Thats from the PHP manual. What does your version of the same piece of code look like?
Are the tables already created, or do you just have a blank file with the correct name?
The MySQL SELECT [dev.mysql.com] statement should help you. There is a list on the right hand side of that page that gives a lot of the other commands you will need to use to populate the table with data.
In brief summary (in case I have it wrong) -
You want to populate the table you have with data. mysql_query [php.net] and the INSERT statement will do that.
You may need to create the tables. Another mysql_query will do that.
You may need to retrieve data from the tables. mysql_query along with a few other functions will do that.
So it seems that you need assistance with the SQL statements i.e. you want the script written for you.
There is something about homework posts in the charter [webmasterworld.com] so I dont mind helping, but im not writing the complete script for you.
So if you can post what you have then im sure that people will be willing to help you with the errors that there may be.
sample insert into table "users", which has 3 fields - id, name, password:
<?
$query = "insert into users (id, name, password) values ($id, '$name', '$password')";
$result = mysql_query($query);
//success
if(mysql_affected_rows()){
echo "inserted!";
}
//failure
else{
echo "failed to insert";
}
?>
i)files (id_file, name, url)
-you can have an insert query for this in the last else if statement in the function recursive_directory():
if((ereg(".html" , $file)) ¦¦ (ereg(".htm",$file))) {
/* insert statement for table files here */
-or u can have the insert query in the last if statement (if isset($_GET['path'])...
ii) file_word (id_file, id_word), and words (id_word, word)
-in your foreach statement in the function count_word() is where this insert query goes:
foreach($temp as $s => $r) {
if($s!=""){
/* insert statement for table file_word and words here */
iii)meta_info (id_file, type, content)
- this would also go in the count_word function inside the first nested if statement : if(count($tags)!=0) {
/* insert statement for table meta_info here */
i dont know what to do know? can you please be more precise in your insert query....i mean can you write the whole 4 query for inserting in 4 tables....
do i have to write all this also?
$username = "";
$password = ";
$hostname = "";
#connect to database
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
#Select database
$db = mysql_select_db('indexer', $dbh) or die(mysql_error());
do i have to write all this for all 4 tables? please help me..
you only have to connect once (before executing queries) in your script. to insert to eachof your table you need a query for each.
Remember to surround your strings with single quote or escape with double quotes when doing db queries. integers do not need quotes.
1)files containing id_file,name,url
$sql = "insert into files (id_file, name, url) values ($id, '$name', '$url')";
mysql_query($sql);
//this will insert into your files table.
2)file_word containing id_file,id_word
$sql = "insert into file_word (id_file, id_word) values($id_file, $id_word)";
mysql_query($sql);
//this will insert into file_word
3)words containing id_word,word
$sql = "insert into words (id_word, word) values ($id_word, '$word')";
mysql_query($sql);
4)meta_info containing id_file,type,content
$sql = "insert into meta_info (id_file, type, content) values ($id_file, \"$type\", \"$content\")";
mysql_query($sql);