SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36

Thread: Array Copying

  1. #26
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to use the ID's to number the records at a later date, but if the ID's have gaps in them, then the numbering won't reflect the number of records stored inside the table.

    How do I re-number the records after deletion?

  2. #27
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why do you need the ids to reflect the total number of records? If you need the total number of records, do 'select count(*) from birds'. Or if you want to number the displayed results, generate them in php once you output, or display the records in an ordered list (<ol>).

    For example:
    Code php:
    $i=0;
    while($row = mysql_fetch_object($result))
    {
      echo ++$i.'<br>';
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
      echo "<input name='id[]' type='checkbox' value='".$row->id."'><br>";
    }
    or
    Code php:
    echo '<ol>';
    while($row = mysql_fetch_object($result))
    {
      echo '<li>';
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
      echo "<input name='id[]' type='checkbox' value='".$row->id."'><br>";
      echo '</li>';
    }
    echo '</ol>';
    Saul

  3. #28
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, never mind. How do I stop the browsers Back and Forward buttons becoming enabled once a record has been deleted, in order to avoid users navigate back and display data that is no longer stored inside the table?

  4. #29
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You cannot. But a good idea is to do a redirect after deleting the records, to prevent 'page expired' error.

    Code php:
    header('Location: http://www.example.com/page.php');
    Saul

  5. #30
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    You cannot. But a good idea is to do a redirect after deleting the records, to prevent 'page expired' error.
    Code:
    header('Location: http://www.example.com/page.php');
    Where abouts do I put that line in the code?

  6. #31
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I display a "Yes" and "No" button upon the delete button being clicked, in order for the user to confirm where or not the record should be deleted?

  7. #32
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salchester View Post
    Where abouts do I put that line in the code?
    At the end of the script, after you delete the records and no other output is to be made.
    Saul

  8. #33
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salchester View Post
    How do I display a "Yes" and "No" button upon the delete button being clicked, in order for the user to confirm where or not the record should be deleted?
    The easiest way is to use JavaScript:
    Code HTML4Strict:
    <input type="submit" onclick="return confirm('Are you sure you want to delete the selected records?')" value="Delete" />
    Saul

  9. #34
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Copying

    Can you comment the following code in order for me to get to grips with what's going on, and how the code works?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
     
    mysql_select_db("WB", $con);
     
    if(isset($_POST['delete']))
    {
      if(is_array($_POST['id']))
      {
        foreach($_POST['id'] as $id)
        {
          $list=((int)$id).',';
        }
        $list=substr($list,0,-1);
      }
      else
      {
        $list=(int)$_POST['id'];
      }
     
      $query="delete from Birds where id in ($list)";
      mysql_query($query) or die(mysql_error());
    }
     
    $result = mysql_query("SELECT * FROM Birds");
     
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
      echo "<input name='id[]' type='checkbox' value='".$row->id."'><br>";
    }
    echo "<input type='submit' name='delete' value='Delete'><br></form>";
    ?>

  10. #35
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
     
    mysql_select_db("WB", $con);
     
    if(isset($_POST['delete'])) //if the Delete button is pressed
    {
      if(is_array($_POST['id'])) //if there are more than one record selected
      {
        foreach($_POST['id'] as $id)//go through each selected record
        {
          $list=((int)$id).',';  //create a comma separated list of ids
        }
        $list=substr($list,0,-1); //remove the last comma
      }
      else
      {
        $list=(int)$_POST['id']; //one id, keep it. (int) as above works as user input validation -- forcing integer type
      }
     
      $query="delete from Birds where id in ($list)"; //create the query
      mysql_query($query) or die(mysql_error()); //run the query
    }
     
    $result = mysql_query("SELECT * FROM Birds");
     
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
      echo "<input name='id[]' type='checkbox' value='".$row->id."'><br>"; //id[] works as an array -- all values of selected checkboxes named id[] are added to an array
    }
    echo "<input type='submit' name='delete' value='Delete'><br></form>";
    ?>
    Saul

  11. #36
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Copying

    How do I do the following without using array?

    Code PHP:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
     
    mysql_select_db("WB", $con);
     
    if(isset($_POST['delete'])){
      if(is_array($_POST['id'])){
        foreach($_POST['id'] as $id){
          $list=((int)$id).',';
        }
        $list=substr($list,0,-1);
      }else{
        $list=(int)$_POST['id'];
      }
     
      $query="delete from Birds where id in ($list)";
      mysql_query($query) or die(mysql_error());
    }
     
    $result = mysql_query("SELECT * FROM Birds");
     
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";
      echo $row->Sex . "<br>";
      echo "<input name='id[]' type='checkbox' value='".$row->id."'><br>";
    }
    echo "<input type='submit' name='delete' value='Delete'><br></form>";
    ?>


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
  •