mike2 Posted May 20, 2014 Share Posted May 20, 2014 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 -ltotal 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.phpdrwxr-xr-x. 2 root root 12288 May 12 15:30 pdf2lrwxrwxrwx. 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.pngWhat 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. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/ Share on other sites More sharing options...
Barand Posted May 20, 2014 Share Posted May 20, 2014 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]; Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480223 Share on other sites More sharing options...
mike2 Posted May 20, 2014 Author Share Posted May 20, 2014 (edited) 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 May 20, 2014 by mike2 Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480224 Share on other sites More sharing options...
Barand Posted May 20, 2014 Share Posted May 20, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480225 Share on other sites More sharing options...
mike2 Posted May 20, 2014 Author Share Posted May 20, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480226 Share on other sites More sharing options...
Barand Posted May 20, 2014 Share Posted May 20, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480230 Share on other sites More sharing options...
mike2 Posted May 20, 2014 Author Share Posted May 20, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480236 Share on other sites More sharing options...
Barand Posted May 20, 2014 Share Posted May 20, 2014 Of course you can. Just don't forget to include a purchase order and billing address. If you keep it the forums the advice is free. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480237 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 Of course you can. Just don't forget to include a purchase order and billing address. If you keep it the forums the advice is free. Okay I will keep it to the forums. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480307 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 (edited) 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.PDFCountry Name: ZWEYear: 2013WVB Number: ZWE000030003Company Name: ECONET WIRELESS ZIMBABWE 4 ./POL000040092.2013.A.00.E.12.31.PDF (score: 945)./POL000040092.2013.A.00.E.12.31.PDFCountry Name: POLYear: 2013WVB Number: POL000040092Company Name: ASSECOSEE Edited May 21, 2014 by mike2 Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480321 Share on other sites More sharing options...
Barand Posted May 21, 2014 Share Posted May 21, 2014 (edited) $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 May 21, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480323 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 $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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480331 Share on other sites More sharing options...
Barand Posted May 21, 2014 Share Posted May 21, 2014 You need to get the wvbnumber before you use it to access the country name Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480341 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 You need to get the wvbnumber before you use it to access the country name So in order to do this I would just move some code around then? How would I do this? Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480343 Share on other sites More sharing options...
Barand Posted May 21, 2014 Share Posted May 21, 2014 (edited) 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 May 21, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480346 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 You need to get the wvbnumber before you use it to access the country name I changed some code around and I was able to resolve it. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480347 Share on other sites More sharing options...
mike2 Posted May 21, 2014 Author Share Posted May 21, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288637-displaying-company-name-from-csv-list-for-web-search-results/#findComment-1480348 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.