nz_mitch Posted January 15, 2009 Share Posted January 15, 2009 Hi everyone, I've searched here and on Google and haven't been able to find anything to do quite what I want. I want to convert a CSV file to an HTML page (perhaps in tables) but be able to independently style each component. This is a Wordpress function that I've used for a Meet the Team page that pulls the data from the server and then lets me output each component as I wish using variables. global $wpdb; $query = "SELECT ID, user_nicename from $wpdb->users ORDER BY user_nicename"; $author_ids = $wpdb->get_results($query); // Loop through each author foreach($author_ids as $author) { // Get user data $curauth = get_userdata($author->ID); // If user level is above 0 or login name is "admin", display profile if($curauth->user_level > 1 AND $curauth->user_level < 5) { // Get link to author page $user_link = get_author_posts_url($curauth->ID); // Set default avatar (values = default, wavatar, identicon, monsterid) $avatar = 'default'; echo "<div id=\"team\"> <table width=\"500\" border=\"0\"> <tr> <td width=\"80\" rowspan=\"2\"><img class=\"team\" src=\"http://luxury-portfolio.co.nz/wp-content/ blogs.dir/16/files/userphoto/$curauth->user_nicename.jpg\" /></td> <td width=\"400\"><h2><a href=\"$user_link\">$curauth->display_name</a></h2></td> </tr> <tr> <td valign=\"top\"> <ul> <li><i>$curauth->jabber</i></li> <li>p. $curauth->aim</li> <li>e. <a href=\"mailto:$curauth->user_email\">$curauth->user_email</li> <li><a href=\"$user_link\">View listings</a></li> </ul> </td> </tr> </table> </div>"; } } } } I'd like to be able to do the same but instead of pulling the data from Wordpress* I'd like to get it straight from a CSV file. Can anyone help me? *Just to clarify I don't intend on this being part of a Wordpress installation, it just seemed to be the easiest way to explain what I'm looking for. I just want a small app I can sit in a sub-directory so I can convert a CSV file to a catalogue. Ideally I'd like to be able to build an upload component into the app, but in the short term just being able to do it via FTP and specifying the filename in the PHP would be great. Link to comment https://forums.phpfreaks.com/topic/140898-convert-csv-to-html-customise-each-item/ Share on other sites More sharing options...
chronister Posted January 15, 2009 Share Posted January 15, 2009 Here is a script I use for getting csv results. It creates an associative array out of the file with field headers on line 1. <?php function getCSV($file) { //Move through a CSV file, and output an associative array for each line ini_set("auto_detect_line_endings", 1); $current_row = 1; $handle = fopen("$file", "r"); while ( ($data = fgetcsv($handle, 100000, ",") ) !== FALSE ) { $number_of_fields = count($data); if ($current_row == 1) { //Header line for ($c=0; $c < $number_of_fields; $c++) { $header_array[$c] = $data[$c]; } } else { //Data line for ($c=0; $c < $number_of_fields; $c++) { $data_array[$header_array[$c]] = $data[$c]; } //print_r($data_array); $dataArray[]=$data_array; //set this var to the name you want to access the data with } $current_row++; } fclose($handle); return $dataArray; } ?> There is a solution to getting the csv data, now it is up to you to loop through the array and format how you wish. Nate Link to comment https://forums.phpfreaks.com/topic/140898-convert-csv-to-html-customise-each-item/#findComment-737531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.