Log in

View Full Version : function not evaluating correctly



Hypertext
27-03-2008, 03:03 AM
Heres my function, it always returns 0...


function user_type($page) {
$user = $logged_user;
$level = mysql_query("SELECT * FROM users WHERE user='$user'");
$level_a = mysql_fetch_array($level);
$final_level = $level_a['usergroup'];
$check_level = mysql_query("SELECT * FROM userstructure WHERE name='$final_level'");
$check_level_a = mysql_fetch_array($check_level);
if ( $check_level_a[''.$page.''] == '1' ) {
return "1";
} else {
return "0";
}
}Heres how I use it in a backend page: ex. testimonials:




if(user_type("testimonials") == "0") {
header("Location: notallowed.php");
exit();
}

Moved by Hitman (Forum Moderator) from Designing & Development: Please post in the correct forum next time, thanks.

Dentafrice
27-03-2008, 01:01 PM
God, learn how to code. What the heck is this?


$check_level_a[''.$page.''] == '1'


Your going to need to use a global I believe,


However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.



<?php
function user_type ($page)
{
global $logged_user;
$level = mysql_query("SELECT * FROM users WHERE user='$user'"); // QUIT USING WILDCARDS =\
$level_a = mysql_fetch_array($level); // You took my structure :@
$final_level = $level_a["usergroup"];
$check_level = mysql_query("SELECT * FROM userstructure WHERE name='$final_level'");
$check_level_a = mysql_fetch_array($check_level);
if ($check_level_a[$page] == "1") {
return "1";
} else {
return "0";
}
}

$user_type = user_type("testimonials");
if($user_type == "0") {
header("Location: notallowed.php"); // This is stupid, it relies on the browser to redirect, plus it ***** up with sessions.
exit();
}
?>

Hypertext
27-03-2008, 01:03 PM
I realised the syntax in ''$var''

the global worked. thanks =)

Dentafrice
27-03-2008, 01:04 PM
Ummm.. what?

Agnostic Bear
27-03-2008, 01:07 PM
$level = mysql_query("SELECT * FROM users WHERE user='$user'"); // QUIT USING WILDCARDS =\

wildcards are fine unless you're pulling out a stupid amount of data which I highly doubt.



$level_a = mysql_fetch_array($level); // You took my structure :@

what structure


header("Location: notallowed.php"); // This is stupid, it relies on the browser to redirect, plus it ***** up with sessions.

All redirects rely on the browser. And it doesn't mess up with sessions.

Dentafrice
27-03-2008, 01:09 PM
If the stupid sessions goes bazackwhap, it throws out a nice little error about sending **** before the sessions headers already sent all that meatwhack.

I showed him my source code to something, and now his _a, _n, looks like mine. :P

Hypertext
27-03-2008, 01:12 PM
Meh, I'll throw out an error, it exit();'s anyway tbh.

Bojangles
27-03-2008, 01:12 PM
If the stupid sessions goes bazackwhap, it throws out a nice little error about sending **** before the sessions headers already sent all that meatwhack.

I showed him my source code to something, and now his _a, _n, looks like mine. :P
Some people never change I guess:rolleyes:

Hypertext
27-03-2008, 01:13 PM
lol, it's better than what I used to have:

$query
$result
$row
$querya
$resulta
$row
$queryasox
$resultex
$rowarc

I changed the language almost.

Bojangles
27-03-2008, 01:15 PM
Lmao why did you use $rowarc o.0

Hypertext
27-03-2008, 01:17 PM
Idk tbh =) hard to keep track of :P

Agnostic Bear
27-03-2008, 01:18 PM
If the stupid sessions goes bazackwhap, it throws out a nice little error about sending **** before the sessions headers already sent all that meatwhack.

I showed him my source code to something, and now his _a, _n, looks like mine. :P

I showed you my basic template engine and you're using code snippets from it for commercial purposes but I don't complain '____'

Hypertext
27-03-2008, 01:19 PM
I only used a better format of variables, if you'd like it so much, I'll revert to different languages, minus the accents.

Dentafrice
27-03-2008, 01:21 PM
lol, it's better than what I used to have:

$query
$result
$row
$querya
$resulta
$row
$queryasox
$resultex
$rowarc

I changed the language almost.

haha, looks confusing. I use the same one in each function, never fails.


Some people never change I guess:rolleyes:

haha =p


I showed you my basic template engine and you're using code snippets from it for commercial purposes but I don't complain '____'

I'm not using it dear! I just saw how you did the regex's and it actually taught me some things about the references you were speaking of.

=] I didn't use them, I tried the way you said, it kept outputting bad though, and I didn't feel like going through the files making them return instead of echo.

So I just devised my own way :P

Hypertext
27-03-2008, 01:58 PM
Hah, indeed it was!

Baving
27-03-2008, 04:06 PM
You can use table joins to make your life a lot easier:


funciton user_type($page) {
global $logged_user;

$sql = "select userstructure.`".addslashes($page)."` from users,userstructure where users.user = '".$logged_user."' and users.usergroup = userstructure.name limit 1";
$result = mysql_query($sql);
$data = mysql_fetch_array($result);

if($data[$page]=='1') return 1; else return 0;
}

It may need a bit of tinkering as I don't know your table structure.

Want to hide these adverts? Register an account for free!