Jump to content

[SOLVED] if statement help


penguin0

Recommended Posts

I am currently making an edit user page, and it all works so far.  One problem is I have it echo a user's permissions in radio buttons, with an if statement.

 

uusers is a variable printing if the user being edited has the perm "users" and so on.

 

Look for this part of the code: 

<tr><td>Regular User?</td><td><input type="radio" name="users" value="1"<? if ($uusers == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="users" value="0" <? if ($uusers == "0") { echo " checked=\"1\""; } ?>> No</td></tr>

 

The problem is if the user manager clicks the perm something other than the one that is selected it will not save, because I have it echoing checked=1 so it will show the users perms.  Anyone know another if statement I can put in for if a perm is changed, so it will update?

 

All the code:

 

<?
include("header2.php");
?>     
<?
$result = mysql_query( "SELECT content FROM pages WHERE number = 4" );
$num_rows = mysql_num_rows( $result );
while ( $a_row = mysql_fetch_row( $result ) ) {
   foreach ( $a_row as $field )
       print "$field";
}
?>  

<?
$userid = intval($_REQUEST['id']);
$sql = "SELECT id, name, position, username, email, admin, pageman, userman, rateman, menuman, users, created FROM users WHERE id = $userid";
$result = mysql_query($sql, $link) or die(mysql_error());
while ( $a_row = mysql_fetch_array( $result ) ) {

$id = $a_row['id'];
$name = $a_row['name'];
$username = $a_row['username'];
$position = $a_row['position'];
$uadmin = $a_row['admin'];
$upageman = $a_row['pageman'];
$uuserman = $a_row['userman'];
$urateman = $a_row['rateman'];
$umenuman = $a_row['menuman'];
$uusers = $a_row['users'];
$email = $a_row['email'];
$created = $a_row['created'];
}
?>
<form action="do_edituser.php" method="POST">
<table cellpadding=2 cellspacing=1 class=ptable><tr><th colspan=2>Edit User: <? echo "$name"; ?></th></tr>
<tr><td>Name:</td><td><input type="text" name="name" size="25" maxlength="25" value="<? echo "$name"; ?>"></td></tr>
<tr><td>Position:</td><td><input type="text" name="position" size="25" maxlength="35" value="<? echo "$position"; ?>"></td></tr>
<tr><td>Username:</td><td><input type="text" name="username" size="25" maxlength="25" value="<? echo "$username"; ?>"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" size="35" maxlength="50" value="<? echo "$email"; ?>"></td></tr>
<tr><td>Created:</td><td><? echo "$created"; ?></td></tr>

<tr><th colspan=2>Edit Permissions</th></tr>
<input type="hidden" name="id" value="<? echo "$_POST[id]"; ?>">

<tr><td>Is Admin?</td><td><input type="radio" name="admin" value="1" <? if ($uadmin == "1") { echo "checked=\"1\""; } ?>> Yes <input type="radio" name="admin" value="0"<? if ($uadmin == "0") { echo " checked=\"1\""; } ?>> No</td></tr>
<tr><td>Edit Pages?</td><td><input type="radio" name="pageman" value="1"<? if ($upageman == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="pageman" value="0"<? if ($upageman == "0") { echo " checked=\"1\""; } ?>"> No</td></tr>


<tr><td>Edit Menus?</td><td><input type="radio" name="menuman" value="1"<? if ($umenuman == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="menuman" value="0"<? if ($umenuman == "0") { echo " checked=\"1\""; } ?>> No</td></tr>

<tr><td>Approve Rates?</td><td><input type="radio" name="rateman" value="1"<? if ($urateman == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="rateman" value="0"<? if ($urateman == "0") { echo " checked=\"1\""; } ?>> No</td></tr>

<tr><td>Edit Users?</td><td><input type="radio" name="userman" value="1"<? if ($uuserman == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="userman" value="0"<? if ($uuserman == "0") { echo " checked=\"1\""; } ?>> No</td></tr>

<tr><td>Regular User?</td><td><input type="radio" name="users" value="1"<? if ($uusers == "1") { echo " checked=\"1\""; } ?>> Yes <input type="radio" name="users" value="0" <? if ($uusers == "0") { echo " checked=\"1\""; } ?>> No</td></tr>

<tr><td align=center colspan=2><input type="submit" name="submit" value="Update User"></td></tr></table>
</form>
</td></tr>             
<?
include("footer.php");
?>

Link to comment
Share on other sites

hey sorry but u got some html elementry errors

 

1 i dont think u can use checked="1" instead use checked="checked"

2 i dont really see you grouping any of the radio buttons. try an group them by label tags like so

 

<label><input type=radio />yes <input type=radio checked=checked /> No</label>

 

3 you got a php problem...

on the line that says

...checked=1 <? if ....

 

its a bit wrong because you have already told it that it should be checked and you can't remove it

 

 

 

4 i just think your missing out on the label tags which you dont really need to use php to check and uncheck them.

 

5 hope it helped somehow

Link to comment
Share on other sites

The way it was before was correct html, except for the checked = 1, but It showed correctly.  That was not my issue.  My issue is when I change the value from yes to no or from no to yes, it wont update.  I used the if statement to find in the db if the user has the perm or not, and show it when you go to edit them.  Anyone know how I can change my if statement to reflect the change before it saves?

 

updated code:

<tr><td>Is Admin?</td><td><label><input type="radio" name="admin" value="1" <? if ($uadmin == "1") { echo "checked=\"checked\""; } ?>/> Yes <input type="radio" name="admin" value="0"<? if ($uadmin == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Pages?</td><td><label><input type="radio" name="pageman" value="1"<? if ($upageman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="pageman" value="0"<? if ($upageman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Menus?</td><td><label><input type="radio" name="menuman" value="1"<? if ($umenuman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="menuman" value="0"<? if ($umenuman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Approve Rates?</td><td><label><input type="radio" name="rateman" value="1"<? if ($urateman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="rateman" value="0"<? if ($urateman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Users?</td><td><label><input type="radio" name="userman" value="1"<? if ($uuserman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="userman" value="0"<? if ($uuserman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Regular User?</td><td><label><input type="radio" name="users" value="1"<? if ($uusers == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="users" value="0" <? if ($uusers == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

Link to comment
Share on other sites

Not sure what you mean by "before it saves".  If you change the value, then click update, and it doesn't update, that generally means that there is a problem with the update script, not the display script.

 

On a different note, you can also use the ternary operator to do the "checking" of radio buttons...

 

<tr>
<td>Is Admin?</td>
<td>
	<label>
		<input type="radio" name="admin" value="1" <? echo ($uadmin == 1) ? 'checked="checked"' : ''; ?> /> Yes 
		<input type="radio" name="admin" value="0" <? echo ($uadmin == 0) ? 'checked="checked"' : ''; ?> /> No
	</label>
</td>
</tr>

Link to comment
Share on other sites

I know that but all the html on the page gets echo if the user has the perm to edit users (if not the user gets an acces denied).  So all this needs to be echoed:

 

<tr><td>Is Admin?</td><td><label><input type="radio" name="admin" value="1" <? if ($uadmin == "1") { echo "checked=\"checked\""; } ?>/> Yes <input type="radio" name="admin" value="0"<? if ($uadmin == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Pages?</td><td><label><input type="radio" name="pageman" value="1"<? if ($upageman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="pageman" value="0"<? if ($upageman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Menus?</td><td><label><input type="radio" name="menuman" value="1"<? if ($umenuman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="menuman" value="0"<? if ($umenuman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Approve Rates?</td><td><label><input type="radio" name="rateman" value="1"<? if ($urateman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="rateman" value="0"<? if ($urateman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Edit Users?</td><td><label><input type="radio" name="userman" value="1"<? if ($uuserman == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="userman" value="0"<? if ($uuserman == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

<tr><td>Regular User?</td><td><label><input type="radio" name="users" value="1"<? if ($uusers == "1") { echo " checked=\"checked\""; } ?>/> Yes <input type="radio" name="users" value="0" <? if ($uusers == "0") { echo " checked=\"checked\""; } ?>/> No</label></td></tr>

Link to comment
Share on other sites

echo '
<tr>
	<td>Is Admin?</td>
	<td>
		<label>
			<input type="radio" name="admin" value="1" ' .  ($uadmin == 1 ? 'checked="checked"' : '') . ' /> Yes 
			<input type="radio" name="admin" value="0" ' .  ($uadmin == 0 ? 'checked="checked"' : '') . ' /> No
		</label>
	</td>
</tr>
....and so on for the others....';

Link to comment
Share on other sites

well it echoed everything correctly, even the perms the user already has, but when I click submit it wont update the perm.

 

I have the second page that it shows what was updated and (i changed userman from 0 to 1:

 

Admin perm? 0

Page perm? 0

User perm? 1

Rate perm? 0

Menu perm? 0

Reg perm? 1

 

it posted like it updated, but didn't.

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.