eevan79 Posted May 25, 2011 Share Posted May 25, 2011 Here is how I get categories for article listing. Article can be written in several categories at once. So I have this code: $category = '1,5,23,46'; //Category IDs $get_cats = explode("," , $category); $cats = ""; for ($i = 0; $i<count($get_cats); $i++ ) { $cat = trim($get_cats[$i]); $catSlug = get_value_of($cat, "inc/cat_ids.php"); $catName = str_replace("-"," ",$catSlug); $cats .= "<a href=\"index.php?category=$catSlug\">".strtoupper($catName)."</a>, "; } $cats = substr($cats,0,-2); As you can see there's get_value_of function that is used to read the category from the file. File is cat_ids.php and looks like this: 1='category-name-1'; 2='category-name-2'; 3='category name-3'; 4='category-name-4'; 5='category-name-5'; etc. I want to improve this in order not to read the file at each step ("for" loop). Means, one can load the file and then generate a list of (links) category compared to the numbers (Ids). So if $category is "1,3,4" I want to get list like this (by reading cat_ids.php): category-name-1, category name-3, category-name-4 With current function I read value for every ID from file in every step. So if article have 10 or more categories script load file 10 or more times. Looking for a solution to do this from a reading file only once. Thanks. Link to comment https://forums.phpfreaks.com/topic/237396-generate-array-list-from-file/ Share on other sites More sharing options...
Psycho Posted May 25, 2011 Share Posted May 25, 2011 I'm working on a freshly installed machine and don't have a test server set up yet, so none of this is tested (especially the regex = I think the single quote marks [all three] may need to be escaped with a forward-slash before each one): //Read master category list into array $categoryFile = "path/to/file/filename.txt"; $categoryList = array(); foreach(file($categoryFile) as $line) { preg_match("#(\d+)='([^']*)'#", $line, $match); $categoryList[$match[1]] = str_replace("-", " ", $match[2]); } //Process the current categories $categories = '1,5,23,46'; //Category IDs //Convert to array AND apply trim $categoryIDsAry = array_map('trim', explode("," , $categories)); //Create the HTML ouput $catsHTMLAry = array(); foreach ($categoryIDsAry as $catID) { $catSlug = $categoryList[$catID]; $catName = str_replace("-", " ", $catSlug); $catsHTMLAry[] = "<a href=\"index.php?category=$catSlug\">".strtoupper($catName)."</a>, "; } $catsHTMLStr = implode(', ' $catsAry); Link to comment https://forums.phpfreaks.com/topic/237396-generate-array-list-from-file/#findComment-1219861 Share on other sites More sharing options...
eevan79 Posted May 25, 2011 Author Share Posted May 25, 2011 Thanks. With few corrections it's working fine. $categoryFile = dirname($_SERVER['SCRIPT_FILENAME'])."/inc/cat_ids.php"; $categoryList = array(); foreach( file($categoryFile) as $line) { preg_match("#(\d+)='([^']*)'#", $line, $match); $categoryList[$match[1]] = str_replace("-", " ", $match[2]); } //$categories = $category; 1,5,23,46 $categoryIDsAry = array_map('trim', explode("," , $category)); $catsHTMLAry = array(); foreach ($categoryIDsAry as $catID) { $catSlug = $categoryList[$catID]; $catName = str_replace("-", " ", $catSlug); $catsHTMLAry[] = "<a href=\"index.php?category=$catSlug\">".strtoupper($catName)."</a> "; } $cats = implode(', ', $catsHTMLAry); Link to comment https://forums.phpfreaks.com/topic/237396-generate-array-list-from-file/#findComment-1220049 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.