blivori Posted July 19, 2011 Share Posted July 19, 2011 Hi, Basically I have a php script that lists all files in a directory and outputs them as hyperlinks and then saves the file names to a MySQL Database. However, only the file names are stored in the DB. Another script, search.php, gets the text input from list.php and outputs all the files in the database witouth being hyperlinked - just plain text. Is it possible to save the filepath in the MySQL and output the file names hyperlinked? This is the code: List.php <?php $dbhost = 'localhost'; $dbuser = 'admin'; $dbpass = 'root'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'files'; mysql_select_db($dbname); mysql_query("TRUNCATE files_tbl"); if ($handle = opendir('./pdf/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="'.$file.'">'.$file.'<br></a>'; $query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');"; mysql_query($query) or die('Error, Insert query failed' . mysql_error()); } } closedir($handle); } ?> <html> <head> </head> <body> <form action="search.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <P>List of files:</p> <P><?=$thelist?></p> Search.php <?php mysql_connect ("localhost", "admin","root") or die (mysql_error()); mysql_select_db ("files"); $term = $_POST['term']; $sql = mysql_query("select * from files_tbl where Name like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo <h1><br/> Name: '.$row['Name']; } ?> I would really appreciate your help. Thanks & Regards, blivori Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/ Share on other sites More sharing options...
Ptsface12 Posted July 19, 2011 Share Posted July 19, 2011 Hello, Try this: <input type="hidden" id="urlcontainer" name="urlcontainer" value="<?php dirname("/etc/passwd") . PHP_EOL; ?>"/> <?php $input = mysql_query("INSERT INTO files_tbl WHERE url_field"); ?> Best Wishes, Ptsface12 Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244471 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Hi Ptsface12, Thanks for your quick reply. Where will I have to put that? It looks like a text box input. What I want to do is to get the full link from the hyperlinked file on the list.php and save it to the fiels_tbl. Thanks again, blivori Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244473 Share on other sites More sharing options...
Ptsface12 Posted July 19, 2011 Share Posted July 19, 2011 Hello, What I suggest you doing is setting up your mysql table. Echoing out your links Import this in your MySQL CREATE TABLE `hreflinks` ( `id` bigint(20) NOT NULL, `link` varchar(255) NOT NULL, `showtext` varchar(255) NOT NULL, ) ENGINE=MyISAM DEFAULT CHARSET=utf8; <?php $username="username"; // edit your username $password="password"; // edit your password $database="database"; // edit your database mysql_connect(localhost,$username, $password); @mysql_select_db($database) or die ("Unable to select Database"); $sql = mysql_query("SELECT id, link, showtext FROM hreflinks") or die(mysql_error()); $row = mysql_fetch_assoc($sql) ?> Put this where you need to echo your details: <?php echo <a href=$row['link'><?php echo $row['showtext']; ?></a>; ?> If you have any problems while running this script, just reply again. If you have IM and need to contact me this way add [email protected] Best Wishes, Ptsface12 Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244474 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Ok got it. Just one last question.. I need to set up a new table or I can use the files_tbl and add a column? Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244477 Share on other sites More sharing options...
Ptsface12 Posted July 19, 2011 Share Posted July 19, 2011 Use the sql I included Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244478 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Hello, Try this: <input type="hidden" id="urlcontainer" name="urlcontainer" value="<?php dirname("/etc/passwd") . PHP_EOL; ?>"/> <?php $input = mysql_query("INSERT INTO files_tbl WHERE url_field"); ?> Best Wishes, Ptsface12 And as for this code? Where do I put it? I'm stuck on where I have to put the code you have given me. Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244479 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Ok. So the code has to be like this? List.php <?php $dbhost = 'localhost'; $dbuser = 'admin'; $dbpass = 'root'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $dbname = 'files'; mysql_select_db($dbname); mysql_query("TRUNCATE files_tbl"); if ($handle = opendir('./pdf/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $thelist .= '<a href="'.$file.'">'.$file.'<br></a>'; $query = "INSERT INTO files.files_tbl(Name) VALUES ('$file');"; mysql_query($query) or die('Error, Insert query failed' . mysql_error()); } } closedir($handle); } ?> <?php $username="admin"; // edit your username $password="root"; // edit your password $database="files"; // edit your database mysql_connect(localhost,$username, $password); @mysql_select_db($database) or die ("Unable to select Database"); $sql = mysql_query("SELECT id, link, showtext FROM hreflinks") or die(mysql_error()); $row = mysql_fetch_assoc($sql) ?> <html> <head> </head> <body> <form action="search.php" method="post"> Search: <input type="text" name="term" /><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <P>List of files:</p> <P><?=$thelist?></p> Search.php <?php mysql_connect ("localhost", "admin","root") or die (mysql_error()); mysql_select_db ("files"); $term = $_POST['term']; $sql = mysql_query("select * from files_tbl where Name like '%$term%'"); $sql2 = mysql_query("SELECT FieldID, Link, ShowText FROM files_tbl") or die(mysql_error()); while ($row = mysql_fetch_array($sql)){ echo <h1><br/> Name: '.$row['Name']; echo <a href=$row['link'><?php echo $row['showtext']; ?></a>; } ?> Sorry for my ignorance but I've only done some basic php. Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244481 Share on other sites More sharing options...
Ptsface12 Posted July 19, 2011 Share Posted July 19, 2011 Indeed. And sorry I made an error on the a href code. It should be echo <a href=$row['link'] not <a href=$row['link' Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244482 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 It's still not working :/. Just to give you a basic idea: This is the List.php with the hrefed links: And when you enter a search term it is supposed to show up another page with files containing contents from the search term. But as you can see they are not hrefed. Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244487 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 There are also a MySQL Database called 'files' which contains 2 tables 'files_tbl' and 'hreflinks' files_tbl contains: FileID Name hreflinks contains: id link showtext Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244488 Share on other sites More sharing options...
TeNDoLLA Posted July 19, 2011 Share Posted July 19, 2011 You mean you want to show the filenames as links from DB based on a user made search ? You have that query already in the earlier codes you posted? If you want to join the links data to the file data you need to have some id field on some of the tables for the reference. Anyways if the links are related to the files, why not just make one table like Files ------------ file_id filename file_link showtext Or did I misunderstood something? Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244505 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Thats what I asked Pstface12 before. I could make one database and add a link but the problem is that I don't know how to exactly add the link to the related field from the PHP script. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244522 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 I tried to do a circumvention by modifying search.php to be like this: <?php mysql_connect ("localhost", "admin","root") or die (mysql_error()); mysql_select_db ("files"); $term = $_POST['term']; $path = "127.0.0.1/pdf/" $sql = mysql_query("select * from files_tbl where Name like '%$term%'"); while ($row = mysql_fetch_array($sql)){ $full_path = $path.$row; echo <h1><br/> Name: '<a href = " > .$full_path['Name']'; } ?> However, it's not even inputting the path or the hyperlinked text. Any help please? Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244530 Share on other sites More sharing options...
blivori Posted July 19, 2011 Author Share Posted July 19, 2011 Tried this now but still no luck: <?php mysql_connect ("localhost", "admin","root") or die (mysql_error()); mysql_select_db ("files"); $term = $_POST['term']; $sql = mysql_query("select * from files_tbl where Name like '%$term%'"); while ($row = mysql_fetch_array($sql)){ $path = "/localhost/pdf/"; $fullpath = $path.$row; echo '<h1><br/> Name: '.$row['Name']; echo '<h3><br/> Path: <a href = '.$fullpath'> '.$fullpath' </a> '; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242305-retrieving-a-file-path-from-another-pagelinking-it-to-db/#findComment-1244544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.