bollenbach Posted April 18, 2007 Share Posted April 18, 2007 heyI'm working on an assignment that requires the use of a csv file... have any of you worked with CSV files? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 18, 2007 Share Posted April 18, 2007 Sure, they are like any other text file that you might open and parse. Here is a function to help read a CSV file:http://www.php.net/fgetcsvThere is also a fputcsv function for writing. Link to comment Share on other sites More sharing options...
bollenbach Posted April 18, 2007 Author Share Posted April 18, 2007 What I'm trying to do is this:I have an ARRAY for a bunch of venues with addresses and telephone numbers (200 venues)THIS ARRAY IS DISPLAYED AS A TABLE(30 venues per page on several pages) -- this is all doneThen I have a CSV file with categories:/* * This array represents categories in which event venues may be placed. * The categories listed are not meant to be exhaustive. */0,"bar"1,"dance club"2,"lounge"3,"pub"4,"restaurant"5,"comedy"6,"art"7,"live theatre"8,"film"9,"other"and another file with/* * This data represents categories assigned to each venue. The first * element in each row represents the unique venue ID. All remaining * elements represent the various categories assigned to a venue. This * sample only shows how the array may be constructed -- remaining data * is missing and must be filled in. */0,0,41,62,63,64,7(it goes on till about 200)and essentially what I have to do is... make a form so that I can manipulate this data..and I want the category info to be displayed where the array for the venues get displayed (in the table)does anyone have a good idea for an approach to this? Link to comment Share on other sites More sharing options...
justsomeguy Posted April 18, 2007 Share Posted April 18, 2007 It would be way easier to use a database instead of a CSV file for this. You could have one table for the categories, a table for venues, etc, and use SQL statements to link everything together like you need. That may sound intimidating but it would be quicker and easier then CSV.If you don't have a choice, then it will probably be easier to parse everything up yourself. You can use the file_get_contents function to read the entire file into a string, and then use explode on the string to split it up into an array of lines. So the end result is that you have an array of lines from the file. Each line can also be split up on commas, so you end up with a multidimensional array of lines and values.That would end up looking something like this: $data = file_get_contents ("data.csv");$lines = explode("\n", $data);for($i = 0; $i < count($lines); $i++) $lines[$i] = explode(",", $lines[$i]); So, when you are displaying something and you want to find out which category it is in, and you have the ID number of the item, you would search through the array of categories until you find a matching ID number for the item, get the ID of the category it is in, and then search through the array of categories to find the title. That would look something like this: $done = false;$found = false;$cat_id = 0;$i = 0;while (!$done){ $current = $lines[$i]; $temp_cat_id = $current[0]; for ($j = 1; $j < count($current); $j++) { if ($current[$j] == $id_to_find) { $cat_id = $temp_cat_id; $done = true; $found = true; } } $i++; if ($i >= count($lines)) $done = true;}$cat_name = "";if ($found){ for ($i = 0; $i < count($categories); $i++) { if ($categories[$i][0] == $cat_id) { $cat_name = $categories[$i][1]; break; } }}if ($cat_name != ""){ echo "The venue is in the category '{$cat_name}'";} Link to comment Share on other sites More sharing options...
bollenbach Posted April 19, 2007 Author Share Posted April 19, 2007 Is there something else that I have to add in to this? or change about it to get it to work.. because so far it's not doing anything. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 19, 2007 Share Posted April 19, 2007 Well, the first piece of code is just to open the CSV file and break it up into lines and fields. Make sure the filename is correct. The second piece of code will find the category name for an item. So if you have item 9 and you know it is in category 3, the second piece of code will find the name of category 3. There's a variable called $id_to_find that should have the ID of the item, in this example 9. It's not doing that in my code. So before that code runs, you need to parse up the list of venues to display them. You didn't include that information in your post describing your CSV setup, all you showed was the file that holds the category names, and the file that links items to categories, not the file of items themselves. So, however you want to do that part, once you run this code to find the category name make sure you either change the $id_to_find variable name to match whatever variable holds the actual ID, or just copy the ID into that variable before the code runs. 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