Hi

I have around 2500 pages that I need to link to various pages on my site. Each of these 2500 pages contains stock and share information, and I want to link them to pages according the the first letter of the company's name. So BP would be under B, Apple under A etc.

Is there anyway I can automatically link these pages to the correct page without typing each one out individually, and then echo something from the company's page to appear as the anchor text?

I am not sure I have explained this very well, but I can't think how to explain it better.

Any help is appreciated.

Thanks

Member Avatar for diafol

if these names are held in a db it's easy. if not, you probably need to do a file search (slow) and add names starting with a specific letter to your list.

$letter = "b"; //get this from a link, e.g. $letter = $_GET['letter'];
//check to see if letter is valid (a-z) e.g. with preg_match
if ($handle = opendir('.')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && !is_dir($file) && strtolower(substr($file,0,1)) == $letter ) {
			echo "<a href=\"$file\">$file</a><br />";
        }
    }
    closedir($handle);
}

You might find that the SPL functions are quicker.
OR you can use use glob, which looks a lot cleaner:

$letter = "c";
foreach (glob($letter . "*.php") as $filename) {
    echo "<a href=\"$filename\">$filename</a><br />";
}

if each page is namned appropriately, eg BP.html BHP.html and may be resident in subfolders b c d, this script will recurse,
try something like

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<style type="text/css">
ul.dirlist, ul.dirlist li { list-style-type:none; padding-left:1em; }
</style>
<title>Try this -recursively Almostbob</title>
</head><body><H3>Links</h3>
<?php
function get_Extension($ff){
$path_parts = pathinfo($ff);
if ($path_parts["extension"]) { $m_Extension = strtoupper($path_parts["extension"]); 
	return($m_Extension); }
else {return("unknown");}  }
function Check_type($ff){ $temp=get_Extension($ff);
 if(($temp=="HTML"))  return (true);
 else return (false);  }
 function recursedir($c = '.', $wcwd = false) {
if($wcwd === false){ $wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);}
 echo('<ul class="dirlist">');
 $d = opendir($c);
 while($f = readdir($d)) {
 if(strpos($f, '.') === 0) continue;
 $ff = $c . '/' . $f;
 if(Check_type($ff)){ echo '<li><a href="'.$ff.'">'.substr($f,0,strlen($f)-4).'</a>';}
 if(is_dir($ff)) {echo '<li>'.$f;
 recursedir($ff, $wcwd); }
 echo '</li>'; }
 echo('</ul>'); } 
 recursedir(); ?>
 </body>
</html>

you could always add a sort to give absolute alpha order
line 15 can be given or clauses to add multiple file types to return true, eg if ($temp =='HTML'||$temp == "HTM" || $temp == "php")

Member Avatar for diafol

Aha! AB - I see now. He wants ALL of them on one page. How silly of me.

Member Avatar for diafol

How's this?

$prev="";$output="";$navlist="<a name=\"navi\">COMPANIES: </a>"; //initialise vars
$list = range('a', 'z'); //array a to z
foreach ($list as $letter) {
	foreach (glob($letter . "*.php") as $filename) { //change for htm or html, whatever - BUT check **
		if($letter!==$prev && $prev!="")$output.="</ul>"; //used to close list block
		if($letter!==$prev){ //start new letter and list block
			$output.="<h3><a name=\"$letter\">" . strtoupper($letter) . "</a> <a href=\"#navi\">&uarr;</a></h3><ul>";
			$nav_r[] = $letter; //add only used letters to navigation
		}
		$output .= "<li><a href=\"$filename\">" . substr($filename,0,-4) . "</a></li>"; //** -4 = take off 4 characters from end of filename
		$prev=$letter;
	}
}
foreach($nav_r as $nav){
	$navlist .= "<a href=\"#$nav\">". strtoupper($nav) . "</a> "; //concatenate nav list
}
echo $navlist;
echo $output . "</ul>";

this might be slower than grabbing all the files in one go and placing them in an array and then sort. however that's easily done as well.

Hi, thank you so much for the replies.

The one I have found the most useful was the first one posted by ardav, (the latter two are a bit too complicated for me to know what I am supposed to do with them.) Is there anyway to adapt the first one so that along with the link it also provides the <title> of the page, or even better there is a string in the page

$data['name'];

If I could generate this that would be amazing.

Thanks for all your help so far

Member Avatar for diafol

do you mean the <title> tag of each page? An say the first paragraph? something like that?
I *think* you'd need to open every file with something like file_get_contents() and then extract the relevant bits from <title> and perhpas the meta description tag with a preg function. That would be REALLY SLOW and intensive.

To my mind this funcionality would lend itself to storing info in a DB.

file_id
file_name
file_title
file_description

Something like that, so you wouldn't have to scan or read any files at all. Indeed the title and description tags of each file would be 'blank', ie they'd retrieve the info from the DB as required. This would further lend itself to producing a templating system (simple php):

<title><?php echo $row['file_title'];?></title>

There are more sophisticated templating systems out there like RainTPL and Smarty.

If you go down this route, perhaps your best bet would be to create a function that reads the title and description tags and enter them into a DB. Then replace the content with code like the above.

Just a thought.

WRT best examples. AlmostBob's example and my last one are more sophisticated and complicated, I agree, but should be better. Try copying and pasting them into your page and see how they turn out.

Is there a way you can have an if statement to not have a certain page link show up that is in the same directory?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.