Can anyone help me add another folder destination to the code below.
for example Categories/test, keep in mind that I want to add not repplace the location below which is Categories/Game

<?php
if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('Categories/Game/page_'.$page.'.php'))
echo file_get_contents('Categories/Game/page_'.$page.'.php');
else echo 'There is no such page!';
?>

Recommended Answers

All 12 Replies

Member Avatar for diafol
$dests = array("Game", "Test", "Another"); //add folders of your choice
$flag = 0;

if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
foreach($dests as $dest){
 if(file_exists("Categories/$dest/page_{$page}.php")){
   echo file_get_contents("Categories/$dest/page_{$page}.php");
   $flag = 1;
 }
}
if($flag == 0)echo 'There is no such page!';

Were you looking for something like that, I didn't quite get you.

Thank you this works, also its there any way for the last line

if($flag == 0)echo 'There is no such page!';

be redirected to a 404 page?

Also is there any way to do the same the value PAGE I have try the following

$name = array("games", "page", "Another");

but it did not work

Member Avatar for diafol

Well that's from the $_POST? That should be constant throughout the loop.

$dests = array("Game", "Test", "Another"); //add folders of your choice
$flag = 0;
 
if(!isset($_POST['page'])) die("0");
$page = (int)$_POST['page'];
foreach($dests as $dest){
 if(file_exists("Categories/$dest/page_{$page}.php")){
   echo file_get_contents("Categories/$dest/page_{$page}.php");
   $flag = 1;
 }
}
if($flag == 0)header("Location: http://www.example.com/404.php");

If it doesn't work, check to see if the "Categories/$dest/page_{$page}.php" bit is giving you what you think. You can echo out this bit instead of echoing the contents.

What Im trying to do is making so that I can change the value PAGE to something like HELLO without a number, I have done part of this by removing

(int)

in line 5, but I have fail to change the name of PAGE, any idea on how to solve this.

page_{$page}.php

Ex: HELLO_{$page}.php

Again Im trying to do the same thing with an array if possible, just like the folder lcoation.

Member Avatar for diafol
if(file_exists("Categories/$dest/{$page}.php")){
   echo file_get_contents("Categories/$dest/{$page}.php");
   $flag = 1;
 }

Like that? I'm a bit confused.

Oh, maybe like this:

$dests = array("Game", "Test", "Another"); //add folders of your choice
$pgs = array("page", "hello", "tudalen"); //add page prefixes of your choice
$flag = 0;
 
if(!isset($_POST['page'])) die("0");
$page = (int)$_POST['page'];
foreach($dests as $dest){
  foreach($pgs as $pg){
    if(file_exists("Categories/$dest/{$pg}_{$page}.php")){
      echo file_get_contents("Categories/$dest/{$pg}_{$page}.php");
      $flag = 1;
    }
  }
}
if($flag == 0)header("Location: http://www.example.com/404.php");

I have to say, I've never seen anything like that before. Say you have hello_1.php and page_1.php, they will both be echoed to the page. If you want to stop with the first file, you can place a 'break' into the loop on success.

I don't know your folder structure, but you may even be better off placing all the filenames into an array and checking those? I really don't know which would be quicker. SPL has some nice file functions too. Maybe Google that.

I found the issue but on a js file that Im using take a look

function loadPage(url)
{
	url=url.replace('#HELLO','');
	
	$('#loading').css('visibility','visible');
	
	$.ajax({
		type: "POST",
		url: "load_page.php",
		data: 'page='+url,
		dataType: "php",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				$('#pageContent').html(msg);
				$('#loading').css('visibility','hidden');
			}
		}
		
	});
	

}

Check line 3, do you know how can I set an array so its not HELLO by default.

I found a solution, what I did its copy and paste line 3 like this

url=url.replace('#Page','');
url=url.replace('#HELLO','');

Now I am able to use both instead of one, do you have a better solution?

Dont mind my last post what I fix did work but not completely, the problem is that for example

#page_1 and #Hello_1 are the same pages when been called I need it to be indidual, taje a look at the entire js code

var default_content="";

$(document).ready(function(){
	
	checkURL();
	$('ul li a').click(function (e){

			checkURL(this.hash);

	});
	
	//filling in the default content
	default_content = $('#pageContent').html();
	
	
	setInterval("checkURL()",250);
	
});

var lasturl="";

function checkURL(hash)
{
	if(!hash) hash=window.location.hash;
	
	if(hash != lasturl)
	{
		lasturl=hash;

		
		if(hash=="")
		$('#pageContent').html(default_content);
		
		else
		loadPage(hash);
	}
}


function loadPage(url)
{
	
	url=url.replace('#page','');
	url=url.replace('#games','');
	
	$('#loading').css('visibility','visible');
	
	$.ajax({
		type: "POST",
		url: "load_page.php",
		data: 'page='+url,
		dataType: "php",
		success: function(msg){
			
			if(parseInt(msg)!=0)
			{
				$('#pageContent').html(msg);
				$('#loading').css('visibility','hidden');
			}
		}
		
	});
	

}

The problem is in line 43 and 44

Member Avatar for diafol

> Now I am able to use both instead of one, do you have a better solution?

Listen, I gave you what I thought was a solution and should do what you want. You didn't give me any feedback as to how it worked or didn't.

Now you tell me you're using javascript and you're stuck with that.

No offence, but post to the javascript forum. I'm gone. Good luck.

I been looking at the code and I have determine that its indeed a PHP issue, the problem is the following if I have a page name page1 and another called game1 when one of them are called instead of just loading 1 it actually loads both when you click on the link wwww.test.com/page1 or wwww.test.com/game1 so when you click on the link both contents are load in the same page. please take a look

<?php

$dests = array(""); //add folders of your choice
$pgs = array(""); //add page prefixes of your choice
$flag = 0;
 
if(!isset($_POST['page'])) die("0");
$page = (int)$_POST['page'];
foreach($dests as $dest){
  foreach($pgs as $pg){
    if(file_exists("Categories/$dest/{$pg}_{$page}.php")){
      echo file_get_contents("Categories/$dest/{$pg}_{$page}.php");
      $flag = 1;
    }
  }
}
if($flag == 0)header("Location: http://www.example.com/404.php");
?>

If you have skype let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.