28, May 2012

If statment not doing what i want - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Access_user Class (Moderator: Olaf)
Topic: If statment not doing what i want
« previous next »
Pages: [1] Print

Author Topic: If statment not doing what i want  (Read 2250 times)
OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« on: Oct 07, 2007, 03:58:55 am »

was working on a little better user search (on Olaf's Access_user script), then the one i did the other day and have everything working but one small problem.

The code works, except a if statement that i added that if the activation switch button was selected to "n" that it would change so it would search the DB where access_level >= 0. when i have the if statement there it always defaults to "n" no matter your selection or even if you didnt select anything, but if i leave the if statement out it works perfectly fine. Guess the question is what in the code is making it default to "n" every time?

i think i just been looking at it to long and missing the most simple fix

code for the search on submit
Code:

<?php
elseif ($_POST['Submit'] == "Search") {
$act_str = (isset($_POST['activation'])) ? $_POST['activation'] : "";
$admin_update->user_search($_POST['login_name'], $_POST['acc_name'], $_POST['email'], $_POST['operator'], $_POST['level'], $act_str);
?>


code for the html page
Code:
Username:<input type="text" name="login_name" value="<?php echo (isset($_POST['login_name'])) ? $_POST['login_name'] : "" ?>"><br>
Account Name:<input type="text" name="acc_name" value="<?php echo (isset($_POST['acc_name'])) ? $_POST['acc_name'] : "" ?>"><br>
Email Address:<input type="text" name="email" value="<?php echo (isset($_POST['email'])) ? $_POST['email'] : "" ?>"><br>
Access Level:<select name="operator">
<option value=">">></option>
<option value=">=" selected>>=</option>
<option value="=">=</option>
<option value="<"><</option>
<option value="<="><=</option>
<option value="!=">!=</option>
</select>
<?php echo $admin_update->access_level_menu("0"); ?><br>
Active:<?php echo $admin_update->activation_switch(); ?>

the search code. the if statement on the 2nd line of function user_search seems to be it, with that there it always defaults to n.

Code:
<?php
function 
wildcard_replace($text){
$text str_replace('*''%'$text);
return $text;
}
function user_search($login$acc_name ""$email ""$operator ""$level ""$act_status ""){
if ($login) {
if ($act_status "n") {
$operator ">=";
$level "0";
}
$sql "SELECT login, acc_name, email, access_level, active FROM %s WHERE login LIKE '%s' AND access_level %s %s";
$sql .= ($acc_name) ? sprintf(" AND acc_name LIKE '%s'"$acc_name) : "";
$sql .= ($email) ? sprintf(" AND email LIKE '%s'"$email) : "";
$sql .= ($act_status) ? sprintf(" AND active = '%s'"$act_status) : "";
$sql_compl $this->wildcard_replace(sprintf($sql$this->table_name$login$operator$level));
echo $sql_compl;
exit;
$res_user mysql_query($sql_compl);
if (mysql_num_rows($res_user) == 1) {
$obj mysql_fetch_object($res_user);
$this->user_name $obj->login;
$this->get_userdata($this->user_name);
} elseif (mysql_num_rows($res_user) > 1) {
$menu "<select name=\"user\">\n";
while ($obj mysql_fetch_object($res_user)) {
$menu .= "  <option value=\"".$obj->login."\">".$obj->login."</option>\n";
}
$menu .= "</select><br>\n";
$this->find_user true;
mysql_free_result($res_user);
return $menu;
} else {
$this->the_msg $this->admin_message(13);
}
} else {
$this->the_msg$this->admin_message(21);
}
}
?>

Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Oct 07, 2007, 09:07:23 am »

what is the result of "echo $sql_compl;"

OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #2 on: Oct 07, 2007, 07:06:42 pm »

heh, i wrote that dang thing about 6 times and must have made the same mistake every time. left out the dubble == i feel like such a idiot now lol, but atleast it working now heh

can anyone thing of a good way to display the results for if multiple accounts are found, or you think that that just a list of users would be enough?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: Oct 07, 2007, 09:51:44 pm »

I think a list of users is ok, but of course it depends on the purpose you are using this

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #4 on: Oct 08, 2007, 11:15:16 pm »

well i thought i had this working but ran into a much odder problem that i don't understand why its doing it.
using these vars
login:*
email:
operator: >=
level: 1
act_status: n

with act_status as n level gets set to 0 and operator is set to >=

the $sql_compl is out puting as
Code:
SELECT login, acc_name, email, access_level, active FROM kingdomsend.users WHERE login LIKE '%' AND access_level >= 0 AND active = 'n'

now if i run that in phpmyadmin it works fine and displays only the accounts that active is 'n', but if i run it through the php script it selects all accounts whos access_level is >= 1 dosnt matter what active is set to.

code for the search function
Code:
<?php
function 
wildcard_replace($text){
$text str_replace('*''%'$text);
return $text;
}
function operator_display(){
$ary_operator = array('>=','>','=','<','<=''!=');
$menu_operator "<select name=\"operator\">";
foreach ($ary_operator as $dsp_operator){
$menu_operator .= "<option value=\"".$dsp_operator."\">".$dsp_operator."</option>";
}
$menu_operator .= "</select>";
return $menu_operator;
}
function user_search($login$acc_name ""$email ""$operator ""$level ""$act_status ""){
if ($login) {
if ($act_status == "n") {
$operator ">=";
$level "0";
}
$sql "SELECT login, acc_name, email, access_level, active FROM %s WHERE login LIKE '%s' AND access_level %s %s";
$sql .= ($acc_name) ? sprintf(" AND acc_name LIKE '%s'"$acc_name) : "";
$sql .= ($email) ? sprintf(" AND email LIKE '%s'"$email) : "";
$sql .= ($act_status) ? sprintf(" AND active = '%s'"$act_status) : "";
$sql_compl $this->wildcard_replace(sprintf($sql$this->table_name$login$operator$level));
$res_user mysql_query($sql_compl);
if (mysql_num_rows($res_user) == 1) {
$obj mysql_fetch_object($res_user);
$this->user_name $obj->login;
$this->get_userdata($this->user_name);
} elseif (mysql_num_rows($res_user) > 1) {
$menu "<select name=\"user\">\n";
while ($obj mysql_fetch_object($res_user)) {
$menu .= "  <option value=\"".$obj->login."\">".$obj->login."</option>\n";
}
$menu .= "</select><br>\n";
$this->find_user true;
mysql_free_result($res_user);
return $menu;
} else {
$this->the_msg $this->admin_message(13);
}
} else {
$this->the_msg $this->admin_message(21);
}
}
?>

and here i thought the hardest part would be doing the str_replace commands  Tongue
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Oct 09, 2007, 09:49:36 am »

do you checked the sql statement with echo $sql_compl;?

OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #6 on: Oct 09, 2007, 06:37:27 pm »

yes i did, thats how i got the info for it when i posted it, it was a direct copy and paste from the echo.

this has been a brain itcher for me heh
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Oct 09, 2007, 07:45:18 pm »

yes i did, thats how i got the info for it when i posted it, it was a direct copy and paste from the echo.

this has been a brain itcher for me heh
show it here

and try a copy / paste in phpmyadmin

OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #8 on: Oct 09, 2007, 10:21:40 pm »

well with doing a little more echoing around the code think i might have found why its giving the results it is, but starting to make my head ever more blah...

if i echo $sql_compl after the line $sql_compl =$this->wildcard_replace(sprintf($sql, $this->table_name, $login, $operator, $level)); then it will be this
Code:
$sql_compl = SELECT login, acc_name, email, access_level, active FROM kingdomsend.users WHERE login LIKE '%' AND access_level >= 0 AND active = 'n'

but if i echo $sql_compl while in the while ($obj = mysql_fetch_object($res_user)) loop $sql_compl will be
Code:
SELECT login, acc_name, email, access_level, active FROM kingdomsend.users WHERE login LIKE '%' AND access_level

it seems to be cutting off >= and everything after on the sql line

hope this helps some because this has actually left me more confused. I even rewrote the code and left out the if statments and just went with a list of users no mater the results and echoed out the sql lines and it kept doing the same thing when it got into the while loop.
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Oct 10, 2007, 06:57:46 am »

ok now you have localized the problem, check your form elements, names etc.

OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #10 on: Oct 10, 2007, 10:32:46 pm »

yeah i checked all thos, everything seems to be fine, its working correctly except that since its seems to be truncating the sql select line its pulling the wrong data. I cant see why that in the while loop it would be cutting off ">= 0 AND active = 'n'".

is there something that mysql_fetch_object wouldnt like about >= in its statement?
Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Oct 11, 2007, 06:26:12 am »

it must be here:
$sql_compl = $this->wildcard_replace(sprintf($sql, $this->table_name, $login, $operator, $level));

work on this, the whole sql statement part looks not optimal

OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #12 on: Oct 17, 2007, 01:01:55 am »

yeah, im quite not sure on why i did that myself..

I'm still working on this altho the release of portal hasn't helped, that game is to addictive.

altho almost back to square one, i have noticed that before the while loop everything is working, after that all the $vars from the function($var...etc) stuff is resetting back to what ever it was set with ="" in the function call, i have put different things in there and noticed that it was calling whatever from it instead of was the passed throw from the form. I have tried not doing function($var="whatever") but then i get Warning: Missing argument

soon i will have it altho not sure why it would do that, altho i know once i find that answer that will solve the problem hehe...(caption obvious there)
OMG!I am geek
**
Posts: 53
406 credits
Members referred : 0


« Reply #13 on: Oct 17, 2007, 05:19:24 pm »

i finally got it working, friend took a quick look and went you just need this, and then i slammed my head on the desk a few time, because i have no idea why i didn't do that since I've done it a 1000x before ROFL

i updated my user search post now search able by login name, real name, email, access level and account status

http://www.webdigity.com/index.php/topic,7187.0.User+search.html
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7193
Tags : php help access user Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP  >  PHP classes @finalwebsites.com  >  Access_user Class (Moderator: Olaf)
Topic: If statment not doing what i want
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 09:30:01 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.