Jump to content

Create a dynamic variable?


Drewdle

Recommended Posts

I have a form that inserts information into a mysql, the only prolem is I need to add different surrounding html to the information when entering it into the database depending what the value is...(essentially I want to store font color around different levels)

 

How would I define the same variable with different information depending on the value?

 

The information is called using:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$editLEVEL=$_POST['level'];

 

I want it so if 'Level1' is the posted value then $editLEVEL would be '<color=1>$editLEVEL</color>'

but if 'Level2' is the posted value the $editLEVEL would be '<color=2>$editLEVEL</color>'

 

If you dont understand just let me know I'll try explain best I can.

 

 

 

Link to comment
Share on other sites

if 'Level1' is the posted value then $editLEVEL would be '<color=1>$editLEVEL</color>'

but if 'Level2' is the posted value the $editLEVEL would be '<color=2>$editLEVEL</color>'

 

do you mean an IF statement?

 

if ($_POST['level'] == 'Level1') {
    $editLEVEL = "<color=1>$editLEVEL</color>";
} else if ($_POST['level'] == 'Level2') {
    $editLEVEL = "<color=2>$editLEVEL</color>";
}

Link to comment
Share on other sites

You're on the right track.  Basically, if level is a, color is 1 if level is b, color is 2, etc.:

if (isset($_POST['level']))
{
     if ($_POST['level']==$content1) $color = 'blue';
     elseif ($_POST['level']==$content2) $color = 'red';
     elseif ($_POST['level']=='duck') $color = 'black';
     elseif ($_POST['level']==$content4) $color='brown';
     elseif ($_POST['level']==$content5) $color='#0000FF';
     else $color='yellow';
}

 

Then you can do something like:

<?php echo "<span style=\"color: $color;\">$_POST[level]</span>"; ?>

or whatever you want it to say

Link to comment
Share on other sites

if 'Level1' is the posted value then $editLEVEL would be '<color=1>$editLEVEL</color>'

but if 'Level2' is the posted value the $editLEVEL would be '<color=2>$editLEVEL</color>'

 

do you mean an IF statement?

 


if ($_POST['level'] == 'Level1') {
    $editLEVEL = "<color=1>$editLEVEL</color>";
} else if ($_POST['level'] == 'Level2') {
    $editLEVEL = "<color=2>$editLEVEL</color>";
}

 

I tried it that way and added another else if statement on the end, then the middle statement didn't work how do I add another else if statement?...or would it just be an else?

 

And also when the database info gets executed on the page all the text after the info is the same colour as the entry, it doesn't appear to be cancled by the </color> tag??

 

heres the database info display code:

<?php
while ($row = mysql_fetch_array($query)) {
       ?><b>Username:</b> <span class=class1><a href="aeditprofile.php?username=<? echo $row['Username'] ?>"><? echo $row['Username'] ?></a></span><br>
<b>Group:</b> <? echo $row['Level'] ?><br /><hr>
<?php
}
?>

 

 

Link to comment
Share on other sites

To further explain my previous message:

 

This is my current code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $editEMAIL=$_POST['email'];
        $editLOCATION=$_POST['location'];
        $editWEBSITE=$_POST['website'];
        $editABOUT=$_POST['about'];
        $editID=$_POST['id'];
        $editLEVEL=$_POST['level']; 
        if ($editLEVEL == 'Registered User') {
    $editLEVEL = "<font color=purple><b>$editLEVEL</b></color>";
} else if ($editLEVEL == 'EventSec Employee') {
    $editLEVEL = "<font color=green><b>$editLEVEL</b></color>";
} else if ($editLEVEL == 'Admin') {
    $editLEVEL = "<font color=red><b>$editLEVEL</b></color>";
}
        $editquery = mysql_query("UPDATE users SET EmailAddress='$editEMAIL' , Level='$editLEVEL' , Location='$editLOCATION', Website='$editWEBSITE', About='$editABOUT' WHERE UserID='$editID'");
        if($editquery) {
            echo "<b>Success!</b><br>";
            echo "Your profile was successfully updated. Please click<span class=\"class1\"><a href=\"index.php\"> here </a></span>to return to the index.<br><br>";
            $display_form = FALSE;
        } else {
            echo "<b>Error</b>";
            echo "<p>Sorry, your profile update failed. Please go back and try again.</p>";
            $display_form = TRUE;
        }
    }
}

 

When I add:

<? echo $row['Level']; ?>

 

All text after that is the same colour as the users level and in the entry code the second elseif statement doesn't insert the html.

 

How do I fix it?

 

 

Link to comment
Share on other sites

You know I didn't even realise my tag mistake...lack of sleep I'm sure.

 

As for your second question, I wouldn't know how to. I can only just about create a variable...god knows how I got as far as I did on my site, alot of questions and a lot more trial and error.

 

When its done I still have to make it as secure as I can, something I currently have nightmares about O.o Makes my eyes funny just reading about the different ways of securing php/databases, nevermind trying to do it.

 

So what would I do to sort out my middle elseif statement aswell, it doesnt execute...only the first and 3rd do?...what have I done wrong there?

 

Cheers btw!

Link to comment
Share on other sites

To be honest, using an ID for user level is better but it's not required and I really think it will just serve to confuse you even more. You need to focus on something far more important right now: security.

 

You're not escaping any of the input. Even putting this at the top of your script will work:

 

foreach($_POST as $key => $val){

  $_POST[$key] = mysql_real_escape_string($val);

}

 

This will escape all post data for you by default. When you get more accustomed to effectively managing user input you can customize to better suit your needs.

 

Maybe look into prepared statements. I wish I'd started using them when I first started. They're as simple as what you're doing now but far more secure.

Link to comment
Share on other sites

if i understand what ur after then i did something similer a few weeks back i used css to change background colour. easy to change to what ever u want though.

$bgc is what i want the class to be.

 

if    ($rows['auth'] == 'YES' and $rows['location'] == 'SERVER'){ 
$bgc = "pass" ; } 

elseif($rows['auth'] == 'YES' and $rows['location'] == 'HDD'){ 
$bgc = "wait" ;  

}else{
$bgc = "fail"; }

?>
<tr id="tabledata" class="<?php echo "$bgc"; ?>">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.