Jump to content

Displaying company name from .csv list for web search results


mike2

Recommended Posts

I would like assistance in displaying a company name in my web search results.

 

Below is my code:

<html>

  <head></head>

  <body>

  <?php if (!isset($_POST['q'])) { ?>





<img src="/wvb-logo-slogen.png" border="0" />

    <h2>Search</h2>

    <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">

      <input type="text" name="q" size="30" />

    </form>



  <?php } else { ?>



<img src="/wvb-logo-slogen.png" border="0" />

    <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 '/pdf2', ltrim($r->swishdocpath, '.') ;  ?>">

         <?php echo $r->swishdocpath; ?>

      </a>

   </strong> (score: <?php echo $r->swishrank; ?>) <br/>

<?php echo $r->swishdocpath; ?><br />

<?php

//Split a filename  by .

$filenames = explode(".", $r->swishdocpath);

//get 3 chars from $filenames to $country

$country = substr($filenames[1],1,3);

echo 'Country name: '.$country."<br />";

//$filenames[2] = explode(".", $r->swishdocpath);

$year = substr($filenames[2],0,4);

echo 'Year: '.$year;

//$filenames = explode(".", $r->swishdocpath);

$wvbnumber = substr($filenames[1],1,12);

echo 'WVB Number: '.$wvbnumber;



?>



  </p>



 <?php

      }

    } catch (Exception $e) {

      die('ERROR: ' . $e->getMessage());

    }

  }

  ?>



  </body>

</html>

 

As you can see I am able now display the WVB number, country name and year. But my question to anyone is how would I display the company name that it corresponds to?

The names of the company are located in a .csv file called active_colist.csv and it is under the /var/www/html directory.

This is what my /var/www/html directory looks like:

[root@zeus wvbadmin]# cd /var/www/html
[root@zeus html]# ls -l
total 2140
-rw-r--r--. 1 root root 2110323 May 14 23:39 active_colist.csv
-rw-r--r--. 1 root root    6678 Apr 30 13:25 favicon.ico
-rw-r--r--. 1 root root   17256 May  5 16:02 h1
-rw-r--r--. 1 root root     113 Apr 29 16:45 hello.php
-rw-r--r--. 1 root root      19 Apr 24 23:53 info.php
drwxr-xr-x. 2 root root   12288 May 12 15:30 pdf2
lrwxrwxrwx. 1 root root      20 May  5 15:46 pdf3 -> /home/wvbadmin/pdf3/
-rw-r--r--. 1 root root    1227 May  6 16:33 pdfsearch2.php
-rw-r--r--. 1 root root    1204 May  6 15:13 pdfsearch3.php
-rw-r--r--. 1 root root    1625 May 19 23:27 pdfsearch.php
-rw-r--r--. 1 root root    1838 Apr 30 13:10 search.php
-rw-r--r--. 1 root root   10077 May 12 11:38 wvb-logo-slogen.png


What is in the .csv file is the first 12 characters that correspond to the company name. What PHP code would I use to grab the first 12 characters of search results match them up with what is in the .csv file and display the proper company name?

This is what is inside of the .csv file:

WVB_NUMBER,PRIMARY_SHORT_COMPANY_NAME,CURR_ISO_CNTRY_OF_INCORP
"AIA000030001","THE NATIONAL BANK OF ANGUILLA","AIA"
"AIA000030003","ANGUILLA ELECTRICITY COMPANY","AIA"
"AIA000030005","KMT-HANSA","AIA"
"ALB000040001","BURSE E TIRANES","ALB"
"ANT000020000","RORENTO","ANT"
"ANT000030001","INTRUM JUSTITIA","SWE"
"ANT000030002","ORTHOFIX INTERNATIONAL","ANT"
"ANT000030004","HAL TRUST","ANT"
"ARE000030001","MASHREQBANK","ARE"
"ARE000030002","COMMERCIAL BANK OF DUBAI","ARE"

 

Any assistance would be greatly appreciated.

Link to comment
Share on other sites

You could store the csv data in an array

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = $row[1];
}
fclose($fh);

then, when you have the $wvb_number

echo $companies[$wvb_number];
Link to comment
Share on other sites

 

You could store the csv data in an array

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = $row[1];
}
fclose($fh);

then, when you have the $wvb_number

echo $companies[$wvb_number];

Okay so where in my code would I put these lines of code? Do I incorporate this into what is being searched? If so, how? Keep in mind that I have never used php before so this is my first time.

 

After this?:

//$filenames = explode(".", $r->swishdocpath);
$wvbnumber = substr($filenames[1],1,12);
echo 'WVB Number: '.$wvbnumber;
?>
Edited by mike2
Link to comment
Share on other sites

Storing the csv data would be done only once so that can go anywhere before you need the data.

 

Echoing the company name would go, well, where you want to echo the company name.

Here is what I did and my output disappeared on me upon hitting refresh: 

 

 

<?php
$file = '/var/www/html/active_colist.csv'; //Suggested code
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = $row[1];
}
fclose($fh);
?>

  <?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; ?><br />
<?php
//Split a filename  by .
$filenames = explode(".", $r->swishdocpath);
//get 3 chars from $filenames to $country
$country = substr($filenames[1],1,3);
echo 'Country Name: '.$country."<br />";
//$filenames[2] = explode(".", $r->swishdocpath);
$year = substr($filenames[2],0,4);
echo 'Year: '.$year."<br />";
//$filenames = explode(".", $r->swishdocpath);
$wvbnumber = substr($filenames[1],1,12);
echo 'WVB Number: '.$wvbnumber."<br />";
echo 'Company Name: '$companies[$wvb_number]; //what is supposed to be outputted to the screen
 
 

 

My question is where did I go wrong?

Link to comment
Share on other sites

echo 'WVB Number: '.$wvbnumber."<br />";
echo 'Company Name: ' . $companies[$wvb_number];

                                     add ^                      change^                                                                     

 

Also check the $companies array was created OK

echo "<pre>", print_r($companies, 1) . "</pre>";

If it's empty it could be an error in the file path.

Link to comment
Share on other sites

echo 'WVB Number: '.$wvbnumber."<br />";

echo 'Company Name: ' . $companies[$wvb_number];

                                     add ^                      change^                                                                     

 

Also check the $companies array was created OK

echo "<pre>", print_r($companies, 1) . "</pre>";

If it's empty it could be an error in the file path.

I did just that and it worked. Thank you. If I have any other questions can I message you privately?

Link to comment
Share on other sites

 

You could store the csv data in an array
$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
$companies[$row[0]] = $row[1];
}
fclose($fh);
then, when you have the $wvb_number
echo $companies[$wvb_number];
 
Okay I just put in a new .csv file. I haven't made changes to my code yet because I am afraid I might mess something up. Now my question is how would I go about changing the code so that the full country name is displayed?
 
This is what is inside the new .csv file:

 

"AIA000030001","THE NATIONAL BANK OF ANGUILLA","AIA","Anguilla"
"AIA000030003","ANGUILLA ELECTRICITY COMPANY","AIA","Anguilla"
"AIA000030005","KMT-HANSA","AIA","Anguilla"
"ALB000040001","BURSE E TIRANES","ALB","Albania"
"ANT000020000","RORENTO","ANT","Netherlands Antilles"
"ANT000030001","INTRUM JUSTITIA","SWE","Kingdom of Sweden"

 

This is what the code currently looks like:

<?php

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = $row[1];
}
fclose($fh);


//Split a filename  by .
$filenames = explode(".", $r->swishdocpath);
//get 3 chars from $filenames to $country
$country = substr($filenames[1],1,3);
echo 'Country Name: '.$country."<br />";
//$filenames[2] = explode(".", $r->swishdocpath);
$year = substr($filenames[2],0,4);
echo 'Year: '.$year."<br />";
//$filenames = explode(".", $r->swishdocpath);
$wvb_number = substr($filenames[1],1,12);
echo 'WVB Number: '.$wvb_number."<br />";
echo 'Company Name: '.$companies[$wvb_number];


?>

This is what the current output looks like:

 

3 ./ZWE000030003.2013.A.00.E.02.28.PDF (score: 1000)
./ZWE000030003.2013.A.00.E.02.28.PDF
Country Name: ZWE
Year: 2013
WVB Number: ZWE000030003
Company Name: ECONET WIRELESS ZIMBABWE

4 ./POL000040092.2013.A.00.E.12.31.PDF (score: 945)
./POL000040092.2013.A.00.E.12.31.PDF
Country Name: POL
Year: 2013
WVB Number: POL000040092
Company Name: ASSECOSEE

Edited by mike2
Link to comment
Share on other sites

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = array('company' => $row[1], 'country' => $row[3]);    // CHANGED LINE
}
fclose($fh);

Then, to display the company it will now be

echo 'Company: ' . $companies[$wvbnumber]['company];

and for the country

echo 'Country: ' . $companies[$wvbnumber]['country'];
Edited by Barand
Link to comment
Share on other sites

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = array('company' => $row[1], 'country' => $row[3]);    // CHANGED LINE
}
fclose($fh);

Then, to display the company it will now be

echo 'Company: ' . $companies[$wvbnumber]['company];

and for the country

echo 'Country: ' . $companies[$wvbnumber]['country'];

All right. I made those changes but now it is skipping a line for some reason.

 

Let me show you what I mean. When I took a look at my results I saw that the country code had skipped a line over.

 

When I did the following:

 

[root@zeus html]# grep IDN000030159 active_colist.csv
"IDN000030159","FAJAR SURYA WISESA","IDN","Republic of Indonesia"

 

I saw the above result. However if you take a look at my search results for this particular item, the line for that particular country is being skipped and as you can see Republic of Indonesia is supposed to correspond with ./IDN000030159.2013.A.00.E.12.31.PDF:

 

2 ./IDN000030159.2013.A.00.E.12.31.PDF (score: 1000)

./IDN000030159.2013.A.00.E.12.31.PDF

Country:

Year: 2013

WVB Number: IDN000030159

Company Name: FAJAR SURYA WISESA

3 ./ZWE000030003.2013.A.00.E.02.28.PDF (score: 1000)

./ZWE000030003.2013.A.00.E.02.28.PDF

Country: Republic of Indonesia

Year: 2013

WVB Number: ZWE000030003

Company Name: ECONET WIRELESS ZIMBABWE

Is there a way to correct this so that the .csv file is being read correctly with the proper code:

 

 

<?php

$file = '/var/www/html/active_colist.csv';
$fh = fopen($file, 'r');
$companies = array();
$row = fgetcsv($fh, 1024); // ignore header
while ($row = fgetcsv($fh, 1024)) {
    $companies[$row[0]] = array('company' => $row[1], 'country' => $row[3]); //changed line
   }
fclose($fh);

//Split a filename  by .
$filenames = explode(".", $r->swishdocpath);
//get 3 chars from $filenames to $country
$country = substr($filenames[1],1,3);
echo 'Country: '.$companies[$wvb_number]['country']."<br />";
//echo 'Country Name: '.$country."<br />";
//$filenames[2] = explode(".", $r->swishdocpath);
$year = substr($filenames[2],0,4);
echo 'Year: '.$year."<br />";
//$filenames = explode(".", $r->swishdocpath);
$wvb_number = substr($filenames[1],1,12);
echo 'WVB Number: '.$wvb_number."<br />";
echo 'Company Name: '.$companies[$wvb_number]['company'];
//echo 'Country: '.$companies[$wvb_number]['country'];
?>
 
Link to comment
Share on other sites

Cut and paste

 

EDIT: Here's the spoonfeeding answer

//Split a filename  by .
$filenames = explode(".", $r->swishdocpath);
//get 3 chars from $filenames to $country                    <-----------+
$country = substr($filenames[1],1,3);                                    |
echo 'Country: '.$companies[$wvb_number]['country']."<br />";            |
//echo 'Country Name: '.$country."<br />";                               |
//$filenames[2] = explode(".", $r->swishdocpath);                        |
$year = substr($filenames[2],0,4);                                       |
echo 'Year: '.$year."<br />";                                            |
//$filenames = explode(".", $r->swishdocpath);                           |
$wvb_number = substr($filenames[1],1,12);          //------- MOVE ME ----+
echo 'WVB Number: '.$wvb_number."<br />";
echo 'Company Name: '.$companies[$wvb_number]['company'];
Edited by Barand
Link to comment
Share on other sites

 

Cut and paste

 

EDIT: Here's the spoonfeeding answer

//Split a filename  by .
$filenames = explode(".", $r->swishdocpath);
//get 3 chars from $filenames to $country                    <-----------+
$country = substr($filenames[1],1,3);                                    |
echo 'Country: '.$companies[$wvb_number]['country']."<br />";            |
//echo 'Country Name: '.$country."<br />";                               |
//$filenames[2] = explode(".", $r->swishdocpath);                        |
$year = substr($filenames[2],0,4);                                       |
echo 'Year: '.$year."<br />";                                            |
//$filenames = explode(".", $r->swishdocpath);                           |
$wvb_number = substr($filenames[1],1,12);          //------- MOVE ME ----+
echo 'WVB Number: '.$wvb_number."<br />";
echo 'Company Name: '.$companies[$wvb_number]['company'];

I did just that a few minutes ago. Thanks again.

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.