Jump to content

Recommended Posts

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

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

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

 

 

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.

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.