#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Setting pointer in a while do loop to the current value


    I'm having a bit of trouble maintaining the value of a variable within a while do loop. It seems the pointer keeps pointing to the first id no matter how many loops there are. I've inserted a hidden field which pulls in each id with shows the IDs are fine but whenever I click on 'editForm' anchor it always points to either the first or last id in the loop, (depending on whether I put the editForm div tag before or after the loop). Can anyone out there help me fix this problem? The code is shown below:

    [code]
    <script>
    $('.formEditAnchor').click(function() {
    $('#editMyForm').submit();
    });
    </script>


    <?php

    function editFunction($editID) {

    $database = new database();

    $res = $database->selectDB("select content as EditContent from content where id = '$editID'");

    $editForm = "<form method='POST' action='_scripts/_php/edit-file.php'>";
    $editForm.= "<textarea name='saveContent' cols='70' rows='25'>";

    while($row = mysql_fetch_array($res)) {
    $row = mysql_fetch_array($res);

    $editableFile = $row['EditContent'];
    $editableContent = file($editableFile);
    $editableString = implode($editableContent);

    }

    $editForm.= $editableString;

    $editForm.= "</textarea>";
    $editForm.= "<input type='hidden' name='hiddenEditPath' value='<?php echo $editableFile; ?>' />";
    $editForm.= "<br />";
    $editForm.= "<input type='submit' name='saveFile' value='Save'>";
    $editForm.= "</form>";

    return $editForm;
    }

    function submitEditForm() {

    if (isset($_POST['formEditAnchor'])) {

    $usersReplyId = $_POST['hiddenUserReplyID'];
    $returnEdit = editFunction($usersReplyId);

    return $returnEdit;

    }
    }

    $result3 = $database->selectDB("select * from blog_content where content_id = '$id'");

    $r = array();
    while($rows = mysql_fetch_array($result3)) {

    $myID = $rows['id'];
    $file = $rows['content'];
    $contents = file($file);
    $string = implode($contents);

    ?>

    <form name="editMyForm" id="editMyForm" method="post" action"<?php echo $_SERVER["PHP_SELF"]?>">
    <input type="hidden" name="hiddenUserReplyID" id="hiddenUserReplyID" value="<?php echo $myID ?>" />
    <table>
    <tr>
    <tr>
    <td bgcolor="#F8F7F1" align="center">Date/Time:<?php echo $rows['datetime']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#f8f7f1"><?php

    echo $string;
    echo "<br /><br />";

    ?>
    </td>
    </tr>
    <tr>
    <td><a href="#editContent" class="create-content" id="formEditAnchor" onclick="<?php $myReturnEdit = submitEditForm(); ?>">Edit File</a></td>
    </tr>
    </tr>
    </table>
    </form>

    <?php

    }

    ?>

    <div id='editContent' class='create-topic'>
    <?php
    echo $myReturnEdit;
    ?>
    </div>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    1) Please edit your post an wrap your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    2) Do not use the deprecated MySQL extensions, switch to PDO.

    3) Your while loop is just setting variables. Each pass will overwrite the previous value until the loop exits. At that point the variables will have the value of the last pass (or the first if there is only 1 row returned).

    Comments on this post

    • dmittner agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    As long as your using a decent text editor, use the TextDiff feature to compare ur version to this one...
    PHP Code:
    <script>
      $('.formEditAnchor').click(function() {
        $('#editMyForm').submit();
      });
    </script>
    <?php

      
    function editFunction($editID) {
        
    $database = new database();
        
    $res $database->selectDB("select content as EditContent from content where id = '$editID'");
        
    $editForm "<form method='POST' action='_scripts/_php/edit-file.php'>";
        
    $editForm.= "<textarea name='saveContent' cols='70' rows='25'>";
        while(
    $row mysql_fetch_array($res)) {
          
    $row mysql_fetch_array($res);
          
    $editableFile $row['EditContent'];
          
    $editableContent file($editableFile);
          
    $editableString implode($editableContent);
        }
        
    $editForm.= $editableString;
        
    $editForm.= "</textarea>";
        
    $editForm.= "<input type='hidden' name='hiddenEditPath' value='" $editableFile "' />";
        
    $editForm.= "<br />";
        
    $editForm.= "<input type='submit' name='saveFile' value='Save'>"
        
    $editForm.= "</form>";
        return 
    $editForm;
      }
      
      function 
    submitEditForm() {
        if (isset(
    $_POST['formEditAnchor'])) {
          
    $usersReplyId $_POST['hiddenUserReplyID'];
          
    $returnEdit editFunction($usersReplyId);
          return 
    $returnEdit;
        }
      }
      
      
    $result3 $database->selectDB("select * from blog_content where content_id = '$id'");
      
    $r = array(); 
      while(
    $rows mysql_fetch_array($result3)) {
        
    $myID $rows['id'];
        
    $file $rows['content'];
        
    $contents file($file);
        
    $string implode($contents);
    ?>
     
    <form name="editMyForm" id="editMyForm" method="post" action"<?php echo $_SERVER["PHP_SELF"]; ?>">
      <input type="hidden" name="hiddenUserReplyID" id="hiddenUserReplyID" value="<?php echo $myID?>" />
      <table>
        <tr>
          <td bgcolor="#F8F7F1" align="center">Date/Time:<?php echo $rows['datetime']; ?></td>
        </tr>
        <tr>
          <td bgcolor="#f8f7f1"><?php echo $string "<br /><br />"?></td>
        </tr>
        <tr>
          <td><a href="#editContent" class="create-content" id="formEditAnchor" onclick="<?php $myReturnEdit submitEditForm(); ?>">Edit File</a></td>
        </tr>
      </table>
    </form>
    <?php
      
    }
    ?>
    <div id='editContent' class='create-topic'>
      <?php echo $myReturnEdit?>
    </div>
    Last edited by Triple_Nothing; July 8th, 2013 at 12:06 PM.

IMN logo majestic logo threadwatch logo seochat tools logo