Jump to content

Trouble looping through an array, and returning variable to load dynamic content


TGWSE_GY

Recommended Posts

Ok so here is a little preliminary information about what I am attempting.

 

I have grown tired of writing a custom content conditions file to process what page/file to load based off a link clicked. So I had the idea of having an array that has the page id(key) filename without the .php extension(value).

 

What is happening in my custom function is reading through the array but everytime it apparently goes through the loop and doesn't and falls through all itterations and then setting the default page(home.php)

 

Below is my php file that loads the array and the custom function to grab the page and is suppose to return the file name and location to load in the content div in the index.php file.

<?php

//We will build the $pgid array first
$pgID = array(0=>"home", 1=>"about", 2=>"mixology", 3=>"blogs", 4=>"press", 5=>"contact");

//FUNCTIONS//
function grabpage(){
//Test if ID is present in URL
if(isset($_GET['ID'])){
	//TRUE then store value of ID from URL in $pg
	$pg = $_GET['ID'];
	//Loop through the $pgID array until a match is found
	foreach($pgID as $key => $value){
		//Test to see if $pg is NOT equal to current array item key
		if($pg == $key){
		//if $pg is equal to $key we then
			//set $getthispage to "content/directory/" concatinate $value concatinate ".file_extension"
			$getthispage = "includes/content/" . $value . ".php";
			return $getthispage;
			break;	
		//if the above is not true then we test to see if $pg is not equal to $key
		}elseif($pg != $key){
			//if above test IF is true then we continue to the next itteration in the loop
			continue;
		}
	}
}else{
	$getthispage = "includes/content/home.php";
	return $getthispage;
}
}

?>

 

here is the index.php code

<?php
error_reporting(2047);
ini_set("display_errors",1);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="simple.css" />
</head>

<body>
	<div id="container">
		<div id="header">
       	    	<center>

       	    	  <img src="images/logo.png" width="800" height="150" alt="DeeJay Octane" />
       	    	</center>
            </div>
            <div id="nav">
            <div>
            </div>
          	<p>
            <a id="navi_link" href="../index.php?section=1">HOME</a><a id="navi_link" href="../index.php?section=2">ABOUT</a><a id="navi_link" href="../index.php?section=3">MIXOLOGY</a><a id="navi_link" href="../index.php?section=4">BLOGS</a><a id="navi_link" href="../index.php?section=5">PRESS</a><a id="navi_link" href="../index.php?section=6">CONTACT</a>
            </p>
            </div>
            
            <div id="content">
			<?php include('includes/config/config.master.php'); ?>
            	<?php $getit = grabpage(); include($getit); ?>
          </div>

            <div id="footer">
            	<center>
            	  <font style="background-color:#000;">Designed & Developed By <a href="http://www.visualrealityink.com">Visual Reality ink.</a></font>
            	</center>
          </div>
	</div>
</body>
</html>

www.deejayoctane.com

Thanks in advance guys

:confused:

Link to comment
Share on other sites

Either that or passed in as an argument. I didn't really look at the rest of the code, but there should be no need for any loop either.

 

function grabpage($id, $arr) {
  if (in_array($id, $arr)) {
    return "includes/content/{$arr[$id]}.php";
  } else {
    return "includes/content/home.php";
  }
}

 

Then....

 

<?php
  $pgID = array("home", "about", "mixology", "blogs", "press", "contact");
  include(grabpage($_GET['ID'], $pgID));
?>

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.