Jump to content

An Array of Links


barrycorrigan

Recommended Posts

Hi Everyone,

 

Firstly I'm new to PHP, I'm stuck on this small task. I have a list of PDFs for particular products. I want users to be able to download a PDF specification of that particular product. So instead of having a link in each page. I was going to have an include file and have one link with some sort of an array to determine if on 'this page' download 'this PDF'. but I don't no where to start: I came up with this code but I'm not sure how to get it into an array.

 

<?php if ($currentPage == '250pp10_pump') {echo '_pdfs/250pp10_pump.pdf';} ?>

 

Any help would be great

 

Thanks

 

Barry

Link to comment
Share on other sites

let me make sure i understand you correctly. You want to have a include file that contains all of your pdf files, and you want to create a way for the script to determine which link to display based on which page you are on?

Link to comment
Share on other sites

Ok so I tried this:

 


<div id="download"><a href="<?php echo $currentPage; ?>"><span>Download Specification</span></a></div><!-- Download Specification -->

	<?php 
		switch($currentPage)
		{
				case '_pdfs/spp_2_inch_pump.pdf': echo 'diesel_driven_high_head_high_pressure_pumps.php';
				case 'spp_3_inch_pump.php': echo '_pdfs/spp_3_inch_pump.pdf';
				case '100pp43_pump.php': echo '_pdfs/100pp43_pump.pdf';
				case 'spp_4_inch_pump.php': echo '_pdfs/spp_4_inch_pump.pdf';
				case '100pp44_pioneer_pump.php': echo '_pdfs/100pp44_pioneer_pump_100pp44.pdf';
				case 'spp_6_inch_pump.php': echo '_pdfs/spp_6_inch_pump.pdf';
				case '150pp66_pioneer_pump.php': echo '_pdfs/150pp66_pioneer_pump_150pp66.pdf';
				case '250pp10_pump.php': echo '_pdfs/250pp10_pump.pdf';
		}
	?>

 

But maybe I have done this wrong because it's picking up the page not the PDF.

Link to comment
Share on other sites

This is what I have now and still no joy :-(

 

<div id="download"><a href="<?php echo $currentPage; ?>"><span>Download Specification</span></a></div><!-- Download Specification -->

	<?php
		switch($currentPage)
		{
				case 'diesel_driven_high_head_high_pressure_pumps': 
					echo '_pdfs/spp_2_inch_pump.pdf';
					break;

				case 'spp_3_inch_pump': 
					echo '_pdfs/spp_3_inch_pump.pdf';
					break;

				case '100pp43_pump':
					echo '_pdfs/100pp43_pump.pdf';
					break;

				case 'spp_4_inch_pump':
					echo '_pdfs/spp_4_inch_pump.pdf';
					break;

				case '100pp44_pioneer_pump':
					echo '_pdfs/100pp44_pioneer_pump_100pp44.pdf';
					break;

				case 'spp_6_inch_pump':
					echo '_pdfs/spp_6_inch_pump.pdf';
					break;

				case '150pp66_pioneer_pump':
					echo '_pdfs/150pp66_pioneer_pump_150pp66.pdf';
					break;

				case '250pp10_pump':
					echo '_pdfs/250pp10_pump.pdf';
					break;
		}
	?>

Link to comment
Share on other sites

sorry i forgot the breaks earlier, how about

 

<style>
A {
color:#000;
text-decoration:none;
}

A:Hover {
color:#F00;
}
</style>
<?php 


function links($currentPage)
{
switch($currentPage)
{
	case '_pdfs/spp_2_inch_pump.pdf':  
		return  'diesel_driven_high_head_high_pressure_pumps.php';

	case 'spp_3_inch_pump.php':  
		return  '_pdfs/spp_3_inch_pump.pdf';

	case '100pp43_pump.php': 
		return '_pdfs/100pp43_pump.pdf';

	case 'spp_4_inch_pump.php': 
		$value = '_pdfs/spp_4_inch_pump.pdf';


	case '100pp44_pioneer_pump.php': 
		return '_pdfs/100pp44_pioneer_pump_100pp44.pdf';


	case 'spp_6_inch_pump.php': 
		return '_pdfs/spp_6_inch_pump.pdf';


	case '150pp66_pioneer_pump.php': 
		return  '_pdfs/150pp66_pioneer_pump_150pp66.pdf';


	case '250pp10_pump.php': 
		return  '_pdfs/250pp10_pump.pdf';
}

return $value;
}



$currentPages = array(
				  '_pdfs/spp_2_inch_pump.pdf',
				  'spp_3_inch_pump.php',
				  '100pp43_pump.php',
				  'spp_4_inch_pump.php',
				  '100pp44_pioneer_pump.php',
				  'spp_6_inch_pump.php',
				  '150pp66_pioneer_pump.php',
				  '250pp10_pump.php'
				  );
?>

<div id="download">
<?php foreach($currentPages as $currentPage) { ?>
    	<a href="<?php echo links($currentPage); ?>">Download (<?php echo links($currentPage); ?>)</a>
        <br />
	<?php } ?>
    
</div>

Link to comment
Share on other sites

Well, you are missing the .php in the file names in the cases number 1.  Number 2 you need to assign $currentPage in every page.  I would define current page once in your include like this:

 

$currentPage = basename($_SERVER['SCRIPT_NAME']);

Also, if you name your pages and pdfs the same then you can forgo the switch and use something like this:

 

$pdfName = '_pdfs/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME) . '.pdf';

Link to comment
Share on other sites


<?php $currentPage = '_pdfs/spp_2_inch_pump.pdf'; ?>

<div id="download">
    	<a href="<?php echo links($currentPage); ?>">Download (<?php echo links($currentPage); ?>)</a>
</div>

 

just change the value of $currentPage, if it matches anything in the switch a value will be returned

Link to comment
Share on other sites

You should make the page basename and the pdf basename the same and just do what AbraCadaver posted.

 

You should not write hard-coded logic that relates the page to the pdf, because you will need to go in and edit your code every time you add a page/pdf file. Code should be general purpose so that it operates on any amount of data without needing to edit it just because you add one more piece of data.

 

Edit: If you do have a case where you need to lookup values, you would use an array to hold the key/value pairs so that you only need to add an entry to the array, instead of needing to find and edit the logic in your program every time you add a value.

 

Edit2: You should actually only have ONE page that uses a get parameter on the end of the URL that indicates which product the page displays.

 

Link to comment
Share on other sites

Hi AbraCadaver,

 

I Tried this:

 

<?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?>
<div id="download">
<a href="<?php $pdfName = '_pdfs/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME) . '.pdf'; ?>"><span>Download Specification </span></a>
</div>

 

But it's displaying each individual page not the PDF.

 

Any Ideas

Link to comment
Share on other sites

The post that AbraCadaver made contained two different pieces of information. That first was to get the current page name dynamically instead of hard-coding it in each page.

 

The second was to eliminate all that code and dynamically produce the pdf name from the basename of the page in a variable named $pdfName. You could then echo $pdfName where you needed it. Since you are directly outputting the value that the line of code he supplied is producing, you would just echo it directly -

 

<div id="download">
<a href="<?php echo '_pdfs/' . pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME) . '.pdf'; ?>"><span>Download Specification </span></a>
</div>

 

And I will reiterate something I posted above, once I saw the forest you were creating. You should not be producing individual pages, one for each product. You will end up spending a huge amount of time just keeping track of and maintaining all the pages and keeping your menu up to date every time you add, delete, or change anything.

 

Ultimately, you should have a product database and one .php page that dynamically builds your menu of product categories/products, displays the information about the currently selected product (using a get parameter on the end of the url to determine what the currently selected product is), and has a download link for the currently selected product. You should let your php code do the work for you instead of you manually making pages and manually making your product navigation menu.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.