Jump to content

Archived

This topic is now archived and is closed to further replies.

corillo181

if()help

Recommended Posts

i'm trying to use thi statement but some how is not working..

[code]
$level=array(0,1,2,3,4,5);
if($level < 1){
echo "blah";
}elseif($level >= 1){
echo " blah blah"
}

[/code]

Share this post


Link to post
Share on other sites
What do you want with the $level array?
Do you want it to loop through all it's elements and echo different things depending on the level? For that:

[code]foreach ($level as $l) {
   if ($l < 1) {
      echo "blah";  
   } elseif ($l >= 1) {
      echo " blah blah";
   }
}[/code]

Share this post


Link to post
Share on other sites
In this case also the elseif is pointless, as it is the opposite of the first if in any case. An else would suffice.

Share this post


Link to post
Share on other sites
sorry i gave a bad example/// i got a enum set from 0 to 5 on my database.. that determin the level of the user..

when the user agree to something it has to change from 0 to one and so on..

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]


<?php if($level == 0){?>
<tr>
<td colspan="3"><div align="center">This page contain adult content, and should only be view be people 18 or older.<br />
are you 18 or older?
<form method="post" action="<?php $_SERVER['PHP_SELF']?>" name="yf"><label>
<input type="submit" name="Submit" value="yes" />
</label>
</form>
by clicking yes, you agree to be 18 years of age or older.

<?php if(isset($_POST['Submit'])){
$query=mysql_query("UPDATE tra_users SET level='1' WHERE username='$username'")or die(mysql_error);
}?>

<?php
}elseif($level >= 1){
echo "blah"
}
?>

[/quote]

Share this post


Link to post
Share on other sites
not 100% on this but i think that enum data type is a string datatype so i think you have to put quotes around your numbers there like

[code]
if ($level == '0') {
..
} elseif ($level >= '1') {
..
}
[/code]

Share this post


Link to post
Share on other sites
Well, I assume there's far more code than we're seeing - at least I assume so. There's no database connection and no way in which $level gets any value at the start of that code. If I read the logic right, the form submit is only checked when $level = 0 (otherwise, it always drops through to the else).

Share this post


Link to post
Share on other sites
mm i didn't posted the whole code because is a big script , i jus tposted the part i know i'm having trouble with and $level gets it's value from $_session wich is passed on with every page.. everything is set up like is supossed to.. it's just tha tpart that i don't know how to make it work..

Share this post


Link to post
Share on other sites
just for shits and grins, put this before your if statement:

echo "level: ".$level;

and post what it echos

Share this post


Link to post
Share on other sites
i don't know whats wrong with it, when ppl agree it does change from 0 to 1 but when i echo it out it echo 0: level 0 .. even is in the database is change to 1 for that user..any one knows why is this?

Share this post


Link to post
Share on other sites
[!--quoteo(post=382563:date=Jun 11 2006, 05:41 PM:name=Richard181)--][div class=\'quotetop\']QUOTE(Richard181 @ Jun 11 2006, 05:41 PM) [snapback]382563[/snapback][/div][div class=\'quotemain\'][!--quotec--]
any one has a level system that can tell me how they got it to work ?
[/quote]

what for users!

[!--quoteo(post=382564:date=Jun 11 2006, 05:48 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Jun 11 2006, 05:48 PM) [snapback]382564[/snapback][/div][div class=\'quotemain\'][!--quotec--]
what for users!
[/quote]

Exsplain the consept of your script or post it ok,
i can not guess.

according with the help above there all correct

Share this post


Link to post
Share on other sites
i want to make something like if a user has a level 5 he can do anything tot he site like a level 5 will be a admin, a level 1 would be someone that agree to somethings.. like that.. i made a enum('0','1','2','3','4'5')

and i passed the level in a $_session and tryed using if($level > 0 ){ do this}, but it doesn't work..

Share this post


Link to post
Share on other sites
If $level is saved in a session ....

... then you need to retrieve the session value in any script where $level is used AND when you change $level you need to put it into the database AND update the value saved as the session variable - otherwise, you'll be looking at an outdated (and wrong) session value.

Share this post


Link to post
Share on other sites
look at the 3rd post thats is the example of what i did tell me how i can chang eit to make it work, becuase it does change but the value as you say stay the same i don't know how to change that..

Share this post


Link to post
Share on other sites
[!--quoteo(post=382567:date=Jun 11 2006, 05:54 PM:name=Richard181)--][div class=\'quotetop\']QUOTE(Richard181 @ Jun 11 2006, 05:54 PM) [snapback]382567[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i want to make something like if a user has a level 5 he can do anything tot he site like a level 5 will be a admin, a level 1 would be someone that agree to somethings.. like that.. i made a enum('0','1','2','3','4'5')

and i passed the level in a $_session and tryed using if($level > 0 ){ do this}, but it doesn't work..
[/quote]

know any level that the user is the code will do as long as you set the do somethink.

example
[code]
<?
$query="select * from xxxxxx where id='$id'";
$result=mysql_query($query);

while ($record=mysql_fetch_assoc($result)){

if($record["level"] >=0) {

do somethink

}else{

echo "sorry not specified";
}
}
?>
[/code]

Share this post


Link to post
Share on other sites
How is $level being populated? Are you setting the variable like so:
$level = $_SESSION['level']

I think you'll be best of with a switch rather than if/elseif statement like so:
[code]<?php
session_start(); //make sure you have this line whenever you are dealing with sessions

$level = $_SESSION['level'];

switch($level)
{
    case 5:
        echo "level 5";
        break;
    case 4:
        echo "level 4";
        break;
    case 3:
        echo "level 3";
        break;
    case 2:
        echo "level 2";
        break;
    case 1:
        echo "level 1";
        break;
    case 0:
        echo "level 0";
        break;
    defualt:
        die("Unknown level script has been killed! level is:" . $level)
        break;
}[/code]
When you use that depending on the value of $level it should echo out the level, if $level is not set to 0,1,2,3,4 or 5 it'll kill the script and echo the value pf $level

Share this post


Link to post
Share on other sites
i just need to know how to repass it as a session becuase for it to wokr the user has to log out and log bakc in so when they pass by the member check the session reads the new info and is passed threw the section,

so the problem is in refreshin the member section

Share this post


Link to post
Share on other sites
To pass the level as session do this:
[code]<?php
session_start();

//put code here that gets the users level

$_SESSION['level'] = $level;

?>[/code]
Then when you want to retrieve the session make sure you have session_start on the page that checks the user level, see my post above.

Share this post


Link to post
Share on other sites
yeah i know i already got the level passed as a section, i just neeed to refresh the section..

how i do that?

Share this post


Link to post
Share on other sites
What do you mean by refresh? Do want the browser to refesh the page? If so why?

To refresh the page in javascript you can do this:
[code]header("Refresh: 0; url=" . $_SERVER['PHP_SELF']);[/code]
provided that you dont have any output to the browser before you use that code.

Share this post


Link to post
Share on other sites
no.. not the page... you know , when you change something that is passed by $_session it wont take effect until th euser close the current section and open a new one.. you know that right?

so what i need is a way for when the user change something that is used by a section to refresh that user section so the new information is in his current section.

Share this post


Link to post
Share on other sites
Session data is updated automatically so you dont need to create a new session for the data come available! Where did you get that from?

Share this post


Link to post
Share on other sites
because i did a form and in the form it agree to set the level from 1 0 to 1 and when the i press the agree it change from 0 to 1, but when i try echo out the level it echo out 0 and when i destroy the session and stared a new one, it did echo out a 1.

Share this post


Link to post
Share on other sites
Was you echoing out $level or $_SESSION['level']. If you was echoing out $level that will not update as holds the old value of level session. If you echo'd out $_SESSION['level'] that should echo out your new level that you set with the form.

Looks you have a bug in your script somewhere if the new session data isn't comming through.

Share this post


Link to post
Share on other sites

×

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.