Jump to content

Dynamic menu coding problem


Recommended Posts

Hello W3S!

 

It's been a while since i've been online here... sorry about that :P

 

Anyways. I have some trouble with a dynamic menu i'm trying to make with MySQLI... not sure if that is the problem anyhow...

 

Here is the code as a start, i will explain under the code below what i'm trying to do:

// File that we are on (viewing / watching)$tab = pathinfo( $_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME );$menu_res = query("SELECT * FROM menu WHERE menu_file_url = ".$tab);$menu_row = mysqli_fetch_row($menu_res);if( $tab == $menu_row['menu_file_url'] || $menu_row['menu_accessible'] == "no" )	stderr("Page Error", "We are currently working on this page! Go to another page to keep browsing! Thanks for your patience! :)");if(isset($CURUSER)){	$menu_while_res = query("		SELECT			*		FROM			menu		WHERE			menu_accessible = 'yes'			AND			menu_view = 'user' OR menu_view = 'both'			ORDER BY			menu_order_id ASC");}else{	$menu_while_res = query("		SELECT			*		FROM			menu		WHERE			menu_accessible = 'yes'			AND			menu_view = 'guest' OR menu_view = 'both'			ORDER BY			menu_order_id ASC");}$HTMLOUT .= "<ul class='nav_first'>";	while ($menu_while_row = mysqli_fetch_array($menu_while_res, MYSQLI_ASSOC))	{		// Menu Items Loaded Here		$tabarray = array(			$menu_while_row['menu_array_id_name'] => "<li><a href='".$menu_while_row['menu_file_url']."'>".$menu_while_row['menu_name']."</a></li>",		);				// K = Key		// V = Value		foreach($tabarray as $k => $v)		{			if( $tab == $k )				$HTMLOUT .= str_replace("<li>", "<li class='nav_active'>", $v);			else				$HTMLOUT .= $v;		}				// Unset Menu For re-load again		unset($tabarray);	}$HTMLOUT .= "</ul>";

Currently i'm trying to make a dynamic menu with MySQLI! It's working perfectly... but when i tried to "expand" the project a bit longer and try to make a dynamic menu with errors on pages if the users are not allowed to view a specific file, then i get nothing...

 

What i'm trying to do is to controle in the database with "Enum" as my DB setup that is "no" is has been set on one of the menu items (menu_accessible).. then the item will NOT show on the menu! AND if the user still tries to enter that specific page by URL, he will then get an error message saying that we are working on the website page...

 

The code i'm trying to insert into this project is this little peace of code here:

// File that we are on (viewing / watching)$tab = pathinfo( $_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME );$menu_res = query("SELECT * FROM menu WHERE menu_file_url = ".$tab);$menu_row = mysqli_fetch_row($menu_res);if( $tab == $menu_row['menu_file_url'] || $menu_row['menu_accessible'] == "no" )	stderr("Page Error", "We are currently working on this page! Go to another page to keep browsing! Thanks for your patience! :)");

However, i get no respond on the code! Even when i have checked if the "$tab" variable is real and related to the name inside the DB (which it is!)...

 

so if possible, can anyone help and tell me what i'm doing wrong here Oo?

Thanks alot by the way! And sorry for the long goodbye hehe :P...studies and all, killing me! -.-'

 

Anyways, hope some answers or good tips... really need this one :P

 

Thanks in advance! :D

 

Mr rootKID ;)

Link to post
Share on other sites

You need to be using prepared statements, your query is not formed correctly and you are apparently not checking for errors inside your query function. Look up prepared statements with mysqli and start using those to pass data to your queries.

Link to post
Share on other sites

Thanks for the answer!

 

However, i have a little bit of a different setup.

 

Here is my connection function:

<?php// DataBase Connection Functionfunction db_connection(){	# Connection Information	//-------------------------------	$DBCONN['host']       = 'localhost';	$DBCONN['user']       = 'root';	$DBCONN['password']   = 'daniel1';	$DBCONN['database']   = 'nxtait'; // ( NeXT Artificial Intelligence Tracker )	//$DBCONN['port']     = '80'; // Not used	$DBCONN['charset'] = "utf8";	//-------------------------------		$mysqli = new mysqli (		$DBCONN['host'], // Host		$DBCONN['user'], // Username		$DBCONN['password'], // Password		$DBCONN['database'] // Database	);		// Check if there was an error	if ( mysqli_connect_errno() )	{		echo "<b><u>"."An error happened when we tried to connect the database!"."</u></b>";		echo "<br />";		echo mysqli_connect_error();		exit();	}		// change character set to utf8	$mysqli -> set_charset( $DBCONN['charset'] );		// (query_connection) MAKER & Switch Module (BETA VERSION!)	// ================================================================	if ( !@( $GLOBALS["query_connection"] = $mysqli ) )	{		switch (((is_object($GLOBALS["query_connection"])) ? mysqli_errno($GLOBALS["query_connection"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)))		{			case 1040:			case 2002:				if ($_SERVER['REQUEST_METHOD'] == "GET")				{					die("						<html>							<head>								<meta http-equiv='refresh' content="5 $_SERVER[REQUEST_URI]">							</head>							<body>								<table border='0' width='100%' height='100%'>									<tr>										<td>											<h3 align='center'>The server load is very high at the moment. Retrying, please wait...</h3>										</td>									</tr>								</table>							</body>						</html>					");				}				else					die("Too many users. Please press the Refresh button in your browser to retry or try again later.");			default:				die("				[".((is_object($GLOBALS["query_connection"])) ? mysqli_errno($GLOBALS["query_connection"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false))."]				<br />				dbconn: mysql_connect: ".((is_object($GLOBALS["query_connection"])) ? mysqli_error($GLOBALS["query_connection"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));		}	}	// ================================================================		////////////////////////////////////////////////////////////	// Loaders / Updaters	////////////////////////////////////////////////////////////	// Connection Type:	// MySQLI = Secured MySQLI Database Connection	////////////////////////////////////////////////////////////		// $connection = $mysqli; // Not used yet		// LOADERS GO FROM HERE AND BELOW!}?>

I'm still working on it to be a beta, but whenever a connection is being made to the DB i use the global (query_connection) in this function:

function query($query){	$result = mysqli_query($GLOBALS["query_connection"], $query);	return $result;}

to connent the query.. but like i said, still working on it. So now that i have it like that, any suggestions on how i could be writing my "prepared statement" down Oo?

 

Thanks for answers/suggestions if possible you have any! x)

 

-> root

Link to post
Share on other sites

Prepared statements need to be a priority. What you're doing doesn't help anything, it doesn't add any functionality or make anything any easier. Just create a MySQLi object and use that, what's the point of the query function? What's the point of this:

	if ( !@( $GLOBALS["query_connection"] = $mysqli ) )	{		switch (((is_object($GLOBALS["query_connection"])) ? mysqli_errno($GLOBALS["query_connection"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)))
When would that not be an object? You just created it as an object, when is it not an object? Why check for that? I don't see the point of much of that code, I don't see what you're gaining by not just creating a new MySQLi object and using that for all of your database work. Especially if you are making things like using prepared statements more difficult, you are actually reducing functionality by using those functions. Prepared statements should be the entire goal, if you want to design a database wrapper then design it around prepared statements. Personally, I don't see the purpose of a wrapper though. If you want an abstract database class that does not depend on the database backend then use PDO instead of MySQLi.
Link to post
Share on other sites

Sorry for the late reply, i have just begun in schools again so did not really have time before now to answer.

 

But as a start, i'm trying to learn MySQLI and only because i actually DO know normal MySQL, not sure about PDO tho...

 

What i am trying to do is to "like i stated previous" to make a working menu that i can use on my files that is in the "root" of my server...

Why i'm making a "object" in the switch is because of that i'm using it on the "query" function i made... like i also stated in the earlier/previous posts is the functions i have here is still in a "beta" mode.

 

The project i'm working on is a little bigger than i can explain "i think"... but the reason i'm trying to make the menu as i wish it to work is that i'm trying to make a website that can have ALOT websites within the source code i'm trying to work on.

 

An example is that if i have a file that is called "webshop.php" and another called "forums.php"... forums are normally not using the "Webshop" part... so, in order to make sure the file is NOT being used by any users on the forum AND on the same website/template design used, the user will get an error message.

 

The reason i made the object was to make an actual connection in the query function so it would work like a normal query... but like i said, the whole project is still in a "beta" mode, so working on the functionality.

 

I can give you the whole project if you wanna see what i am trying to do here, because it is a little hard to explain what i'm trying to do here. The "main" thing is that i'm trying to make it easier for coders (me for instance) to make a new website with faster methods.

 

The website will be able to build AND contain more than one website within itself, in other words then a coder can instead of just moving around between codes inside an editor, then he can gather the whole project under one website/place-project...

 

I'm not sure if you understand what i'm trying to do or explain here, but if you wonna have the source i'm trying to build i'm more then willing to send it to you so you can see what i'm talking about.

 

And yes, i do know what you mean about making a prepared statement and all that, but i just thought it would be easier to gather the whole thing under one function Oo?...

 

What i wish is just to make this one work... it is vital for my project to work this one out and make sure it's working...

 

Hope you have some sort of idea, or at least have some good tips on how to fix the DB-Connection function and query function x)

 

And sorry for the bad english/language, i was partying hard last night and well.. got lucky hehe x)... that, and then i also just woke up -.-'.

 

Thanks in advance if you still can help out Oo?

Just ask if you wish me to try to explain the project later, might be i'm more awake and helpful than now haha ^^'

 

Thanks again! :)

Link to post
Share on other sites

You need quotes around $tab in this line:

 

$menu_res = query("SELECT * FROM menu WHERE menu_file_url = ".$tab);

so:

 

$menu_res = query("SELECT * FROM menu WHERE menu_file_url = '".$tab."'");

 

You could have added this line and seen the output was 0.

 

echo mysql_num_rows($menu_res);
Link to post
Share on other sites

ohh... did not realise that one before you told me, how embarrasing! Thanks! -.-'...

 

EDIT

 

I'm not sure, but i noticed that i only get the "filename" and not the extension to my database via "$tab" variable... could it be because inside my DB i actually uses the extension on each file? like "filename.php"...

 

How do i get the filename + extension Oo?

Edited by rootKID
Link to post
Share on other sites

hmm... starting to make sence x)...

 

Will try and see if it's working later when home x)...

Edited by rootKID
Link to post
Share on other sites

And yes, i do know what you mean about making a prepared statement and all that, but i just thought it would be easier to gather the whole thing under one function Oo?

It's not. Trying to jam everything into one generic function might make specific things "easier", but you're losing a lot of functionality. The MySQLi extension is not complex just because the programmers decided to make it as complex as possible for no particular reason. MySQLi, and programming in general, is as complex as it is because this is what it needs to be in order to be generally useful instead of intended for specific purposes and being inflexible for everything else.
Link to post
Share on other sites

hmm... u suppose i see ur point, but the way i have build my website is a little bit complicated... i have developed my own "sort of" website design/theme template, so not sure how to re-do the whole thing and then use the MySQLI the proper way as u told me too...

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...