smeguru Posted December 27, 2008 Share Posted December 27, 2008 Firstly sorry for my lack of skills with PHP but hoping you guys can help me out whilst I am learning. I have inherited some old code like this which is on every single page of a website. <?php if (isset($pagesetting['leftside']['section1'])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section1'].".inc.php"); } if (isset($pagesetting['leftside']['section2'])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section2'].".inc.php"); } if (isset($pagesetting['leftside']['section3'])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section3'].".inc.php"); } if (isset($pagesetting['leftside']['section4'])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section4'].".inc.php"); } if (isset($pagesetting['leftside']['section5'])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section5'].".inc.php"); } ?> Whilst this works it seems to be very much open to problems. I am looking to see if it is possible to change the code to to a loop that would just keep repeating until the $pagesetting['leftside']['sectionNUM'] is not set. This is becuase some pages have 3 sections some have 10. So currently every page has different code I want to use a template that can just have one loop if possible and allow for all situations. Ideally I would also like to check to see if the inculde file exsits before it is called. Any helps or tips would be appreciated. Thanks Stephen Quote Link to comment https://forums.phpfreaks.com/topic/138552-change-this-old-code-to-a-loophelp-needed/ Share on other sites More sharing options...
kenrbnsn Posted December 27, 2008 Share Posted December 27, 2008 Try this: <?php foreach ($pagesetting['leftside'] as $indx => $section) { if (file_exists($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection." . $section . ".inc.php")) include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection." . $section . ".inc.php"); } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/138552-change-this-old-code-to-a-loophelp-needed/#findComment-724436 Share on other sites More sharing options...
omarh2005 Posted December 27, 2008 Share Posted December 27, 2008 Hi smeguru Try this <? for($i=1;$i<=5;$i++){ if (isset($pagesetting['leftside']['section'.$i])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section'.$i].".inc.php"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/138552-change-this-old-code-to-a-loophelp-needed/#findComment-724437 Share on other sites More sharing options...
smeguru Posted December 28, 2008 Author Share Posted December 28, 2008 Thanks for the help, I can get both bit of code working. But wondered if i could ask for a little bit more guidence. The first code works great, but does not allow for other items not called sectionx in the array. <?php foreach ($pagesetting['leftside'] as $indx => $section) { if (file_exists($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection." . $section . ".inc.php")) include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection." . $section . ".inc.php"); } ?> The second code again works but would test upto 5 ISSET queries even if 3 4 & 5 are not set. <? for($i=1;$i<=5;$i++){ if (isset($pagesetting['leftside']['section'.$i])) { include ($_SERVER['DOCUMENT_ROOT']."/shared-resources/sidesection.".$pagesetting['leftside']['section'.$i].".inc.php"); } } ?> Is the a method do making the code only look for ['section'.$i] from the ini file. For example my ini file currently has. [leftside] show=yes section1=search section2=tellafriend section3=reviewus section4=linkback So i only want it to loop though $pagesetting['leftside']['section1], $pagesetting['leftside']['section2], $pagesetting['leftside']['section3], $pagesetting['leftside']['section4] Hope that makes sense. Thanks again. Stephen Quote Link to comment https://forums.phpfreaks.com/topic/138552-change-this-old-code-to-a-loophelp-needed/#findComment-724787 Share on other sites More sharing options...
wildteen88 Posted December 28, 2008 Share Posted December 28, 2008 Kens original code should allow this. If your ini is set like this: [leftside] show=yes section1=search section2=tellafriend section3=reviewus section4=linkback Then kens code will be including/requesting for the following files: $_SERVER['DOCUMENT_ROOT']/shared-resources/sidesection.search.inc.php $_SERVER['DOCUMENT_ROOT']/shared-resources/sidesection.tellafriend.inc.php $_SERVER['DOCUMENT_ROOT']/shared-resources/sidesection.reviewus.inc.php $_SERVER['DOCUMENT_ROOT']/shared-resources/sidesection.linkback.inc.php In order for the code to work correctly ensure the path to the files are correct. Quote Link to comment https://forums.phpfreaks.com/topic/138552-change-this-old-code-to-a-loophelp-needed/#findComment-724814 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.