SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 40
  1. #1
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Manipulation

    Hiya Everyone,

    Using the following code, how do I load all information from the database into an array ($records), and display each record on screen with it's own delete button.

    Then when a delete button is clicked, all records NOT including the record that had the button clicked should get copied into a separate array ($temp).

    Then all record from the $temp array should get copied into the database, overwriting everything that was there previously.

    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");
    }
    
    $records = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    }
    
    // Loop the following code
    
    echo $records[0]->Name;
    echo $records[0]->Species;
    echo $records[0]->Sex;
    ?>
    How can this be done?

    Is there anyway of using a loop and a counter variable to display all the records, instead of manual declaring the "Numeric Array Store" value etc.

    Many Thanks,

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why can't you just display your records inside your while loop?
    PHP Code:
    while($row mysql_fetch_object($result))
    {
        echo 
    $row->Name;
        echo 
    $row->Species;
        echo 
    $row->Sex;

    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo 
    "<table>\n";
    while(
    $row mysql_fetch_object($result)) {
        echo 
    "    <tr>\n";
        echo 
    "        <td>" .$row->Name"</td>";
        echo 
    "        <td>" .$row->Species"</td>>";
        echo 
    "        <td>" .$row->Sex"</td>";
        echo 
    "        <td><input type='checkbox' name= '" .$row->Name"' /></td>\n";
        echo 
    "    </tr>\n";
    }
    echo 
    "    <tr>\n";
    echo 
    "          <td><input type="submit" value="Submit" /></td>\n";
    echo 
    "    </tr>\n";
    echo 
    "</table>\n";
    echo 
    "</form>\n"
    Your post variables array will contain the name of the birds to delete, simply loop through the $_POST array and delete all keys except for submit.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron/Kromey,

    I finally got all records in the array to be displayed, by putting a counter variable as the "Numeric Array Store" value.

    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();
    $temp = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex. "<br />";
      $line++;
    }
    ?>
    How do I now add a button to each of the records displayed, then when a delete button is clicked, copy all but the clicked record into a separate array called temp.

    THE REASON FOR NOT TAKING THE INFORMATION STRAIGHT FROM THE DATABASE/TABLE AND DELETING IT, IS DUE TO THE FACT THAT THE ARRAYS WILL BE USED FOR FUTURE FUNCTIONALITY.

    Many Thanks,

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the line number for your checkbox value, in your form handler simply unset the array element that corresponds to the value. You will have to pass the array to the form handler, consider a SESSION variable.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    I currently have the following code, but it doesn't seem to be working correctly:
    Can you help? Any changes, please emphasize!!!
    By all means change the buttons to check boxes, I wanted to do that later anyways.

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    $item = 0;
    $line = 0;
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    $records = array();
    $temp = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex. "<br />";
      $line++;
      
      echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">\n";
      echo "<input type=\"hidden\" name=\"remove\" value=\"". $line ."\" />\n";  
      echo "<input type=\"submit\" name=\"sumbit\" value=\"" . $line . "\" />\n";
      if(isset($_POST['remove']))
      {
        if ($item = $line)
        {
          $item++;
        }
      }
      else
      {
        echo $temp[0]->Name;
      }
    
      echo "</form>\n";
    }
    
    ?>
    Many Thanks,

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $i 0;

    echo 
    "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo 
    "<table>\n";
    foreach( 
    $records as $k  ) {
        echo 
    "    <tr>\n";
        echo 
    "        <td>" .$k->Name"</td>";
        echo 
    "        <td>" .$k->Species"</td>>";
        echo 
    "        <td>" .$k->Sex"</td>";
        echo 
    "        <td><input type='checkbox' name='remove[" .$k"]' value=" .$k" /></td>\n";
        echo 
    "    </tr>\n";
        
    $i++;
    }
    echo 
    "    <tr>\n";
    echo 
    "          <td><input type="submit" value="Submit" /></td>\n";
    echo 
    "    </tr>\n";
    echo 
    "</table>\n";
    echo 
    "</form>\n"
    Last edited by byron3@earthlink; Jun 28, 2007 at 13:37.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    May I ask, what have you done.
    What will "frm_handler.php" contain?

    How do I copy information from one array ($records) to another array ($temp)?

    Many Thanks,

  9. #9
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Salchester your POST data will go to a piece of php code called frm_Handler. This code does not need to display anything, it simply catches your POST information and does the processing. Once you have filled your initial array $records store it in a SESSION variable.
    In your form handler code you will need to unset the elements in the $records array which have been marked for deletion. Need to update your database this would be a good time, unset an element and delete the corresponding record.
    If you want to keep a copy of both, clone your array and unset rows in your copied array.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    How do I do all of that?
    How do I get the code to the stage that its asks you if you want to delete the selected records, and then deletes them?

    Many Thanks,

  11. #11
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One line of code @ a time.

    If you are going to use session variable you will need a session_start() @ the start of each code page.

    Oh and to fill in the last piece of the puzzle, once you have finished processing your data you can use the header() to return to the page you posted your data from.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    How do I copy information from one array to another?
    I'm still unsure on what goes frm_handler.php, and how to do it?

    Can you give me examples?

    Many Thanks,

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

    Array Manipulation

    Byron,

    I have managed to put the following code together, though I need all records except the deleted one to be copied to another array ($temp), instead of deleting it from the table/database.

    Also, I wanted the ID to be the "Numeric Array Store" value not Name.

    How come when the "Delete" button is clicked nothing happens except the page refreshing?
    The record doesn't get deleted from the table.

    What needs to be done/changed in order for it to do what I mention in the first two paragraphs?

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    
    if(isset($_POST['remove']))
    {
        mysql_query("DELETE FROM Birds WHERE Name=".$_POST['remove']);
    }
     
    $result = mysql_query("SELECT * FROM Birds");
     
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
     
    $records = array();
     
    while($row = mysql_fetch_assoc($result))
    {
        echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">\n";
    
        echo "Name: ".$row['Name'] . "<br/>";
        echo "Species: ".$row['Species']. "<br/>";
        echo "Sex: ".$row['Sex'];
     
        echo "<input type=\"hidden\" name=\"remove\" value=\"".$row['Name']."\" />\n";
     
        echo "<input type=\"submit\" name=\"submit\" Value=\"Delete\" />\n";
        echo "</form>\n";
    }
    ?>
    Many Thanks,
    P.S You've been a great help so far, keep up the good work, a credit to the forum.

  14. #14
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    session_start();

    if ( isset( 
    $_POST['remove'] )) {
        
    $tmp $_SESSION['records'];
        
    $remove $_POST['remove'];
        foreach ( 
    $remove as $K => $V ) {
            
    $query "delete from birds where name='" .$tmp[$V]->Name"'";
            
    mysql_query$query );
            if (
    mysql_errno()) {
                echo 
    "Could not remove : " .$tmp[$V]->Name"<br />\n";
                echo 
    "Error Number : " .mysql_errno(). "<br />\n";
            }
            unset( 
    $tmp[$V] );
        }
        
    $_SESSION['records'] =& $tmp;
        
    header("Location:Where_ever_I_Came_From.php");

    You are sending the data back to the same form, you need a piece of code such as above( frm_handler.php ) to do the processing.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    Upon running the following code, I get greeted with the following error messages. Why is this?

    Warning: Invalid argument supplied for foreach() in C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\frm_handler.php on line 9

    Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\frm_handler.php:9) in C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\frm_handler.php on line 21

    Code:
    <?php
    
    session_start();
    
    if ( isset( $_POST['remove'] ))
    {
      $tmp = $_SESSION['records'];
      $remove = $_POST['remove'];
      foreach ($remove as $K => $V)
      {
        $query = "delete from Birds where Name='" .$tmp[$V]->Name. "'";
        mysql_query( $query );
        if (mysql_errno())
        {
          echo "Could not remove : " .$tmp[$K]->Name. "<br />\n";
          echo "Error Number : " .mysql_errno(). "<br />\n";
        }
        unset( $tmp[$V] );
      }
      $_SESSION['records'] =& $tmp;
      header("Location: 1.php");
    }
    ?>
    Many Thanks,

  16. #16
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Go back and look at my form code, it uses an array called remove.
    Do a var_dump( $remove ); right before the foreach and tell me what it prints.
    Your code uses a variable not an array called 'remove'. The foreach loop is expecting an array as the variable.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    string(1) "1"

    What does that do?

    Many Thanks,

  18. #18
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That means that your variable is a string with one character and contains the value of "1". You will be using these values as array keys so you do not need to quote the values in the form or use intval() to convert to an integer.
    You need to change your form code to something like I had that uses an array variable called 'remove'.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    I now get greeted with the following error message:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in
    C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\1.php on line 44

    Code:
    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    $item = 0;
    $line = 0;
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    $records = array();
    $temp = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
    
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex. "<br />";
      $line++;
      
    $i = 0;
    
    echo "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo "<table>\n";
    foreach( $records as $k  ) {
        echo "    <tr>\n";
        echo "        <td>" .$k->Name. "</td>";
        echo "        <td>" .$k->Species. "</td>>";
        echo "        <td>" .$k->Sex. "</td>";
        echo "        <td><input type='checkbox' name='remove[" .$k. "]' value=" .$k. " /></td>\n";
        echo "    </tr>\n";
        $i++;
    }
    echo "    <tr>\n";
    echo "          <td><input type="submit" value="Submit" /></td>\n";
    echo "    </tr>\n";
    echo "</table>\n";
    echo "</form>\n"; 
    
    
    ?>
    Many Thanks,

  20. #20
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $session_start();
    $con mysql_connect("localhost","peter","abc123");
    if (!
    $con) {
      die(
    'Could not connect: ' mysql_error());
    }

    mysql_select_db("WB"$con);
    $item 0;
    $line 0;

    $result mysql_query("SELECT * FROM Birds");

    if(
    mysql_error() != || mysql_num_rows($result) == 0) {
      die(
    "No database, or no data");
    }

    $records = array();
    $temp = array();
    while(
    $row mysql_fetch_object($result)){
      
    array_push($records$row);

      echo 
    $records[$line]->Name;
      echo 
    $records[$line]->Species;
      echo 
    $records[$line]->Sex"<br />";
      
    $line++;
    }
    $_SESSION['records'] =& $records;

    $i 0;

    echo 
    "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo 
    "<table>\n";
    foreach( 
    $records as $k ) {
        echo 
    "    <tr>\n";
        echo 
    "        <td>" .$k->Name"</td>";
        echo 
    "        <td>" .$k->Species"</td>>";
        echo 
    "        <td>" .$k->Sex"</td>";
        echo 
    "        <td><input type='checkbox' name='remove[" .$k"]' value=" .$k" /></td>\n";
        echo 
    "    </tr>\n";
        
    $i++;
    }
    echo 
    "    <tr>\n";
    echo 
    "          <td><input type='submit' value='Submit' /></td>\n";
    echo 
    "    </tr>\n";
    echo 
    "</table>\n";
    echo 
    "</form>\n"
    You forgot to close out your while loop.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

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

    Array Manipulation

    Byron,

    Now I get this error message:

    Fatal error: Function name must be a string in C:\Documents and Settings\Dale Piper\Desktop\xampplite\htdocs\1.php on line 2

    Code:
    <?php
    $session_start();
    
    $con = mysql_connect("localhost","peter","abc123");
    
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $item = 0;
    $line = 0;
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    $records = array();
    $temp = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex. "<br />";
      $line++;
    }
    
    $_SESSION['records'] =& $records;
    $i = 0;
    echo "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo "<table>\n";
    foreach( $records as $k )
    {
      echo "    <tr>\n";
      echo "        <td>" .$k->Name. "</td>";
      echo "        <td>" .$k->Species. "</td>>";
      echo "        <td>" .$k->Sex. "</td>";
      echo "        <td><input type='checkbox' name='remove[" .$k. "]' value=" .$k. " /></td>\n";
      echo "    </tr>\n";
      $i++;
    }
    
    echo "    <tr>\n";
    echo "          <td><input type='submit' value='Submit' /></td>\n";
    echo "    </tr>\n";
    echo "</table>\n";
    echo "</form>\n"; 
    ?>
    Many Thanks,

  22. #22
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Salchester My apologies that is supposed to be : "session_start();" with no $ sign. I don't know how that got there. (lol)
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  23. #23
    SitePoint Zealot
    Join Date
    Aug 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you should just hire Byron to do this work for you...

  24. #24
    Non-Member
    Join Date
    Jun 2007
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WoW_15 View Post
    Maybe you should just hire Byron to do this work for you...
    Wow_15,

    No! Its better to achieve it yourself through help, instead of getting other people to do everything for you.

    Many Thanks,

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

    Array Manipulation

    Quote Originally Posted by byron3@earthlink View Post
    Salchester My apologies that is supposed to be : "session_start();" with no $ sign. I don't know how that got there. (lol)
    Byron,

    I now get the following error message:

    Catchable fatal error: Object of class stdClass could not be converted to string in C:\Documents and Settings\It's Dale\Desktop\xampplite\htdocs\1.php on line 45

    Code:
    <?php
    
    session_start();
    
    $con = mysql_connect("localhost","peter","abc123");
    
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("WB", $con);
    
    $item = 0;
    $line = 0;
    
    $result = mysql_query("SELECT * FROM Birds");
    
    if(mysql_error() != 0 || mysql_num_rows($result) == 0)
    {
      die("No database, or no data");
    }
    
    $records = array();
    $temp = array();
    while($row = mysql_fetch_object($result))
    {
      array_push($records, $row);
      echo $records[$line]->Name;
      echo $records[$line]->Species;
      echo $records[$line]->Sex. "<br />";
      $line++;
    }
    
    $_SESSION['records'] =& $records;
    $i = 0;
    echo "<form method='post' action='frm_handler.php' name='Birds'>\n";
    echo "<table>\n";
    foreach( $records as $k )
    {
      echo "    <tr>\n";
      echo "        <td>" .$k->Name. "</td>";
      echo "        <td>" .$k->Species. "</td>>";
      echo "        <td>" .$k->Sex. "</td>";
      echo "        <td><input type='checkbox' name='remove[" .$k. "]' value=" .$k. " /></td>\n";
      echo "    </tr>\n";
      $i++;
    }
    
    echo "    <tr>\n";
    echo "          <td><input type='submit' value='Submit' /></td>\n";
    echo "    </tr>\n";
    echo "</table>\n";
    echo "</form>\n"; 
    ?>
    Many Thanks,


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
  •