Jump to content

How do you make dynamic pages from array variables?


bloodgoat

Recommended Posts

What I mean is... Say I have an array.

<?php
/*					DATE									URL			CONTENT */
$rnews	= array("Tuesday, March 17, 11:46pm",	"09-03",	$contents[2]);
?>

In my array are the date, the URL to the archive where all news for the current timeframe are compiled, and the contents of the news post.

Instead of going to the archive to view it, how can I use functions (or whatever else) to just pull values from the array, and embed them into a page dynamically made in PHP, without actually making a separate HTML file?

 

ie/ instead of making news1.html and going index.php?page=news1, I just want to keep everything in the array and still be able to go index.php?page=news1. I realize I could do this by compounding a huge amount of if...else statements

<?php
if($page == "news1"){
echo "Date:".$rnews[0]."<br>\n";
echo $contents[2];
}
?>

But that isn't dynamic enough as it requires me to make a new if...else statement for every new page that I create. I want it to be more dynamic than that. Like if I create a new array with new variables, without making an if...else statement like above, I can just manipulate the variables through the URL (possibly, I just don't know any other ways) and the output would display the variables accordingly.

 

I'm positive a friend of mine did this for a great content management system he designed years ago. The difference is, he was using SQL whereas I am not (though I suppose I could).

 

Or, if what I'm describing isn't entirely possible, what would be the next best method?

 

As it stands, I have amassed a huge list of arrays full of information that I'm pulling for various things and have it centralized for editing through my browser (this has to do with my thread about fwriting through input fields yesterday), but everything is amassed on one page. I'd like to split it up into individual pages for each array I'm working with, without having to make an actual file for them all.

I think you may need to explain the structure of your array a little better for this, maybe something like this:

$rnews=array('news1' => array('dt'=>"Tuesday, March 17, 11:46pm",   "09-03",   'content'=>$contents[2]),
             'news2' => array('dt'=>"Tuesday, March 17, 11:46pm",   "09-03",   'content'=>$contents[2]),
             'news3' => array('dt'=>"Tuesday, March 17, 11:46pm",   "09-03",   'content'=>$contents[2]));
foreach ($rnews as $key => $val) {
  if ($page=$key) {
    echo 'Date: '.$val['dt'].'<br />';
    echo $val['content'];
  }
}

Or something like that but I'd be more inclined to store news in a database instead and pull it out with a query.

Archived

This topic is now archived and is closed to further replies.

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