943,603 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1216
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 7th, 2008
0

checkboxes problem

Expand Post »
I want to write a code that will view all the records within a table and assign a checkbox for each record.. the check box will be used for deleting the records just like in emails. can anyone help me on this?
Similar Threads
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
bornok15 is offline Offline
91 posts
since Feb 2008
Mar 7th, 2008
0

Re: checkboxes problem

hi
try this example:
suppose you want following to be done:
[]record1
[]record2
here [] is check box
then do following to get above:
1.suppose you have two variable $rec1 and $rec2 which contains data to be printed along with check box,
$rec1="record1" ;
$rec2="record2";
2.prepare a $rspString like following:

$rspString="<form>"+$rec1+"<input type='checkbox' name='some_name' value="+$rec1+">
<br>"+$rec2+"<input type='checkbox' name='some_name2' value="+$rec2+">

</form>";
print this.
it will create check box with required variable value.
this is just a example. you can follow this example to generate required string for you.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Mar 7th, 2008
0

Re: checkboxes problem

Hi,
Even though it is a fairly advanced script you could try looking at this post:

http://www.daniweb.com/forums/thread111225.html
or you can do a search on checkboxes and find a few additional examples
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Mar 7th, 2008
0

Re: checkboxes problem

just assign a checkbox with a different name to each one with id value as the checkbox value. then use the foreach() statement to loop through the post results and regex the key to make sure it belongs to that set of input elements. after that take the value from the foreach statement and use it in a query to delete the record.

i know that sounds complicated and you probably don't understand it. if you need some example code let me know.
Last edited by kkeith29; Mar 7th, 2008 at 8:38 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Mar 7th, 2008
0

Re: checkboxes problem

Its like in group of records where they all have a checkboxex assigned to them and you can check it to do something to it.. i hope you could give some simple example on this.. thanks
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
bornok15 is offline Offline
91 posts
since Feb 2008
Mar 7th, 2008
0

Re: checkboxes problem

Btw this is how i do it right now. im just using a text link to delete the records.
PHP Syntax (Toggle Plain Text)
  1. $action=$_REQUEST['action'];
  2. $id=$_REQUEST['id'];
  3.  
  4. if ($action==1){
  5.  
  6. $link=mysql_connect($hostname,$username,$password);
  7. $dbname=mysql_select_db($database);
  8.  
  9. echo "<table width=100% cellspacing=0>";
  10. echo "<tr><td bgcolor=darkblue><center><font face=verdana size=4 color=white>GV Local Site using PHP and MySQL</font></tr>";
  11.  
  12. $query = "SELECT * FROM employee";
  13.  
  14. echo "<table width=100% cellspacing=0 border=1><tr><td><center><B>ID<td><center><B>First<td><center><B>Last Name<td><center><B>Address<td><center><B>Delete Entry</tr>";
  15.  
  16. $result = mysql_query($query) or die ("Query failed: " . mysql_error() . "Actual query: " . $query);
  17.  
  18. //$result = mysql_query("select * from mytable");
  19. //while ($row = mysql_fetch_object($result)) {
  20.  
  21. while ($row = mysql_fetch_object($result)){
  22. echo "<tr><td>".$row->id;
  23. echo "<td>".$row->first;
  24. echo "<td>".$row->last;
  25. echo "<td>".$row->address;
  26. echo "<td><a href='ayan.php?action=2&id=$row->id'>Delete Record</a></tr>";
  27. }
  28. echo "</table><br><br><center><font face=arial size=4 color=white><b><a href='index.php'>Go back to Main</a></b></font>";
  29. }
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
bornok15 is offline Offline
91 posts
since Feb 2008
Mar 7th, 2008
0

Re: checkboxes problem

i am working on the code, i will post soon.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Mar 7th, 2008
0

Re: checkboxes problem

here is the code. i am not for sure if it will work properly with your setup because i don't know the rest of the code, but i have used a similar system in many of my admin centers i have created and it works nicely.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $action = $_GET['action'];
  4. if (!empty($action)) {
  5. $con = mysql_connect($hostname,$username,$password);
  6. $db_con = mysql_select_db($database);
  7. switch($action) {
  8. case 1:
  9. $sql = "SELECT * FROM `employee`";
  10. $query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
  11. $tr_data = '';
  12. $i = 0;
  13. while ($row = mysql_fetch_assoc($query)) {
  14. $tr_data .= '<tr>';
  15. $tr_data .= '<td>' . $row['id'] . '</td>';
  16. $tr_data .= '<td>' . $row['first'] . '</td>';
  17. $tr_data .= '<td>' . $row['last'] . '</td>';
  18. $tr_data .= '<td>' . $row['address'] . '</td>';
  19. $tr_data .= '<td><input type="checkbox" name="delete' . $i . '" value="' . $row['id'] . '" /></td>';
  20. $tr_data .= '</tr>';
  21. $i++;
  22. }
  23. $html =<<<HTML
  24. <table width="100%" cellspacing="0">
  25. <tr>
  26. <td bgcolor="#0000FF" align="center">
  27. <font face="verdana" size="4" color="#FFFFFF">GV Local Site using PHP and MySQL</font>
  28. </td>
  29. </tr>
  30. </table>
  31. <form action="ayan.php?action=2" method="post">
  32. <table width="100%" cellspacing="0" cellpadding="3" border="1">
  33. <tr>
  34. <th>ID</th>
  35. <th>First</th>
  36. <th>Last Name</th>
  37. <th>Address</th>
  38. <th>Delete</th>
  39. </tr>
  40. $tr_data
  41. </table>
  42. </form>
  43. <br />
  44. <br />
  45. <center>
  46. <font face="arial" size="4" color="#FFFFFF"><strong><a href="index.php">Go back to Main</a></strong></font>
  47. </center>
  48. HTML;
  49. break;
  50. case 2:
  51. foreach($_POST as $key => $value) {
  52. if (preg_match("/^delete+[0-9]$/",$key)) {
  53. $sql = "DELETE FROM `employee` WHERE `id` = '" . mysql_real_escape_string($value) . "'";
  54. $query = mysql_query($sql,$con) or die('Error: ' . mysql_error());
  55. }
  56. }
  57. $html = 'Employees Deleted Successfully';
  58. break;
  59. }
  60. }
  61.  
  62. echo $html;
  63.  
  64. ?>

let me know if there are any problems. i will help you fix them.
Last edited by kkeith29; Mar 8th, 2008 at 12:00 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Mar 8th, 2008
0

Re: checkboxes problem

Can u explain how the $tr_data and the for each block works?
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
bornok15 is offline Offline
91 posts
since Feb 2008
Mar 8th, 2008
0

Re: checkboxes problem

what i did with the $tr_data is store the html into a variable instead of echoing like you had. this makes it so i can use the header() function to redirect, sorry its a force of habit. i can change if you want. the foreach() block starts off by reading the post data sent from the form where you checked the checkboxes. "foreach" of the array values it checks if the key (name element in html) is valid. if it is then it deletes the employee otherwise it skips it.

clear enough?
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Trouble with Array Delete in PHP , MySQL
Next Thread in PHP Forum Timeline: Variable refering to a Variable Name





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC