Jump to content

Loading content from CSV


coolshrimp

Recommended Posts

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...