Topic: If statment not doing what i want (Read 1160 times)
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« on: Oct 07, 2007, 04: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
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #2 on: Oct 07, 2007, 08: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:
Posts: 8357
43129 credits Members referred : 3
« Reply #3 on: Oct 07, 2007, 10:51:44 PM »
I think a list of users is ok, but of course it depends on the purpose you are using this
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #4 on: Oct 09, 2007, 12:15:16 AM »
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
Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6557
40174 credits Members referred : 374
It's time to use PHP5!
« Reply #5 on: Oct 09, 2007, 10:49:36 AM »
do you checked the sql statement with echo $sql_compl;?
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #8 on: Oct 09, 2007, 11: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:
Posts: 6557
40174 credits Members referred : 374
It's time to use PHP5!
« Reply #9 on: Oct 10, 2007, 07:57:46 AM »
ok now you have localized the problem, check your form elements, names etc.
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #10 on: Oct 10, 2007, 11: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:
Posts: 6557
40174 credits Members referred : 374
It's time to use PHP5!
« Reply #11 on: Oct 11, 2007, 07: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
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #12 on: Oct 17, 2007, 02: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)
1 post to be geek!
Posts: 49
374 credits Members referred : 0
« Reply #13 on: Oct 17, 2007, 06: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