tinkertron Posted January 24, 2010 Share Posted January 24, 2010 I want to take this moment to thank alot of you for giving me the courage to dive into this head first on learning php/mysql. I'm getting very near to getting my first online database up and running. Your more then welcome to see what I have learn so far! Even test drive it yourself. http://tdcj.homeip.net/test.php ... So here's my question: If I want to add check boxes next to the entries, would I have to make a special colunm in MySQL table, if I want to make it funcation to edit or delete that entry? I'm learning as I go... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/ Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2010 Share Posted January 24, 2010 Nope you don't need anything in your database for a checkbox, just include it in your loop when you're pulling the info from the table. For example: $result = mysql_query($query); while($row = mysql_fetch_array($result)) { // Your code for displaying the table rows here ?> <input type="checkbox" name="delete[]" value="<?php echo $row['id']; ?>" /> <?php } Now see the checkbox name has [] after it, that's important. It means that when the form is submitted, an array called "delete" will be available containing the unique IDs of the rows selected for deletion. You can do the same with edit as well. All it will be is a matter of then looping through that delete or edit array and deleting them with a MySQL query Hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000724 Share on other sites More sharing options...
tinkertron Posted January 24, 2010 Author Share Posted January 24, 2010 I think I follow but the code seem to be wacky to me, so i'm going to post my code, and maybe you can show me: <?php $con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Also where would the "link" or "button" appear to either delete or edit? Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000725 Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2010 Share Posted January 24, 2010 In your main loop, which is: while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; is where you would put the checkbox, so change the above to this: while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"" . $row['id'] . "\" /></td>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; Obviously you'll need to add a table header for the delete column. You'll also need a <form> tag and a submit button Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000726 Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 You could handle the delete without a <form> and submit, by calling some javascript onclick of your checkbox, with the id in the js call, and then delete without a page reload. The record could then be marked as deleted, or with a little more work the row could be removed from the page. If the row were simply marked as deleted then it would not be there the next time the page was loaded. If you like, I can supply some example code to do this. Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000742 Share on other sites More sharing options...
tinkertron Posted January 24, 2010 Author Share Posted January 24, 2010 Monkeh: You've changed the code twice, simlar to each other but I started seeing alot of backlashes "/" why the reason for this? You went from this <input type="checkbox" name="delete[]" value="<?php echo $row['id']; ?>" /> to this echo "<td><input type=\"checkbox\" name=\"delete[]\" value=\"" . $row['id'] . "\" /></td>"; am I missing something here? Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000804 Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2010 Share Posted January 24, 2010 There's absolutely no difference, just you posted your own code the second time so I showed you what your new code would look like. I decided not to break your trend of echo-ing HTML using PHP so just changed it a little. The backslashes allow you to escape quotation marks. Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000806 Share on other sites More sharing options...
laffin Posted January 24, 2010 Share Posted January 24, 2010 " " " = php parsing error, as php had no way of knowing which " is the end quote. " \" " = correct definition of a double quoted string having a double quote within the string. it does take some time getting used to the \ character within strings (a bit harder to read in my opinion) backslashes also used for many metacharacters \n \t \b for example Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1000833 Share on other sites More sharing options...
tinkertron Posted January 25, 2010 Author Share Posted January 25, 2010 jl5501 If you like, I can supply some example code to do this. I would greatly appreciate this. and if you wouldn't mind can you also do this for edit? I would need the db to be as up to date as much as possible. I just notice that problem today, when I add a entry the db does not show the add information unless the user refresh the page :'( it one thing after another. I did buy a php application from phpgrid.com but unable to get it to work. I have someone from the UK helping me. I made a new years resolution to quit smoking, but this project is not helping :-\ , I need a simple but easy grid with the information as shown on the tabs, other then the database, the rest I can do, hopefully. As soon as this project is complete and I get the pat on my back, then I will quit smoking, and move on in my life. Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001073 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 I can show you some example code I wrote for a similar situation. No guarantee that it will help you, but it might. I will post this a little later Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001166 Share on other sites More sharing options...
tinkertron Posted January 25, 2010 Author Share Posted January 25, 2010 jl5501 hey thanks! Even if it doesn't at least I can learn from the code. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001167 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 OK here are some code snippets of a system where I mark for deletion, and have a button on the page to delete marked records. As it sets a delete mark in the db, it can hold it until you run the delete or unset the delete mark It can easily be changed to do the actual deletion, but for this particular customer, delete marking was what was required. Bear in mind, I always use mysql_fetch_object to get database data so you will see me addressing fields in object notation. In each table row, I have this at the end <td class="body"> <a href="ra_admin.php?id=<?php echo $ra->id?>">Edit</a> </td> <td class="body"> <input type="checkbox" name="mdcb" onClick="markDelete(this,<?echo $ra->id?>)" <?php if($ra->markdelete==1) echo 'checked="checked"';?>> </td> ok and next you need the javascript function markDelete() function markDelete(elm,id) { var mark = elm.checked; var url="markfordel.php?id=" + id + "&delmark=" + mark; //alert(url); http.open("GET",url, true); http.onreadystatechange=handleDelmark; http.send(null); } that makes an ajax call to markfordel.php which does this $id = $_GET['id']; $delmark = $_GET['delmark']; if($delmark == 'true') $delmark = 1; else $delmark = 0; $qry = sprintf("update radata set mark_delete = %d where id=%d",$delmark,$id); $res = mysql_query($qry); You will notice that the javescript function does have a callback set, which will be executed when the php has finished, but I do not have anything currently in that function except for an alert to show me what the php did, which is commented out. What you could have, is a div on the page which you place a nice message in This is code from a live commercial system for a customer of mine, so I know it works Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001554 Share on other sites More sharing options...
tinkertron Posted January 25, 2010 Author Share Posted January 25, 2010 jl5501 thanks for the posting. I will refer to this often as possible. Right now i'm running into a few bumps in the road, (if it's not one thing, it's another). I didn't know that I would be getting myself into this much trouble, but I am insisted on getting this project done, so that I can show it off to my employer and be done with it. I have invested to much time and money into this project that I wish it would be done by tomorrow, but it doesn't look that way. I wish I could hire a php/mysql css programmer and be done with it, but i'm limited on funds and so on. I bought this phpgrid from phpgrid.com and I though all my prays were answered, but it's still not :'( Check out my current page that i'm working on http://tdcj.homeip.net/main.html (tell me what you think, also the grid on there is an example and not the correct content that I will be putting in.) Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001559 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 That grid has a few nice features, but in my experience, changing the few things you need to, to get exactly what you want is exasperating. But I am a freelance web coder and do things like that in a custom way for a living, so I am naturally biased :-) Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001564 Share on other sites More sharing options...
tinkertron Posted January 25, 2010 Author Share Posted January 25, 2010 Are you up for hire? or are you swamp? With the current software that I have, majority of the programming is already done. How much would you charge me to help me get this project done? Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001567 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 Hi Yes I am available, but we had better discuss the details by PM or email or skype. I would not want to be banned for advertising on the board. I can be reached by email on [email protected] or skype as john.lodge3 or yahoo as johnl5501 Quote Link to comment https://forums.phpfreaks.com/topic/189602-checkbox-to-delete-or-edit/#findComment-1001570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.