I have a question regarding how i can make my my navigation display the "correct pages".

I am trying to build a CMS, with php and Mysql.

So far I only had a table, from the DB, called "pages". I made the following code, to make this work - And made the navigation through that table:

<?php //******************PAGE DATA START SHERE*****************************************************************************
// Determine which page ID is set----------------------------------- 
if (!isset($_GET['pid'])) {
	$pageid = '1';
} else {
	$pageid = preg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page------------------------------------------
$sqlCommand = "SELECT pagetitle, pagebody FROM pages WHERE id='$pageid' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $pagetitle = $row['pagetitle'];
	$body = $row["pagebody"];
} 
mysqli_free_result($query); 
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------

// Query the module data for display--------------------------------------------------- 
$sqlCommand = "SELECT modulebody FROM modules WHERE showing='1' AND name='footer' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $footer = $row["modulebody"];
} 
mysqli_free_result($query); 
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------

// BUILD MAIN NAVIGATION AND DISPLAY DATA FROM THIS-------------
$sqlCommand = "SELECT id, linklabel, pos FROM pages WHERE showing='1' ORDER BY pos ASC"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) { 
    $pid = $row["id"];
    $linklabel = $row["linklabel"];
	$position = $row['pos'];
	
	$menuDisplay .= '<li><a href="index.php?pid=' . $pid . '">' . $linklabel . '' . $position . '</a></li>';
	
} 
mysqli_free_result($query); 

//******************PAGE DATA ENDS HERE*******************************************************************************
?>

This works fine!

THEN I have created another table, this is called "SUBJECTS". My intention is to make this work as a "parent", to the PAGES table. So subjects is the global navigation, and pages is showing under whatever subject it has been set to, as a local navigation.

I did the exact same when I pulled out the SUBJECTS from the database:

<?php //******************SUBJECT DATA STARTS HERE*****************************************************************************
if (!isset($_GET['sid'])) {
	$subjectid = '1';
} else {
	$subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
}
// Query the body section for the proper subject------------------------------------------
$sqlCommand = "SELECT subjecttitle, subjectbody FROM subjects WHERE id='$subjectid' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $subjecttitle = $row['subjecttitle'];
	$body = $row["subjectbody"];
} 
mysqli_free_result($query); 
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------


// Build SUBJECT navigation and gather SUBJECT data here--------------------------------
$sqlCommand = "SELECT id, linklabel, pos FROM subjects WHERE showing='1' ORDER BY pos ASC"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 

$SubjectMenuDisplay = '';
while ($row = mysqli_fetch_array($query)) { 
    $sid = $row["id"];
    $linklabel = $row["linklabel"];
	$position = $row['pos'];
	
	$SubjectMenuDisplay .= '<li><a href="index.php?sid=' . $sid . '">' . $linklabel . '' . $position . '</a></li>';
	
} 
mysqli_free_result($query); 

//******************SUBJECT DATA ENDS HERE*******************************************************************************
?>

I have put SUBJECTS in a horizontal global menu, AND the SUBJECT links are the only ones that are working, and can display $pagebody etc etc.

The PAGES are in a left vertical menu, and not "working" when clicked on, after I integrated the subjects.
(Also I need to make the SUBJECT with id=1 be the index page).

//THIS CODE FOR PAGES
if (!isset($_GET['sid'])) {
	$subjectid = '1';
} else {
	$subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
}

//THIS CODE FOR SUBJECTS
if (!isset($_GET['sid'])) {
	$subjectid = '1';
} else {
	$subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
}

They are completely alike, and not working. It must be some if else statements, checking wheter one or the other is set or not?

Help highly appreciated :-)

If i explained badly, please let me know, and I will try to do it better.

Klemme

CORRECTION TO THE LAST BIT OF CODE:

The PAGES are in a left vertical menu, and not "working" when clicked on, after I integrated the subjects.
(Also I need to make the SUBJECT with id=1 be the index page).

PHP Syntax (Toggle Plain Text)

    //THIS CODE FOR PAGES
    if (!isset($_GET['pid'])) {
    $pageid = '1';
    } else {
    $pageid = preg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
    }
     
    //THIS CODE FOR SUBJECTS
    if (!isset($_GET['sid'])) {
    $subjectid = '1';
    } else {
    $subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
    }

They are completely alike, and not working. It must be some if else statements, checking wheter one or the other is set or not?

Help highly appreciated :-)

If i explained badly, please let me know, and I will try to do it better.

Klemme

Talking to myself again.. Anyways it works like this, so I got it sorted so far :-)

if (isset($_GET['sid'])) {
	$subjectid = ($_GET['sid']);
	$pageid = NULL;
	$subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
} elseif (isset($_GET['pid'])){
	$subjectid = NULL;
	$pageid = ($_GET['pid']);
	$pageid = preg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}	else {
	$subjectid = NULL;
	$pageid = NULL;
}

Hey again!

Except now I dont have an Index page....

Can someone see how I get the last part of this, display SUBJECTID = 1, From the table SUBJECTS, if not a page or a subject has been chosen, like when you first enter the site, and havent clicked any links yet?

if (isset($_GET['sid'])) {
	$subjectid = ($_GET['sid']);
	$pageid = NULL;
	$subjectid = preg_replace("[^0-9]", "", $_GET['sid']); // filter everything but numbers for security
} elseif (isset($_GET['pid'])){
	$subjectid = NULL;
	$pageid = ($_GET['pid']);
	$pageid = preg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}	else {
	$subjectid = NULL;
	$pageid = NULL;
}

When I enter the index page, there is no information displayed in the body of the page because of this.

If a link has been set, I get the body informaition like this:

<?php 
	if (isset($_GET['pid'])){	
	echo $pagebody; 
	} elseif (isset($_GET['sid'])){
	echo $subjectbody;
	} else echo $subjectid;
	?>

Klemme

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.