mike2 Posted April 30, 2014 Share Posted April 30, 2014 To whom it may concern: I have this code that was written and basically what I am trying to do is get my search results (which are PDF files by the way) to be displayed as links to those particular PDF files as the pertain to the PDF files themselves. Here is the code: <html> <head></head> <body> <?php if (!isset($_POST['q'])) { ?> <h2>Search</h2> <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <input type="text" name="q" size="30" /> </form> <?php } else { ?> <h2>Search Results</h2> <?php try { // create object //$swish = new Swish('/usr/local/apache/htdocs/swish/index.swish-e'); $swish = new Swish('/var/www/html/pdf2/index.swish-e'); // get and run query from command-line $queryStr = htmlentities($_POST['q']); $result = $swish->query($queryStr); ?> Found <?php echo $result->hits; ?> match(es) for '<?php echo $queryStr; ?>'. <?php // iterate over result set // print details for each match while($r = $result->nextResult()) { ?> <p> <?php echo $r->swishreccount; ?>. <strong> <a href="<?php echo $r->swishdocpath; ?>"> <?php echo $r->swishtitle; ?> </a> </strong> (score: <?php echo $r->swishrank; ?>) <br/> <?php echo $r->swishdocpath; ?> </p> <?php } } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } } ?> </body></html> What this code does is it goes to the pdfsearch.php web page and it displays a Search box with a search field. The user then enters a word to search for words that are stored within those PDF files and the PDFs that have said word or words are diplayed: Here is an example: Let's say I am searching for the word test. Search ResultsFound 13 match(es) for 'test' . 1. (score: 1000)./ITA000030409.2013.3.00.L.09.30.PDF 2. (score: 737)./POL000040092.2013.A.00.E.12.31.PDF 3. (score: 737)./ISR000030655.2011.A.00.L.12.31.PDF 4. (score: 680)./SWE000040017.2013.1.00.E.03.31.PDF 5. (score: 680)./ZAF000030086.2013.A.00.E.08.31.PDF 6. (score: 608)./FRA000040051.2011.AMF.00.L.12.31.PDF 7. (score: 608)./ZAF000030068.2013.A.00.E.06.30.PDF 8. (score: 524)./IRL000030033.2013.A.00.E.03.31.PDF 9. (score: 524)./ZAF000030504.2012.U.00.E.06.30.PDF 10. (score: 524)./MYS000091186.2013.A.00.E.09.30.PDF 11. (score: 414)./ZWE000030003.2013.A.00.E.02.28.PDF 12. (score: 414)./CAN000031319.2013.3.00.E.09.30.PDF 13. (score: 262)./NGA000030110.2008.A.00.E.12.31.PDF My question is what would I have to do to my PHP code so that the search results of the PDF files themselves are able to be linked to each of their prospective PDF files individually so that when a user clicks on an individual PDF link, that individual PDF appears? The directory where the PDF files are stored is located under the /var/www/html/pdf2 directory. If anyone could assist me in this project I would greatly appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/ Share on other sites More sharing options...
fastsol Posted April 30, 2014 Share Posted April 30, 2014 It seems that this line of code should already be making a link out of something <a href="<?php echo $r->swishdocpath; ?>"> <?php echo $r->swishtitle; ?> </a> Can you post the exact html output to the browser that the page is currently doing? So a copy of hte view source from the browser. Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477749 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 It seems that this line of code should already be making a link out of something <a href="<?php echo $r->swishdocpath; ?>"> <?php echo $r->swishtitle; ?> </a> Can you post the exact html output to the browser that the page is currently doing? So a copy of hte view source from the browser. Okay. Do you mean actually show you the link to what I am doing? Here is the link: http://64.244.240.221:8080/pdfsearch.php if you type in test you will get results. also here is the page source when i right clicked and selected page source. <html> <head></head> <body> <h2>Search Results</h2> Found 13 match(es) for 'test'. <p> 1. <strong> <a href="1./ITA000030409.2013.3.00.L.09.30.PDF"> </a> </strong> (score: 1000) <br/> ./ITA000030409.2013.3.00.L.09.30.PDF </p> <p> 2. <strong> <a href="1./POL000040092.2013.A.00.E.12.31.PDF"> </a> </strong> (score: 737) <br/> ./POL000040092.2013.A.00.E.12.31.PDF </p> <p> 3. <strong> <a href="1./ISR000030655.2011.A.00.L.12.31.PDF"> </a> </strong> (score: 737) <br/> ./ISR000030655.2011.A.00.L.12.31.PDF </p> <p> 4. <strong> <a href="1./SWE000040017.2013.1.00.E.03.31.PDF"> </a> </strong> (score: 680) <br/> ./SWE000040017.2013.1.00.E.03.31.PDF </p> <p> 5. <strong> <a href="1./ZAF000030086.2013.A.00.E.08.31.PDF"> </a> </strong> (score: 680) <br/> ./ZAF000030086.2013.A.00.E.08.31.PDF </p> <p> 6. <strong> <a href="1./FRA000040051.2011.AMF.00.L.12.31.PDF"> </a> </strong> (score: 608) <br/> ./FRA000040051.2011.AMF.00.L.12.31.PDF </p> <p> 7. <strong> <a href="1./ZAF000030068.2013.A.00.E.06.30.PDF"> </a> </strong> (score: 608) <br/> ./ZAF000030068.2013.A.00.E.06.30.PDF </p> <p> 8. <strong> <a href="1./IRL000030033.2013.A.00.E.03.31.PDF"> </a> </strong> (score: 524) <br/> ./IRL000030033.2013.A.00.E.03.31.PDF </p> <p> 9. <strong> <a href="1./ZAF000030504.2012.U.00.E.06.30.PDF"> </a> </strong> (score: 524) <br/> ./ZAF000030504.2012.U.00.E.06.30.PDF </p> <p> 10. <strong> <a href="1./MYS000091186.2013.A.00.E.09.30.PDF"> </a> </strong> (score: 524) <br/> ./MYS000091186.2013.A.00.E.09.30.PDF </p> <p> 11. <strong> <a href="1./ZWE000030003.2013.A.00.E.02.28.PDF"> </a> </strong> (score: 414) <br/> ./ZWE000030003.2013.A.00.E.02.28.PDF </p> <p> 12. <strong> <a href="1./CAN000031319.2013.3.00.E.09.30.PDF"> </a> </strong> (score: 414) <br/> ./CAN000031319.2013.3.00.E.09.30.PDF </p> <p> 13. <strong> <a href="1./NGA000030110.2008.A.00.E.12.31.PDF"> </a> </strong> (score: 262) <br/> ./NGA000030110.2008.A.00.E.12.31.PDF </p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477755 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 Okay. Do you mean actually show you the link to what I am doing? Here is the link: http://64.244.240.221:8080/pdfsearch.php if you type in test you will get results. also here is the page source when i right clicked and selected page source. <html> <head></head> <body> <h2>Search Results</h2> Found 13 match(es) for 'test'. <p> 1. <strong> <a href="1./ITA000030409.2013.3.00.L.09.30.PDF"> </a> </strong> (score: 1000) <br/> ./ITA000030409.2013.3.00.L.09.30.PDF </p> <p> 2. <strong> <a href="1./POL000040092.2013.A.00.E.12.31.PDF"> </a> </strong> (score: 737) <br/> ./POL000040092.2013.A.00.E.12.31.PDF </p> <p> 3. <strong> <a href="1./ISR000030655.2011.A.00.L.12.31.PDF"> </a> </strong> (score: 737) <br/> ./ISR000030655.2011.A.00.L.12.31.PDF </p> <p> 4. <strong> <a href="1./SWE000040017.2013.1.00.E.03.31.PDF"> </a> </strong> (score: 680) <br/> ./SWE000040017.2013.1.00.E.03.31.PDF </p> <p> 5. <strong> <a href="1./ZAF000030086.2013.A.00.E.08.31.PDF"> </a> </strong> (score: 680) <br/> ./ZAF000030086.2013.A.00.E.08.31.PDF </p> <p> 6. <strong> <a href="1./FRA000040051.2011.AMF.00.L.12.31.PDF"> </a> </strong> (score: 608) <br/> ./FRA000040051.2011.AMF.00.L.12.31.PDF </p> <p> 7. <strong> <a href="1./ZAF000030068.2013.A.00.E.06.30.PDF"> </a> </strong> (score: 608) <br/> ./ZAF000030068.2013.A.00.E.06.30.PDF </p> <p> 8. <strong> <a href="1./IRL000030033.2013.A.00.E.03.31.PDF"> </a> </strong> (score: 524) <br/> ./IRL000030033.2013.A.00.E.03.31.PDF </p> <p> 9. <strong> <a href="1./ZAF000030504.2012.U.00.E.06.30.PDF"> </a> </strong> (score: 524) <br/> ./ZAF000030504.2012.U.00.E.06.30.PDF </p> <p> 10. <strong> <a href="1./MYS000091186.2013.A.00.E.09.30.PDF"> </a> </strong> (score: 524) <br/> ./MYS000091186.2013.A.00.E.09.30.PDF </p> <p> 11. <strong> <a href="1./ZWE000030003.2013.A.00.E.02.28.PDF"> </a> </strong> (score: 414) <br/> ./ZWE000030003.2013.A.00.E.02.28.PDF </p> <p> 12. <strong> <a href="1./CAN000031319.2013.3.00.E.09.30.PDF"> </a> </strong> (score: 414) <br/> ./CAN000031319.2013.3.00.E.09.30.PDF </p> <p> 13. <strong> <a href="1./NGA000030110.2008.A.00.E.12.31.PDF"> </a> </strong> (score: 262) <br/> ./NGA000030110.2008.A.00.E.12.31.PDF </p> </body> </html> Why do you need to see this information I just showed it to you? Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477756 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 It seems that this line of code should already be making a link out of something <a href="<?php echo $r->swishdocpath; ?>"> <?php echo $r->swishtitle; ?> </a> Can you post the exact html output to the browser that the page is currently doing? So a copy of hte view source from the browser. What exactly are you asking me to do? What is in my original post is the output I type in the word test and what shows up after you hit enter is the list of PDF files that have that word in it. Also what does the page source have to do with the above code telling me that it should already be making a link out of something? Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477757 Share on other sites More sharing options...
cyberRobot Posted May 1, 2014 Share Posted May 1, 2014 Try adding the anchor tag around something you can click. For example, you could try something like this: <a href="<?php echo $r->swishdocpath; ?>"> <?php echo $r->swishdocpath; ?> </a> Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477782 Share on other sites More sharing options...
fastsol Posted May 1, 2014 Share Posted May 1, 2014 (edited) Its seems that $r->swishtitle isn't actually outputting anything and is causing a error in the html cause the closing </a> is not being output to the browser either. The other thing is the file path is printing a . at the beginning of the string for some reason. That can't be there to make this work. Get rid of that with ltrim() maybe and then prepend pdf2 to the string to make it point to the correct folder. <a href="<?php echo '/pdf2', ltrim($r->swishdocpath, '.'); ?>"> <?php echo $r->swishtitle; ?> // Find out why this is not echoing anything. </a> Edited May 1, 2014 by fastsol Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477783 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 Its seems that $r->swishtitle isn't actually outputting anything and is causing a error in the html cause the closing </a> is not being output to the browser either. The other thing is the file path is printing a . at the beginning of the string for some reason. That can't be there to make this work. Get rid of that with ltrim() maybe and then prepend pdf2 to the string to make it point to the correct folder. <a href="<?php echo '/pdf2', ltrim($r->swishdocpath, '.'); ?>"> <?php echo $r->swishtitle; ?> // Find out why this is not echoing anything. </a> This is what I did: <a href="<?php echo '/pdf2', ltrim($r->swishdocpath, '.') ; ?>"> <?php echo $r->swishdocpath; ?> </a> When I did this I typed in the word test and the PDF files that have the word test in them appear as part of my search results. Not only that but I am now able to open up those PDF files within the search results and I am able to open up those individual PDF files. They now link up to the individual PDF files within the directory where they are stored. Did I really need the swishtitle? Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477810 Share on other sites More sharing options...
cyberRobot Posted May 1, 2014 Share Posted May 1, 2014 (edited) Did I really need the swishtitle? That's up to you. I would imagine that swishtitle is supposed to display the title of the document which would be useful to some. If you want to use it, you'll need to go back to the documentation and/or do some research to see what's going on. Note that I haven't used the SWISH object before, so I'm not sure why it doesn't work. Edited May 1, 2014 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477814 Share on other sites More sharing options...
cyberRobot Posted May 1, 2014 Share Posted May 1, 2014 Also note that I don't understand why this <a href="<?php echo $r->swishdocpath; ?>"> outputs this <a href="1./ITA000030409.2013.3.00.L.09.30.PDF"> And this <?php echo $r->swishdocpath; ?> ouputs this ./ITA000030409.2013.3.00.L.09.30.PDF I don't see where that "1" comes from. It sounds like your links are working now, so maybe it's not an issue. Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477815 Share on other sites More sharing options...
fastsol Posted May 1, 2014 Share Posted May 1, 2014 The number is from this <?php echo $r->swishreccount; ?>. For some reason the swishtitle was not displaying and seemed to cause the closing </a> not to display either. Not sure if he got the </a> part fixed by removing the title but he says the links work now. Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477816 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 The number is from this <?php echo $r->swishreccount; ?>. For some reason the swishtitle was not displaying and seemed to cause the closing </a> not to display either. Not sure if he got the </a> part fixed by removing the title but he says the links work now. I am able to connect to the PDF files now. But my question is if I wanted to change the directory to /home/wvbadmin/pdf2 and grab those PDF files from there. Would I be able to do it? If so how? Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477820 Share on other sites More sharing options...
mike2 Posted May 1, 2014 Author Share Posted May 1, 2014 The number is from this <?php echo $r->swishreccount; ?>. For some reason the swishtitle was not displaying and seemed to cause the closing </a> not to display either. Not sure if he got the </a> part fixed by removing the title but he says the links work now. I want to be able to search for PDF files but this time instead of them being run from the /var/www/html/pdf2 directory, I want to see if I can run them from the /home/wvbadmin/pdf2 directory. I just tried doing that and when I typed in my example word 'test' to do it. I got this error message: Search ResultsERROR: Could not open the index file '/home/wvbadmin/pdf2/index.swish-e': Permission denied Here is my modified code: I want to be able to search for PDF files as well as the ability to be linked up to each PDF file respectively. <html> <head></head> <body> <?php if (!isset($_POST['q'])) { ?> <h2>Search</h2> <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <input type="text" name="q" size="30" /> </form> <?php } else { ?> <h2>Search Results</h2> <?php try { // create object // $swish = new Swish('/usr/local/apache/htdocs/swish/index.swish-e'); $swish = new Swish('/home/wvbadmin/pdf2/index.swish-e'); // get and run query from command-line $queryStr = htmlentities($_POST['q']); $result = $swish->query($queryStr); ?> Found <?php echo $result->hits; ?> match(es) for '<?php echo $queryStr; ?>' . <?php // iterate over result set // print details for each match while($r = $result->nextResult()) { ?> <p> <?php echo $r->swishreccount; ?> <strong> <a href="<?php echo '/pdf2', ltrim($r->swishdocpath, '.') ; ?>"> <?php echo $r->swishdocpath; ?> </a> </strong> (score: <?php echo $r->swishrank; ?>) <br/> <?php echo $r->swishdocpath; ?> </p> <?php } } catch (Exception $e) { die('ERROR: ' . $e->getMessage()); } } ?> </body> </html> What I don't get is why will it work in the /var/www/html/pdf2 directory but not for the /home/wvbadmin/pdf2 directory? I get a permisssion error as shown above. I want to grab and search the PDF files from that particular directory. Quote Link to comment https://forums.phpfreaks.com/topic/288139-help-with-getting-pdf-search-results-to-appear-as-hyperlinks/#findComment-1477822 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.