SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 36

Thread: Array Copying

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

    Array Copying

    Howdy,

    How do I copy only data stored at locations 0,2,3,5,8 from one array to another, upon clicking a button?
    Can all the processing be done on the same page?

    Many Thanks,

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you know those locations (i assume location means array indexes)?? I dont know some advanced ways but if you have those locations known and stored in some variables then i think you can easily transfer the values one to another using foreach or for loops.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well...
    Code php:
    $array2[0]=$array1[0];
    $array2[2]=$array1[2];
    $array2[3]=$array1[3];
    $array2[5]=$array1[5];
    $array2[8]=$array1[8];
    Saul

  4. #4
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I store the array indexes for $array1 inside a variable, and display all the data in each array index onto the screen, along with a delete button. Upon a button being clicked, how can I log the array index for that record, and copy all records except from the logged one into $array2?

    Can all the processing be done on the same page?

    Hope this makes sense?

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK see this code:
    PHP Code:
    $array1 = array('1','2');
    $array2 = array();
    for(
    $i=0;$i<count($array1);$i++){
        
    $array2[] = $array[$i];

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I do the above as mention in Post 4, whilst using php_daemon's code (Post 3)?

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sorry but that doesn't make much sense. What exactly are you trying to do, in terms of the actual problem?
    Saul

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

    Array Copying

    Quote Originally Posted by php_daemon View Post
    Sorry but that doesn't make much sense. What exactly are you trying to do, in terms of the actual problem?
    Sorry it's hard to explain...

    I have now come up with the following code, that displays all records inside the array ($records) along with buttons.

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    $line = 0;
    
    $records = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex;
      $line++;
    }
    ?>
    Upon a record being selected by clicking on a button, how do I copy all but the selected record inside a second array ($temp)?

    Can all the processing be done on the same page?

    Hope this makes sense, Let Me Know?
    Last edited by Salchester; Jul 2, 2007 at 05:51.

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

    Array Copying

    Quote Originally Posted by php_daemon View Post
    Sorry but that doesn't make much sense. What exactly are you trying to do, in terms of the actual problem?
    Basically I'm trying to delete a record from an array, but not from the table. The only way I can think of to delete a record, is to copy all but the record that needs to be deleted into a second array called ($temp)

    Am i write, or is there a better more easier way?

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can simply use unset(), the remove an element from an array:

    PHP Code:
    unset($records[2]); 
    Saul

  11. #11
    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 can simply use unset(), the remove an element from an array:

    PHP Code:
    unset($records[2]); 
    How do I delete the appropriate element from the array, upon the appropriate button being clicked?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    $line = 0;
    $records = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex;
      echo "<input type='submit' name='Submit' value='Delete Record $line'>";
      $line++;
    
    }
    
    ?>

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Things you can do:

    Use different forms for every record and have a hidden field with the index.

    Use JavaScript to change a hidden field value to the index of the button clicked.

    Use image buttons with indices as values.

    Use checkboxes to select records.

    Extract the index from submit button value using string functions.
    Saul

  13. #13
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Things you can do:

    Use different forms for every record and have a hidden field with the index.

    Use JavaScript to change a hidden field value to the index of the button clicked.

    Use image buttons with indices as values.

    Use checkboxes to select records.

    Extract the index from submit button value using string functions.
    How do you do it using check boxes to select records, and a submit button, to delete the selected records?

    Many Thanks,

  14. #14
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Using the snippet from your code,

    Code php:
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
     
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex;
      echo '<input type="checkbox" value="'.$line.'" name="id[]" />';
      $line++;
     
    }
    echo "<input type='submit' name='Submit' value='Delete Selected'>";

    $_POST['id'] will be an array with the selected record indices.

    Still some things do not add up for me. For all this to make sense, you have to read same data in same order to an array when handling the form submission. Why do you not delete the records from the database directly?
    Saul

  15. #15
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll explain in a sec,

    Can you show me the end result with comments so I can try and understand how it works?
    As I'm getting a bit confused at this end.

  16. #16
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Still some things do not add up for me. For all this to make sense, you have to read same data in same order to an array when handling the form submission. Why do you not delete the records from the database directly?
    Good Point,

    I currently have the following code that outputs all information contained in a table to the screen, whilst displaying a check box beside each one.

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    while($row = mysql_fetch_object($result))
    {
      echo $row->Name . "<br>";
      echo $row->Species . "<br>";;
      echo $row->Sex . "<br>";;
      echo "<input name='' type='checkbox' value=''><br>";
    }
    echo "<input type='submit' name='Submit' value='Delete'><br>";
    ?>
    Upon a record being selected and the submit button being clicked, how do I use a Query String to delete a record using an ID, but then re-number the ID column to reflect the number of records stored in the table?

  17. #17
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use the primary key column. Assuming it is named 'id',

    Code php:
    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>";
    }

    And delete the selected records:
    Code php:
    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)";
    Saul

  18. #18
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I put the two pieces of code together?
    Last edited by Salchester; Jul 2, 2007 at 09:55.

  19. #19
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Firstly, your form script isn't complete (I don't see the form tags). It might go like this:
    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>";
    ?>
    Saul

  20. #20
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do I get the ID column to automatically increment?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    // Create database
    if (mysql_query("CREATE DATABASE WB",$con))
    {
      echo "Database created!";
    }
    else
    {
      echo "Error creating database: " . mysql_error();
    }
    
    // Create table in WB database
    mysql_select_db("WB", $con);
    $sql = "CREATE TABLE Birds 
    (
      ID int,
      Name text,
      Species text,
      Sex text
    )";
    mysql_query($sql,$con);
    
    mysql_close($con);
    ?>

  21. #21
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $sql = "CREATE TABLE Birds 
    (
      ID int not null auto_increment,
      Name text,
      Species text,
      Sex text,
      primary key(id)
    )";
    Saul

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

    Array Copying

    How do I re-number (re-ID) the records stored in the table, upon deleting a record?
    There is a method in my madness!!!

  23. #23
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is absolutely no need to do that. All you need to now about the ID column is that it's auto incremented and the IDs are never repeated, thus unique.
    Saul

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

    Array Copying

    Quote Originally Posted by php_daemon View Post
    There is absolutely no need to do that. All you need to now about the ID column is that it's auto incremented and the IDs are never repeated, thus unique.
    I know, but how would I go about doing it anyway, as it will be used later on during development.
    There is a method in my madness!

  25. #25
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is no practical use for that and it is really a bad idea. Prove me I'm wrong and I will show you how could you do that.
    Saul


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
  •