Results 1 to 5 of 5

Thread: For Statement Question

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Statement Question

    Well, I am running a for statement that show numbers up to the rank of the user viewing it. Here is the code:
    PHP Code:
    <?php

    $id 
    $_GET['id'];
    $level $_SESSION['level'];

    // Get User Data
    $get mysql_query("SELECT * FROM `members` WHERE id = '$id' LIMIT 1") or die ("Error Getting User Data! \n<br />\n" .mysql_error());
    $u mysql_fetch_array($get);
    $chk mysql_num_rows($get);
    //

    // Check Levels
    if ($level <= $u['level']) { echo('<meta http-equiv="refresh" content="2;URL=index.php" /> <div class="error">You do not have permission to edit this user</div>'); }
    else {
    //

        ########## If Form Sent ##########
        
    if ($_POST['edit']) {
            
    $username mysql_real_escape_string($_POST['username']);
            
    $map mysql_real_escape_string($_POST['map']);
            
    $level mysql_real_escape_string($_POST['level']);
            
            
    // Format Username
            
    if ( !eregi('[IG]'$username) ) { $username $username.'[IG]'; }
            if ( 
    ereg('[ig]'$username) ) { $username str_replace('[ig]''[IG]'$username); }
            
    //
            
            // Insert Data
            
    $insert mysql_query("UPDATE `members` SET username = '$username', map = '$map', level = '$level' WHERE id = '$id'") or die ("Error Updating User Data! \n<br />\n" .mysql_error());
                echo(
    '<meta http-equiv="refresh" content="2;URL=index.php" /> <div class="message">The user '.$username.' has been edited.</div>');
            
    //
        
    }

        
    ########## If Form Not Sent ##########
        
    if (!$_POST['edit']) {
        
            
    // Check Results
            
    if ($chk 1) { echo('<meta http-equiv="refresh" content="2;URL=index.php" /> <div class="error">Invalid User ID!</div>'); } else { ?>
        
                <form method="post" action="">
                    <table width="400" align="center">
                        <tr>
                            <td>Username:</td>
                            <td><input type="text" name="username" value="<?php echo $u['username'?>" /></td>
                        </tr>
                        <tr>
                            <td>Test Map:</td>
                            <td><input type="text" name="map" value="<?php echo $u['map'?>" /></td>
                        </tr>
    <?php if ($level >= 8) { ?>
                        <tr>
                            <td>Rank Level:</td>
                            <td>
                                <select name="rank">
    <?php for ($r=1$r $level$r++) { while ($r == $u['level']) { $s 'selected="selected"'; } ?>
                                    <option <?php echo $s ?>><?php echo $r ?></option>
    <? ?>
                                </select>
                            </td>
                        </tr>
    <? ?>
                        <tr>
                            <td></td>
                            <td><input type="submit" name="edit" value="Edit" /></td>
                        </tr>
                    </table>
                </form>
        
        <? 
        }
    }

    ?>
    The numbers are the ranks that the viewing user is allowed to promote another member to. The problem is that I need the member's (the one getting promoted/demoted) rank to be initially selected. Instead of just adding the 'selected="selected"' to the members rank, it adds it to every number after it finds the members rank because $s is now set. How can I make this work? Thanks in advance.
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    I'm not 100&#37; on this without testing, but I think you may want to do an if statement instead of while on this line:

    Code:
    <?php for ($r=1; $r < $level; $r++) { while ($r == $u['level']) { $s = 'selected="selected"'; } ?>
    so it would look something like this:

    Code:
    <?php for ($r=1; $r < $level; $r++) { if ($r == $u['level']) { $s = 'selected="selected"'; } ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I originally tried the if instead of the while, but it didn't work. I think the reason for this is that once the script finds the point where $r == $u['level'] is true, it will read it as true for every following run through. Any ideas on what to do? Thanks
    Thanks DD, you saved me countless times

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <?php for ($r=1; $r < $level; $r++) { ?> 
      <option <?php echo $r === u['level'] ? 'selected="selected"' : '' ?>><?php echo $r ?></option> 
    <? } ?>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, thats made it work
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •