Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Using array in this instance?
Old 09-30-2008, 10:12 PM Using array in this instance?
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Hiyas, Just stuck on a little problem.

at the moment I have error variables store with information -

error.php
<?
$exc1001 = 'Email addresses does not match!';
$exc1002 = 'Wrong username and/or password';
$exc1003 = 'Incorrect password';
?>

Now when the a script outputs an error its redirected to this error page.
It stores what error it has encountered in the address bar.
error.php?error=1001

What I am wanting is instead of using lots of if's to write a simple script that checks what error is in the address bar and then check the list of errors, then outputs that error.
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-01-2008, 01:24 AM Re: Using array in this instance?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
PHP Code:
$err[1001] = 'Error 1001';
$err[1002] = 'Error 1002';
$err[1003] = 'Error 1003';

if (isset(
$errors[$_GET['error']])) {
   
$error $errors[$_GET['error']];
}
else {
   
$error 'Unknown error';

__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 10-01-2008, 06:57 AM Re: Using array in this instance?
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
PHP Code:
<?
  $err
[1001] = 'Error 1001';
  
$err[1002] = 'Error 1002';
  
$err[1003] = 'Error 1003';
  
$err[1004] = 'Error 1004';
  
$err[1005] = 'Error 1005';

if (isset(
$errors[$_GET['error_code']])) {
   
$error $errors[$_GET['error_code']];
}
else {
   
$error 'Unknown error';
}

echo 
$error
?>
All this outputs is Unknown Error when error_code=1004 etc.
Thankyou for your reply
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 10-01-2008, 07:33 AM Re: Using array in this instance?
Junior Talker

Posts: 4
Name: Dai Williams
Location: Derby, UK
Trades: 0
Solution was valid but had a typo, try:

PHP Code:
<? 
$err
[1001] = 'Error 1001'
$err[1002] = 'Error 1002'
$err[1003] = 'Error 1003'
$err[1004] = 'Error 1004'
$err[1005] = 'Error 1005'
 
if (isset(
$err[$_GET['error_code']])) { 
$error $err[$_GET['error_code']]; 

else { 
$error 'Unknown error'

 
echo 
$error 
?>

Last edited by DaiWelsh; 10-01-2008 at 07:33 AM.. Reason: tag error
DaiWelsh is offline
Reply With Quote
View Public Profile Visit DaiWelsh's homepage!
 
Old 10-01-2008, 07:56 AM Re: Using array in this instance?
maxxximus's Avatar
Extreme Talker

Posts: 219
Name: Rob
Location: UK
Trades: 0
A simple switch statement would help you here

PHP Code:
<?php
$error
intval($_GET['error']);
switch (
$error) {
case 
1001:
    echo 
"Email addresses does not match!";
    break;
case 
1002:
    echo 
"Wrong username and/or password";
    break;
case 
1003:
    echo 
"Incorrect password";
    break;
default:
    echo 
"Error undefined";
    break;
}

?>
maxxximus is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 06:50 AM Re: Using array in this instance?
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Thankyou very much both of you.

Both work fantastically and I think I will go with the switch as I haven't used that before and I like collecting working Scripts I can apply to other sites I may make in the future.

Both of your Talkupation! points have clicked on

Thanks again
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 10-07-2008, 07:20 AM Re: Using array in this instance?
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
It is better to use switch if you need to perform some extra action in different cases. If you only need to set the value of a single variable you should use indexed array.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Using array in this instance?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19150 seconds with 12 queries