Editing a row of data in a table using an id - php

I am stuck with finding a means of being able to edit a specific row in my table. I have so far managed to come up with add,delete and search for a specific item that is stored in my database, i base the delete on a id field. Currently i am using PHP and HTML, could someone please show me a simple example that retrieves data from a database which stores in a table and when a user selects edit. The row that has been selected for edit becomes editable and thereafter updated without duplicating the data.
CLARIFICATION OF My QUESTION
For Editing to work the UI must look like, when the edit button corresponding to particular row is clicked then all the columns with the data should get replaced with text boxes, i.e. row-wise edit form...
Thanks

This is a very vague question, involves a lot of things. Not, just php, mysql or html but mainly javascript. However I will give a simple example, since you must already be able to bring the data onto a table.
Suppose your <tr> structure is similar to this
<tr>
<td class="nameField"><span class="text">My name</span></td>
<td class="controls"><a class="edit">Edit</a></td>
</tr>
Now using jQuery for this
$('a.edit').click(function() {
$(this).closest("tr").find("td").not(".controls").each(function(k,v) {
// ^ find all td but not the one with the controls
var span = $(this).children('span');
//get the elem holding the value
$(this).append('<input type="text" value="'+span.text()+'" />');
// Add a text box with the input inside the td
span.hide();
});
});
Demo

SQL Syntax you are looking for is UPDATE
For example you have an articles table tied to a user with user_id, then you have a list of articles whereby users can select to edit (with edit link or something similar)
Then you'd do something like
mysql_query('UPDATE articles set content = $new_content where id = $article_id');
Don't forget to sanitize $new_content and $article_id, or better yet, use mysqli altogether.

Related

How to set visibility of table row in PHP Office Word TemplateProcessor

I would like to conditionally display table row in predefined Ms Office Word file. The solution I am using currently only allows to manage display of whole block and so on the whole tables according to different combination of displayed data. I find this solution obscure. This is why I need conditionally display table row.
Current solution works with:
$$nameofdesiredkey
the conditionally shown content
nameofdesiredkay$$
I was able to solve that issue by workaround using another data table as result of dynamic values from pontential spread of values in the original table.
During my reserach idea of using multiple tables, using one dynamic data table list in one TD or just using $$variablestart variableend$$ and the start and end of each of nearest surrounding TDs seemed potent also.

PHP: JSON/AJAX live update to display data

Hi everyone,
When I delete first record, my table will live update but the order number remained '2',
is it possible to make it change to '1' without refresh page,
currently I am using ajax to do it, and found json_encode & json_decode maybe able to solve my problem?
$.ajax({
type:"POST",
url:"delete_item.php",
data:{id:del_id},
success:
function(){
}
});
$(this).parents(".record").animate('fast').animate({opacity:'hide'},'slow');
This is for display my data
<tbody id='item_list'>
<?php eitem_listItem($page, $record,$search); ?>
</tbody>
Sorry for my poor english.
The easiest way of addressing this is to hide the actual id of the data row (presumably you are using id as the unique identifier for your query) - so have it as a data attribute or an id for the delete button (to give you the del_id) or other method of keeping it in the data but not displayed.
Then when you are iterating through your data to display it - have a javascript count set up so that on the display of the data - each <tr> row gets its displayed index from this count - rather than the id of the actual data.
This means that you will display a 1 for the first displayed row - but the actual id that you use for the delete button may be 2 for example.
So the number on the side of the table becomes a displayed index that will always match the actual sequence of the data rows, but not necessarily will match the id of the row.

Click a button with XPath using an id from the same row

Im currently testing using PHPUnit and i need to create and delete the same things. So i create something called test, i want to be able to find it and delete it. I have multiple Delete buttons on the page, and it works by doing something like this -
$this->click("xpath=(//img[#title='Delete'])[11]");
but by choosing [11] for example, it is only using the delete button from row 11. And if Test isnt Row 11 for example, i will delete something i do not wish to delete.
I wish to try and delete a specific row by finding a row with a certain id attached to it.
The row may have the words "Test" followed by more information within columns. Then a delete button at the end titled 'Delete'. I wish to find the row with the id or words 'Test' and then find and use the 'Delete' title on that row.
Any help?
EDIT -Example image of a row. I would like to grab the first text, and then use the very last button on the right titled 'Delete'
http://i.stack.imgur.com/S4BNE.png
SECOND EDIT - This is the whole table, it contains all of the rows and within the rows it contains the td's (Obviously, as you'd know).
This is where ill find the text im trying to find. This tr would count as the 4th of 6.
This shows the td containing delete, a td containing 'Test Profile' and the whole table itself.
http://i.imgur.com/4r1tsJe.png
TL;DR Essentially, im trying to do, Where td contains 'Test', use 'Delete' from the same row.
Not very clear but I'll try to guess from your comments.
Forget about rows, but let's find all the td with "Test" (or some text you want in it)
descendant::td[contains(.,'Test')]/
and from here select the following-sibling td that contains a delete image
following-sibling::td//img[#title='Delete']
now you have the set of images nodes to click on, for each row.
Of course, if your function
$this->click("xpath=.....");
accepts only one node at the time, one possibility is to put the parenthesis and select always the first node (assuming that your test will delete it)
(descendant::td[contains(.,'Test')]/following-sibling::td//img[#title='Delete'])[1]
Even if the node is deleted when you click on it, in the next test this expression will always match, until there is any node left on the page
This is similar (not perfectly the same) to having a for loop ?
buttons = "xpath=....."
for each b in buttons do {
$this->click(b);
}
hope it helps
If you want to find the first row that contains Test in the first <td> element, or that has an id attribute of "Test" on the <tr> element; and you want to click the Delete img element from that row, you could try
$this->click("xpath=(//tr[#id='Test' or contains(td[1], 'Test')]//img[#title='Delete'])[1]");
But this is somewhat guesswork, since I can't see the HTML you're working with.
Since it looks like 'Test' could be in all-caps or in mixed-case, you may have to build in some case-insensitivity:
$this->click("xpath=(//tr[#id='Test' or
contains(translate(td[1], 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
'TEST')]//img[#title='Delete'])[1]");
Yes it's kludgy, but AFAIK PHP only supports XPath 1.0.

Add data from one table to another when specific data form one table is selected

I have a drop down menu on add_media.php that is populated from the "bulletin_date" field within the "bulletins" table in a MySQL database. When a "bulletin_date" is selected, I would like the "filename" that is a part of that same record in the "bulletins" table to be placed into the "publications" table along with the rest of the data that is being submitted via the add_media.php form.
I'm uncertain how to add to the "publications" table the "filename" from the "bulletins" table that is a part of the same record as the "bulletin_date" that was selected.
I hope this makes sense...I does in my head, but hard to put in words... :-)
Kind of need a bit more information before answering this, like, can you pre-populate the data from the bulletins table into the add_media.php form? This can be accomplished by retrieving the value on page load, and sticking it in a fashion like this:
<input type="hidden" value="<?php echo $bulletinsDateVal ?>" />
This will make it part of the posted data from add_media.php. It will probably need parsing/changing after the post to format it correctly, and there is the possibility of a loss of precision depending on how this is implemented.
If you can't, or if you would rather not, do it that way the other way is supplying the id of the row in bulletins table in the form, much like above. You can then simply add the bulletin_date column value as part of the INSERT that you do after the page is posted like so:
// Code which handles the POST from the form on add_media.php
$bulletinDataSelectVal = $_POST['bulletinDataSelect'];
$sqlInsert = "INSERT INTO publications(filename, other, form, data)
VALUES( (SELECT filename FROM bulletins WHERE id = '$bulletinDataSelectVal'),
'blah',
'dee',
'blah')";
Does that all make sense?
I see now after reading your comment, so you will probably want the latter then. Assuming your table has an unique id column, or some other way to identify it, definitely populate the dropdown with select's whose text is bulletin_date, but also set each select element's value to the ID of that row in the table like so:
<select name="bulletinDataSelect">
<option value="<?php echo bulletinData["id"]; ?>"><?php echo $bulletinData["bulletin_date"]; ?>
You can then use that idOfBulletinData value to do the above insert which I have edited.

Keeping track of dynamically generated form elements and passing them as POST variables

I have a page that retrieves records from 1 table in a database when a certain condition is met.
What I want to achieve is to provide the use to with an opportunity to update each record displayed using text boxes.
I am having trouble interpreting what logic to proceed with after the user hits the 'submit' button.
Normally, if I'm updating one record (or a static number of records), I will use the apporpriate amount of SQL statements.
Since the amount of records are dynamically generated, what is the best way to update all at once? How would I know which records were retrieved in the first place to update?
FOR EXAMPLE:
OK, We have a table with student ids (ID), names (SNAME), subjects (SUBJ), grade for each subject (GRADE) and general remarks (COMMENTS).
I want to retrieve information about all students that got an 'A', and write UNIQUE congratulatory remarks for each student (such as 'good job', or 'congratulations', or etc.)
I'd retrieve the records and lay them out on the page, with a text box next to each student record for the comments to be entered. Because I don't know how many text boxes to make, I give the text boxes dynamically generated names using the student ID. The user now enters unique comments for each student, and clicks on submit.
Now, how am I supposed to update these records with the values entered in each text box?
I wouldn't know which students were retrieved in the first place - how would I know what names to use? I'm trying to avoid having to execute the query again after submitting - but is there any other way?
Hope this question was not too confusing.
thanks
Further expanding earlier answers:
You need a loop (e.g. foreach) to display and save the textareas. If the names of the textareas include the students ID, you don't need to know the name, because the text is inserted to the database by the primary key (the students ID). You may name your form-elements as array, to iterate over them, for example (where the numbers are the IDs):
<textarea name="comment[2345234]"></textarea>
<textarea name="comment[8957485]"></textarea>
Read it out as described by #evan:
foreach((array)$_POST['comment'] as $studentId => $studentComment)
{
var_dump($studentId, $studentComment);
}
And if you implement this whole thing as self-requesting form (Affenformular in german), you may also use just one single loop to save and output the textareas.
"I don't think you're understanding what I'm trying to ask." Maybe you don't understand the answers, even you stated it. You don't need a students name to save a database record. But if you really want to submit it, you may also use hidden inputs.
Use foreach() to find the values you care about, put them in an array, and process the array.
Expanding on #Ignacio's answer to make it more easily understandable:
foreach($_POST as $name_of_input => $value_of_input)
{
// do stuff - here is something so you can see the results after the submit
echo "$name_of_input :: $value_of_input <br>";
}

Categories