fetching data from database in randomly generated button - php

i found it difficult,,,, fetching data from database while a buttons are randomly generated in for each how can i fetch

Without understanding what your question really is, you could go trough the mysql query result like this:
// button_text is a database column in this example
while ($row = mysql_fetch_row($result)){
echo "<button type="button">".$row['button_text']."</button>";
}
But to really help you, you need to rephrase your question!

I'll make some assumptions for what you are trying to do:
1. You have buttons that fetch more info from a db
2. The buttons are "randomly" generated, and each calls different info (pets, cars, etc).
You may or may not be using ajax, but I will describe it basically assuming straight php, and you are using a single page (for clarity in my explanation).
What you need to do is have each button either be a link or a submit for a form. This just depends on whether you want to use GET or POST. The buttons will have php generated links (if GET) or values (if POST). For example, using get the link could be "www.file.php?cat=cars". The button would just have the value of "Cars", and since bother are generated, that shouldn't be an issue keeping them the same.
When the page is now reloaded based on the click, the top of the page has a query in it to get the new info. For example, it would run a query looking for all items that have the car category. Then the new information would be displayed, and the new random buttons would show.
VERY IMPORTANT: Sanitize all GET and POST values before using them in a query

Related

Recovery of form field values upon calling of same file

I have a file which displays the resultset of a query. Now there is an option to compare the individual records (maximum upto 4 simultaneously). Problem is when I switch to next page, the previously selected values for comparison are lost (as the same file loads new records afresh, through pagination algorithm). Is there anyway, I could pass the selected values from the first selection and thereon to the final call(of same page). Technology being used is PHP, MySQL & Javascript. Any help would be highly appreciated. Thanks!
This is a shot in the dark without some sample code or a clear explanation of the pagination, but if you are using a form then you can pass the selections to hidden fields, session variables or some other scoped variable. It appears you are using AJAX, but without the code, I can't give you specifics. If you have full access to the code then a click function on the selection of the elements would allow you to pass the variables via AJAX store them via JAVASCRIPT.
If you can provide a few more details, my answer can be more specific. I would have just asked for clarification but I just joined and don't have the 'reputation' for that yet. ;-)

How to execute mysql_fetch_row() upon a button click

I am tying to put some user data from the database using php.
The database contains so many record and i am displaying it using mysql_fetch_row() and this gives me the fields of first row.
Now the problem is a have two buttons namely next and previous. And when a click next the fields in the next row has to be obtained the they should be inserted into page without loading the page once again. I am facing problem with this.
I am using php for server side programming and html, css for client side programming.
I think that no one will give you the answer. Too many to write. You need to read about ajax (this is the technology that will allow you to do some action without page reloading).
You need to write javascript(you can use some library like jquery) function which will open some php page with the data that you want and then update your site.
Here you have some nice tutorial:
http://www.w3schools.com/jquery/jquery_ajax_intro.asp

Need advice on updating a database from a form

I am not sure how to word the question because I don't really know what exactly I'm even asking for. Basically I have built a small sales portal that puts info in a database and then spits it back out in various ways. Occasionally the records need to be updated, but I need a way to update them from the web page where the records are displayed (without going to a completely different page) so that the sales people do not have to go into the database to change the records. I know how to use the UPDATE function with PHP, what I would like to do is have an "Edit" button at the end of each row and have the information become changeable when the button is clicked. What programming language would I need to do this in? How would that code be structured? I am really only familiar with PHP, HTML, and CSS.
Thanks for your help!
You're going to need javascript to handle the form fields the way you mentioned.
Each record row wil have the original value and a hidden input
Javascript will hide the original value and show the input
You could either have a save button at the end of each record row or a master "save all" button". Having multiple at the end of each record will be easier but from a users point of view maybe a bit cumbersome.
Use AJAX to send the updated data to the server - this should be handled in the "save record" javascript function.
Then you'll need PHP or some other server side language to actually update the record.

using modal message/window to display database record

I'm displaying tabular data populated via loop from a database.
I know that I could edit/delete etc in the usual manner, where
Edit Record
But I would really like to do this using the modal-message feature of jquery.
However, what I can't seem to figure out is how to indicate to the modal-message window/div the correct record number from the database (whereas in php, I'm simply using the print function.
Is it feasible to set maybe an onClick event that sets a variable ($id) to equal whatever the $row['abstract_id'] number is?
The idea being that when I click on the Edit button, I can put php within the div and it will call up the correct record.
EDIT for clarity: I don't want to actually edit it, it's to pull up the text of the abstracts, which are too big to fit in the tabular format (but there are too many abstracts to give each submission its own page).
The key here is how I can pass the record id from the database/php side to the javascript side even if it's setting/resetting some variable. I thought about using .load(read_abstract.php) but then realized that I don't think that .load(read_abstract.php?id=' . $row['abstract_id']') would work- and don't know what the JS equivalent would be (or if it exists).
You can move the id into a data attribute in the HTML tag:
echo '<span class="edit-button" data-abstract-id="'.$row['abstract_id'].'">edit</span>'
Then in the modal, you'll be able to reference it with:
jQuery('.edit-button').click(function() {
id = jQuery(this).attr('data-abstract-id');
});

Auto refresh in PHP

I have piece of code which refresh data periodically (after 5 sec.), and put it into a table.
This table has sorting option, and checkbox to select a particular row.
Now problem is when i want to sort or choose a row using checkbox, because of auto refresh it set whole table data in previous position. Means if any data i had sorted will not show sorted and/or checked row will be unchecked again.
Please provide me some suggestion how to handle this issue.
Thanks
Your alternative might be to do an AJAX call to pull the data periodically instead of refreshing the whole page via PHP. That way you'll be able to send the correct parameters to handle the sort towards the logic within PHP.
OR
You can push named parameters/actions within the url for sorting purposes. Then use URL's on the table header and the page reloads with a url of something like:
http://example.com/table/sort:asc
http://example.com/table?sort=asc
And then your logic could appropriately pick the previously selected areas up.
if (isset($_GET['sort'])) {
//Do sorting stuff
}

Categories