coolshrimp Posted July 28, 2015 Share Posted July 28, 2015 (edited) how come if i do this it only runs once and shows rows with "silver" but not the ons with gold?do i need to clear $rows before running the while statement? if so how would i do that? <?php//Open the file.$fileHandle = @fopen("myfile.csv", "r") or die(print_r(error_get_last(),true));?><strong>Silver Images</strong><br><br><?php$count = 0;//Loop through the CSV rows.while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) { $count++; // Skip First 2 Rows if ($count > 2) { if ($row[3] === 'Silver') {echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?><br><br><strong>Gold Images</strong><br><br><?php$count = 0;//Loop through the CSV rows.while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) { $count++; // Skip First 2 Rows if ($count > 2) { if ($row[3] === 'Gold') {echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?> This works but i don't want to have to set $filehandle every time i have 10 spots on the same webpage that i need content loaded from the same .csv file : <strong>Silver Images</strong><br><br><?php//Open the file.$fileHandle = @fopen("myfile.csv", "r") or die(print_r(error_get_last(),true));$count = 0;//Loop through the CSV rows.while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) { $count++; // Skip First 2 Rows if ($count > 2) { if ($row[3] === 'Silver') {echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?><br><br><strong>Gold Images</strong><br><br><?php//Open the file.$fileHandle = @fopen("myfile.csv", "r") or die(print_r(error_get_last(),true));$count = 0;//Loop through the CSV rows.while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) { $count++; // Skip First 2 Rows if ($count > 2) { if ($row[3] === 'Gold') {echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?> Edited July 28, 2015 by coolshrimp Link to comment Share on other sites More sharing options...
justsomeguy Posted July 28, 2015 Share Posted July 28, 2015 http://php.net/manual/en/function.fseek.phpAn alternative would be to open the file once, read everything into an array, close the file, and then use the array instead of the file handle. Your current code never closes the file. Link to comment Share on other sites More sharing options...
coolshrimp Posted July 28, 2015 Author Share Posted July 28, 2015 cool ok i got it. dose all this look ok? <?phpfunction csv_to_array($file_name) { $data = $header = array(); $i = 0; $file = fopen($file_name, 'r'); while (($line = fgetcsv($file)) !== FALSE) { if( $i==0 ) { $header = $line; } else { $data[] = $line; } $i++; } fclose($file); return $data;}?><?php$file_name = 'myfile.csv';$thearray = csv_to_array($file_name);?><strong>Silver Images</strong><br><br><?php$count = 0;foreach ($thearray as $row) { $count++; if ($count > 1) { if ($row[3] === 'Platinum') { echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?><br><br><strong>Corporate Images</strong><br><br><?php$count = 0;foreach ($thearray as $row) { $count++; if ($count > 1) { if ($row[3] === 'Corporate') { echo <<<EOF<a href="{$row[4]}" target="_blank"><img src="../../images/Logos/{$row[1]}" style="max-width:{$row[2]}px;" alt="{$row[0]}"></a>EOF; } }}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted July 28, 2015 Share Posted July 28, 2015 It looks fine to me. Link to comment Share on other sites More sharing options...
coolshrimp Posted July 28, 2015 Author Share Posted July 28, 2015 Great thank you. Link to comment Share on other sites More sharing options...
coolshrimp Posted July 28, 2015 Author Share Posted July 28, 2015 (edited) if i wanted to hide the function in another file whats the best way to include it?that will automatically get the included file no matter what directory a page is calling it from?also should i use Include, Include_once, require? Would i do: include ($_SERVER['DOCUMENT_ROOT'] . "Scritps/PHP/CSV.php";); inside"CSV.php": <?phpfunction csv_to_array($file_name) { $data = $header = array(); $i = 0; $file = fopen($file_name, 'r'); while (($line = fgetcsv($file)) !== FALSE) { if( $i==0 ) { $header = $line; } else { $data[] = $line; } $i++; } fclose($file); return $data;}?> Edited July 28, 2015 by coolshrimp Link to comment Share on other sites More sharing options...
justsomeguy Posted July 28, 2015 Share Posted July 28, 2015 Yeah something like that should be fine. also should i use Include, Include_once, require?They all do slightly different things, use the one that makes the most sense. Link to comment Share on other sites More sharing options...
coolshrimp Posted July 29, 2015 Author Share Posted July 29, 2015 (edited) Any tricks you know for the includes to work on both localhost and web server without setting absolute paths on all files?seems i have to use absolute path: include '../php/CSVReader.php'; If u use these: include (getcwd() . '/php/CSVReader.php');include ($_SERVER['DOCUMENT_ROOT'] . '/php/CSVReader.php'); On localhost it returns:"W:MySitesSiteNameSitedialogue/php/CSVReader.php""W:/MySites/php/CSVReader.php"And on server it returns:"/home2/server/public_html/HostedSites/SiteName/dialogue/php/CSVReader.php"/home2/server/public_html/HostedSites/SiteName/php/CSVReader.php" On localhost it should be:"W:MySitesSiteNameSitephp/CSVReader.php" And on server it should be:"www.SiteName.com/php/CSVReader.php" or "/home2/server/public_html/HostedSites/SiteName/php/CSVReader.php" Edited July 29, 2015 by coolshrimp Link to comment Share on other sites More sharing options...
justsomeguy Posted July 29, 2015 Share Posted July 29, 2015 It sounds like you don't have document root set correctly on your local server. Link to comment Share on other sites More sharing options...
coolshrimp Posted July 29, 2015 Author Share Posted July 29, 2015 since i have multiple sites in one folder and im using a singe vhost in xammp, document_root will not work so instead iv done this //CSV Reader Scripts$FileLocaton = $_SERVER['DOCUMENT_ROOT'] . '/php/CSVReader.php';if (file_exists($FileLocaton)) { //Automatic Path include $FileLocaton;} else { //Backup Maual Path include '../php/CSVReader.php';} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now