Jump to content

Arrays not outputting


Ninjakreborn

Recommended Posts

I am working on my personal programmer assistant


I have the following
[code]<?php
## Configuration Settings
// User Variables: Put anything you want loading up as a global variable here
# Your email address
$config['user']['email'] = "";

// Path Variables: These are variables related to paths, or similar information

#root path
$config['paths']['root'] = $_SERVER['DOCUMENT_ROOT'];

#framework folder
$config['paths']['system'] = "framework/ppassistant/system";

#path to specific page
$config['paths']['self'] = $_SERVER['PHP_SELF'];
#Assets folder
$config['paths']['assets'] = "/ppassistant/projectassets/";

// Database Variables: Configuration settings and information related to databases
$config['db']['active'] = FALSE;
$config['db']['host'] = "";
$config['db']['username'] = "";
$config['db']['password'] = "";

// Main System Files: Core files related specifically to the system itself
$config['system']['error'] = array();

// inclusionary files
#critical: Files 100% necessary for the framework to run
#Corefiles: File's within the core folder.  Generally these can be taken out and added at will
#Classes: File's that contain classes
#Functions: File's that contain just functions
#plugins: File's that contain third party classes/functions (aside from ones I created)
#extensions: Extensions onto pre-existing classes
$config['require']['critical'] = array();


$config['require']['corefiles'] = array("/core/"=>"inisettings.php"
  );

$config['require']['functions'] = array("/functions/"=>"arrays.inc.php",
        "/functions/"=>"forms.inc.php",
        );
 
$config['require']['classes'] = array();


## End editable configuration settings


// check and connect to database.
if ($config['db']['active'] == TRUE) { // if yes perform db work
if (!mysql_connect($dbhost, $dbusername, $dbpassword)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem making connection to the database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
if (!mysql_select_db($db)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem Selecting database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
}

// Run includes based on chosen settings.
#critical
foreach($config['require']['critical'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#corefiles
foreach($config['require']['corefiles'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}

#functions
foreach($config['require']['functions'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#classes
foreach($config['require']['classes'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}


// Check for errors, if so, show them.
if (isset($config['system']['error'])) {
foreach ($config['system']['error'] as $k=>$v) {
echo $v . "<br />";
}
exit();
}
?>
[/code]
Ok, for some reason, the bottom part is not working.  Everything else has been tested.  I wrote it blindly, then went and started testing everything, found some problems, fixed them.  Now I have encountered a problem here.  When I try to check for server error's, and display them, then exit.  It's not displaying any server error's for some reason.
Link to comment
Share on other sites

Your problem is that you are treating "require_once" as a function that returns "false" if it fails. It is not a function, it is a statement and doesn't return anything. You should be checking if the file exists and filling in the error message if that fails.

Ken
Link to comment
Share on other sites

I know the file exists.
What the current point is (of what I am trying to do), is based on an array of file's to be required.  Require them.  IF something goes wrong during the process, show an error, instead of just kicking out a random error out of nowhere (the standard default error that comes up), I wanted to make it easier to understand, that is why I am trying to trap it in a variable.

If you try to require, and it doesn't it should return the error (right?)
Link to comment
Share on other sites

Yes, sound advice, but even then, there is something wrong with the way I am implementing this in general.
I ran another test

[code]
<?php
// check and connect to database.
if ($config['db']['active'] === TRUE) { // if yes perform db work
if (!@mysql_connect($dbhost, $dbusername, $dbpassword)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem making connection to the database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
if (!@mysql_select_db($db)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem Selecting database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
}

// Run includes based on chosen settings.
#critical
foreach($config['require']['critical'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#corefiles
foreach($config['require']['corefiles'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}

#functions
foreach($config['require']['functions'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#classes
foreach($config['require']['classes'] as $k=>$v) {
if (!@require_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}


// Check for errors, if so, show them.
echo $config['system']['error'];
if (isset($config['system']['error'])) {
foreach ($config['system']['error'] as $k=>$v) {
echo $v . "<br />";
echo "hi";
}
exit();
}
?>
[/code]
Right now, no errors that pop up are getting recorded into the $config['system']['error'] array.
Here is what is annoying me
In theory if a require fails you can do something with it.
Even on other applications I see
if (!@require_once("/system/config.php")) {
define("basepath", "/system/config.php");
}
I have seen that before, it's obvious you can act on it.
I want to do the same, if it Does Not require, then I want to formulate my own custom error.
Which is telling what the error was, then echoing out the filename of the file that could not be included.  WIth enough formatting to make it look decent when the errors come up so they are easy to track down.
Same with databases, if I database does not connect, you can do something with that information, same if db cannot be selected.
That is what I am trying to do
catch when something happens, I tested the database, the values are not going into the array at all, anyone see anything wrong with the array setup or the way i am trying to trap the information into the array?
Link to comment
Share on other sites

[quote]@ surpresses errors, so @require_once isn't going to give any errors, and the if won't be reached.
[/quote]
Yes but won't it still eb something that can be acted on.
If the require is not successfully, then it can be acted on with !require_once
See, the error suppression is to prevent it from throwing out the initial error, letting me catch that it had an error, and formulate my own error handler.

This is what is starting to confuse me.

I even tested it out, here
[code]<?php
if (!@require_once("/config.php")) {
  echo "File Not Included";
}else {
  echo "File included";
}
?>[/code]
When I set this to the right filepath, the out put was "file included" when I set the wrong path intentionally to test it it outputs "File Not Included" so that shows me there, that if a require fails, you can act on it.  I also already knew if a database connection or selection fails it can be acted on.  There is something I am missing with teh array implementation.  I declared the arrays, I know the requires work when done normally, but when I alter one to specifically be false (to try to force an error), nothing is gonig into the $config['system']['error'] array.  Same when the database is fed fake information to test the system, it still doesn't register my arrays.  Or it's not outputting them right, but I did standard checks on the array to see if anything was in them, and there was nothing.
Link to comment
Share on other sites

To check what a function returns, just look it up in the manual. Although this one gets a bit confusing.
"Return values are the same as with include(). If the file was already included, this function returns TRUE"

This makes me think if it's including it the first time, it will still return FALSE, so the logic you want won't work.
Link to comment
Share on other sites

Same with the database.
I might have to use the way codeignitor loads it's files.
I wanted a solution that looked cleaner to me, or took up less code.
I have done custom error handling before, but never with requires.
Maybe I should just require them and leave them at that.

If I do if file exists, will it be the same thing.  I want global error handling, for the system I am creating.
Other errors related to a site, I do seperately.  However if ANY errors occur during the startup of the program, or any initial processes associated with the program, I want to gather all information into an array, and kill the execution at the very bottom of the script, so I aguarantee when it starts up, if no errors show up, I know then, that everything is 100% successful, and there are no errors anywhre throughout the system.

I am thinking running if (fileexists), for the things I am including will get too encumbersome, but that might be the only way.  I may just end up using the way they include files  (The files that you wnat to auto load), on code ignitor.  I was thinking there was a simpler way though.
Link to comment
Share on other sites

Maybe [url=http://www.php.net/manual/en/function.error-get-last.php]error_get_last()[/url] can help you?
Replace the require_once with include_once and then do something like this:
[code]<?php

include_once("file.txt");
$last_error = error_get_last();
if($last_error !== NULL && $last_error['line'] == (__LINE__ -2))
die ("An error occurred while including the file.");

?>[/code]

Not the nicest way, but it'll do.

Orio.
Link to comment
Share on other sites

Hmm that is a thought.  Something for the future, but the reason I am going to the difficulty to try and generate specific error message, is because I am wanting this whole system to be very powerful when it's done.  Very clean, very smooth, and most of all easy to read.  That was why I was trying a simple approach, and ran into these problems.  THat is a worthy fix, but I am wanting something substantial, something I can 100% rely on, and gaurantee they get included, if not there is a returned error.

I want this system to run very smoothly, and the errors to be very understandable, so if someone was just starting out, it wouldn't be hard for them to decide what was going on with what errors.

That is why I was pressing for this arrangement, if I used codeignitors method.  WHen someone includes a controller, if it doesn't exist it give's an error, I am just having trouble tracking down the file's code ignitor uses to auto_load the classes, functions, core file's, and everything else.  I know it gives nicely formatted error messages, for one's that do not load correctly, only different is I want to wait until the end of the process and show them all at once instead of killing the script right then.  SO they have a full list of all errors, and all problems that arose while starting up the system.
Link to comment
Share on other sites

[code]// check and connect to database.
if ($config['db']['active'] === TRUE) { // if yes perform db work
if (!@mysql_connect($dbhost, $dbusername, $dbpassword)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem making connection to the database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
if (!@mysql_select_db($db)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem Selecting database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
}

// Run includes based on chosen settings.
#critical
foreach($config['require']['critical'] as $k=>$v) {
if (!include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#corefiles
foreach($config['require']['corefiles'] as $k=>$v) {
if (!include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}

#functions
foreach($config['require']['functions'] as $k=>$v) {
if (!include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#classes
foreach($config['require']['classes'] as $k=>$v) {
if (!include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
echo "die4";
}
}


// Check for errors, if so, show them.
echo $config['system']['error'];
if (isset($config['system']['error'])) {
foreach ($config['system']['error'] as $k=>$v) {
echo $v . "<br />";
}
exit();
}[/code]
Actually your include idea worked.  It's not registering stuff in the arrays, but my new problem is it's still outputting that first error message.  The data base portion even seems to be working now.
Link to comment
Share on other sites

[code]<?php
// check and connect to database.
if ($config['db']['active'] === TRUE) { // if yes perform db work
if (!@mysql_connect($dbhost, $dbusername, $dbpassword)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem making connection to the database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
if (!@mysql_select_db($db)) {
$config['system']['error'][] = "Critical Database Error:";
$config['system']['error'][] = "Problem Selecting database";
$config['system']['error'][] = "Exact Error: " . mysql_error();
$config['system']['error'][] = "<hr />";
}
}

// Run includes based on chosen settings.
#critical
foreach($config['require']['critical'] as $k=>$v) {
if (!@include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#corefiles
foreach($config['require']['corefiles'] as $k=>$v) {
if (!@include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}

#functions
foreach($config['require']['functions'] as $k=>$v) {
if (!@include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
}
}
#classes
foreach($config['require']['classes'] as $k=>$v) {
if (!@include_once($config['paths']['root'] . $config['paths']['system'] . $k . $v)) {
$config['system']['error'][] = "Critical Inclusion Error:";
$config['system']['error'][] = $k . $v . " Could not be required into the system.";
echo "die4";
}
}


// Check for errors, if so, show them.
echo $config['system']['error'];
if (isset($config['system']['error'])) {
foreach ($config['system']['error'] as $k=>$v) {
echo $v . "<br />";
}
exit();
}
?>[/code]
Thanks, I forgot about that.
When you do require, if it doesn't work it kilsl the script.  When I changed it to include, and tried that, it worked perfectly.
It included the ones it could, and hteone's it couldn't it supressed the error, and populated the array.

That was exactly what I was wanting.  I had totally forgotten that by nature require kills the script if the requirement fails.

Thanks.  Now I can move onto formatting and cleaning up the error messages.  Perfect.

Thanks again.
Link to comment
Share on other sites

a little off topic but: the use of @ has been mentioned, but personally I think it's seldom a good idea to use it. Amongst many other things, such as making life easier, frameworks should be handling errors properly rather than hiding them. If the day comes where you have an error and cant find it, you're stuffed.

take a look at [url=http://www.php.net/set_error_handler]set_error_handler[/url]. it goes along way to push errors all into one single place which can, at your disretion, be turned on/off.
Link to comment
Share on other sites

[quote]a little off topic but: the use of @ has been mentioned, but personally I think it's seldom a good idea to use it. Amongst many other things, such as making life easier, frameworks should be handling errors properly rather than hiding them. If the day comes where you have an error and cant find it, you're stuffed.

take a look at set_error_handler. it goes along way to push errors all into one single place which can, at your disretion, be turned on/off.[/quote]

I understand what you are saying, and see what you mean.  However I see the use in them, when it comes to custom error messages.

[quote]If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.[/quote]

Based on specific reasons I would rather not use set_error_handler.  THere are a few other reasons as well, it's just not what I am specifically looking for in this situation.

I know a lot of people dislike the use of the at symbol, and generally so do I, with the exception of errors that I want to perform custom error handling on.  In this, the error message (default) might be supressed, but a custom error message is generated in stead, so it still gives the proper error message, just a lot cleaner.  Right now if I start up something, and see 2-3 errors pop up, it takes a minute to figure out what's going on, or even why it's not working.  I would rather do compelte custom error handling, and doing error handling without the @ symbol seems impossible, because then it not only outputs my custom error, but also throws out php's default errors.
I also don't want to totally turn off error handling entirely because then it will not output errors related to the project, when someone is actually working on a site.

That is the reasoning for me to use it during this specific situation.
Link to comment
Share on other sites

It works fine, as mentioned up there.

However there is another problem.

if (isset($config['system']['error'])) {
foreach ($config['system']['error'] as $k=>$v) {
echo $v . "<br />";
}
// kill script if errors are present.  Program should run error free.
}
Here when I test for the array, I had the die there, where it says kill teh script, and it's killing the script whether there are errors or not.
I need it to test for config, if it's got values in it, that didn't work.
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.