Jump to content

[SOLVED] Categories coding help


pgabriel

Recommended Posts

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

<?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>

<?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?

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]

<?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?

<?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 :)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.