barrycorrigan Posted June 14, 2011 Share Posted June 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/ Share on other sites More sharing options...
fugix Posted June 14, 2011 Share Posted June 14, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229490 Share on other sites More sharing options...
gary80 Posted June 14, 2011 Share Posted June 14, 2011 couldnt you use a switch? <?php switch($currentPage) { case '250pp10_pump': echo '_pdfs/250pp10_pump.pdf'; case '2': echo '_pdfs/2.pdf.pdf'; #etc... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229493 Share on other sites More sharing options...
barrycorrigan Posted June 14, 2011 Author Share Posted June 14, 2011 Hi fugix, That is correct Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229494 Share on other sites More sharing options...
barrycorrigan Posted June 14, 2011 Author Share Posted June 14, 2011 Hi gary80 Cheers for that I'll get that code in Barry Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229496 Share on other sites More sharing options...
fugix Posted June 14, 2011 Share Posted June 14, 2011 an array would not be ideal here, you could also use if else statements, however in your case i believe that a switch statement would be the most efficient way as gary80 states Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229502 Share on other sites More sharing options...
barrycorrigan Posted June 14, 2011 Author Share Posted June 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229507 Share on other sites More sharing options...
fugix Posted June 14, 2011 Share Posted June 14, 2011 first, you have your first case backwards, second, for proper switch syntax look here Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229511 Share on other sites More sharing options...
barrycorrigan Posted June 14, 2011 Author Share Posted June 14, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229522 Share on other sites More sharing options...
gary80 Posted June 14, 2011 Share Posted June 14, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229524 Share on other sites More sharing options...
AbraCadaver Posted June 14, 2011 Share Posted June 14, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229525 Share on other sites More sharing options...
barrycorrigan Posted June 14, 2011 Author Share Posted June 14, 2011 Hi gary80 That worked great but the only this is that it displays all the PDF links in every page. How do I get it were it only displays the one relevant to that page Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229552 Share on other sites More sharing options...
gary80 Posted June 14, 2011 Share Posted June 14, 2011 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229555 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2011 Share Posted June 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229560 Share on other sites More sharing options...
barrycorrigan Posted June 15, 2011 Author Share Posted June 15, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229931 Share on other sites More sharing options...
PFMaBiSmAd Posted June 15, 2011 Share Posted June 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239331-an-array-of-links/#findComment-1229950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.