Drewdle Posted January 17, 2011 Share Posted January 17, 2011 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. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 17, 2011 Share Posted January 17, 2011 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>"; } Quote Link to comment Share on other sites More sharing options...
BLaZuRE Posted January 17, 2011 Share Posted January 17, 2011 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 Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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 } ?> Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 18, 2011 Share Posted January 18, 2011 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?? use switch http://php.net/manual/en/control-structures.switch.php Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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? Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 18, 2011 Share Posted January 18, 2011 change </color> to </font> and your good Quote Link to comment Share on other sites More sharing options...
crabfinger Posted January 18, 2011 Share Posted January 18, 2011 on another note why not add the numeric level to the database and interpret it on the other end? saves for database size Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 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! Quote Link to comment Share on other sites More sharing options...
Drewdle Posted January 18, 2011 Author Share Posted January 18, 2011 Grr, once again the error was on my part. I had a lowercase where I needed a capital letter. Still, I wouldnt mind learning to do this using the numeric way you mentioned if you wouldn't mind teaching me? Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted January 18, 2011 Share Posted January 18, 2011 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. Quote Link to comment Share on other sites More sharing options...
hyster Posted January 18, 2011 Share Posted January 18, 2011 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"; ?>"> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.