pgabriel Posted May 12, 2007 Share Posted May 12, 2007 I want to rewrite the categories. $catfile = .txt file where the categories and subcategories are, like "Business::Accounting & Finance" I wish to replace "::","/" - "&","and" - " ","-" And have something like: Folder/Business/Accounting-and-Finance Could somebody help me with this? What should i write for URL`s? My PHP code: <?php $catary = file($catfile); $lastmajor = "xxnone"; for ($i=0; $i<count($catary); $i++) { $cdisp = rtrim($catary[$i]); $cmach = rawurlencode($cdisp); $cdisp2 = explode("::", $cdisp); $cmach2 = explode("%3A%3A", $cmach); $majorlink = "<a class=catmajor href='".$baseHREF."Folder/show.php?cat=" . $cmach2[0] . "'><strong>" . $cdisp2[0] ."</strong></a>"; $minorlink = "<a class=catminor href='".$baseHREF."Folder/show.php?cat=" . $cmach2[0] . "%3A%3A" . $cmach2[1] . "'>" . $cdisp2[1] ."</a>"; if ($cmach2[0] != $lastmajor) { $cats .= "<p>" . $majorlink . " \n"; $lastmajor = $cmach2[0]; $cats .= " <br>" . $minorlink; } else { $cats .= ", " . $minorlink; } } ?> <?php echo "$cats"; ?> Thanks a lot. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/ Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 <?php function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } echo rewritecategory("Business::Accounting & Finance"); ?> That function runs the rules you said at the beginning of your post to rewrite the categories to how you would like them. Hope that helps --- EDIT --- Is that what you're after? I don't fully understand what you want. I'd do something like this if you just want links: <?php function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file($catfile); foreach($catary as $line) { $category = rewritecategory($line); echo "<p><a href=\"$category\">$category</a></p>"; } ?> Output: <p><a href="Business/Accounting-and-Finance">Business/Accounting-and-Finance</a></p> Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251236 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 Thanks a lot. It`s working somehow. I will test it right away. I have to implement the code in urls to output something like: <b>The-Category</b> Sub-Category-1, Sub-Category-2, etc... Thanks again. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251277 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 <?php function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file($catfile); $output = array(); foreach($catary as $line) { $category = rewritecategory($line); array_push($output, "<a href=\"$category\">$category</a>"); } $result = join(", ", $output); echo $result; ?> Not tested, that's what you need to do though --- EDIT --- Just tested it, works fine Where do you want categories and subcategories to be read from? The $catfile? Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251279 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 Thanks, Yes the categories are read from $catfile Here is my test work: submitpanel. com/Windows Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251291 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 So the format of the strings is: Category::Subcategory ? Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251292 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 Yes. Sorry, i think i didn`t mention. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251294 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 Very busy right now, I'll try and do it later on if nobody beats me! Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251300 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 Ok. Again, thank you very much for your time. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251302 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 This is my code now. But still not working. function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file("category.txt"); $lastmajor = "xxnone"; for ($i=0; $i<count($catary); $i++) { $cdisp = rtrim($catary[$i]); $cmach = rawurlencode($cdisp); $cdisp2 = explode("::", $cdisp); $cmach2 = explode("%3A%3A", $cmach); $majorlink = "<a class=catmajor href='".$baseHREF."/".$cmach2[0]."'><strong>" . $cdisp2[0] ."</strong></a>"; $minorlink = "<a class=catminor href='".$baseHREF."/" .$cmach2[0] . "%3A%3A" . $cmach2[1] . "'>" . $cdisp2[1] ."</a>"; if ($cmach2[0] != $lastmajor) { $cats .= "<p>" .$majorlink . " \n"; $lastmajor = $cmach2[0]; $cats .= " <br>" . $minorlink; } else { $cats .= ", " . $minorlink; } } echo "$cats"; I`ve attached de category.txt file. Still need some help to output the categories. Thanks. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251367 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 <?php function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file("category.txt"); foreach($catary as $line) { $newline = rewritecategory($line); $explode = explode("/", $newline); $category = $explode[0]; $sub = $explode[1]; if(!is_array($headings[$category])) { $headings[$category] = array(); } array_push($headings[$category], $sub); } foreach($headings as $mainheading => $subheading) { echo "<h3>$mainheading</h3>\n"; foreach($headings[$mainheading] as $sub) { echo "$sub<br />\n"; } } ?> Now the output is sorted, how do you want the links to work? Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251373 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 It works very very well. Thanks a lot. I will arrange them from here. THANKS! Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251375 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 You're welcome! Remember to click Solved Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251376 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 Another thing. How to display the anchor like they are in txt file but without :: Check the link. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251387 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 For example: "Business::Accounting & Finance" The category: <a href=".../Business">Business</a> Subcategory: <a href="..."/Business/Accounting-and-Finance">Accounting & Finance</a> Thanks. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251390 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 <?php function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file("category.txt"); foreach($catary as $line) { $newline = rewritecategory($line); $explode = explode("/", $newline); $category = $explode[0]; $sub = $explode[1]; if(!is_array($headings[$category])) { $headings[$category] = array(); } array_push($headings[$category], $sub); } foreach($headings as $mainheading => $subheading) { echo "<h3><a href=\"$mainheading\">$mainheading</a></h3>\n"; foreach($headings[$mainheading] as $sub) { echo "<a href=\"$mainheading/$sub\">$sub</a><br />\n"; } } ?> Not sure if the subheading links will work though. --- EDIT --- Just got round to testing, works fine Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251393 Share on other sites More sharing options...
pgabriel Posted May 12, 2007 Author Share Posted May 12, 2007 I did this but the categories anchors look like: Audio-Encoders-Decoders. This means that they are the same with the rewritten ones. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251546 Share on other sites More sharing options...
chigley Posted May 12, 2007 Share Posted May 12, 2007 I don't understand you. What's the problem? Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-251619 Share on other sites More sharing options...
pgabriel Posted May 20, 2007 Author Share Posted May 20, 2007 chigley, this is the code: function rewritecategory($str) { $str = str_replace("::", "/", $str); $str = str_replace("&", "and", $str); $str = str_replace(" ", "-", $str); return $str; } $catary = file("category.txt"); foreach($catary as $line) { $newline = rewritecategory($line); $explode = explode("/", $newline); $category = $explode[0]; $sub = $explode[1]; if(!is_array($headings[$category])) { $headings[$category] = array(); } array_push($headings[$category], $sub); } The output: foreach($headings as $mainheading => $subheading) { echo "<h3><a href=\"$mainheading\">$mainheading</a></h3>\n"; foreach($headings[$mainheading] as $sub) { echo "<a href=\"$mainheading/$sub\">$sub</a>, "; } } Now the categories are looking like this: <a href="Audio-and-Multimedia/Audio-File-Recorders">Audio-File-Recorders</a> In href they look as i wanted. They are rewritten. The category anchor is: <a href="Audio-and-Multimedia/Audio-File-Recorders">Audio-File-Recorders</a> In category.txt file they look like Audio & Multimedia::Audio File Recorders Audio & Multimedia::Audio Encoders/Decoders In the anchor i would like to output something like this: <a href="Audio-and-Multimedia/Audio-File-Recorders">Audio File Recorders</a> or <a href="Audio-and-Multimedia/Audio-Encoders-and-Decoders">Audio Encoders/Decoders</a> Can you help me? Thanks Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-257516 Share on other sites More sharing options...
pgabriel Posted May 20, 2007 Author Share Posted May 20, 2007 chigley, can you help me with this? Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-257820 Share on other sites More sharing options...
pgabriel Posted May 22, 2007 Author Share Posted May 22, 2007 Finally i figured it out. Topic solved. Link to comment https://forums.phpfreaks.com/topic/51052-solved-categories-coding-help/#findComment-258994 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.