Jump to content

Help with getting PDF search results to appear as Hyperlinks


mike2

Recommended Posts

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 Results

Found 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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

 

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?

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by fastsol
Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

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 by cyberRobot
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 Results

ERROR: 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.