How to make a MySQL generated table interactive PHP - php

I have currently have a search form that specifies specific information of the users interest and generates a table with the filtered data.
I want to make this table interactive, specifically by allowing the user to click on the row (entry) of interest and be sent to a page with more detailed information about that entry.
I'm a novice at best and was wondering if anyone knew a way to approach this. I can provide more information if required but my MySQL and PHP search is pretty straightforward: The search locates specific columns in the MySQL table and the data that is filtered provides some - not all - the information in a table that is generated from the query.

Simply output a link that goes to a page that loads based on RowId, but don't forget security involved with passing an ID around.
<?php
foreach($resultset as $row)
echo "<a href='action.php?rowId={$row['id']}'>View Profile</a>";
?>
Then in action.php
<?php
$profile = user_load_profile_function( $_REQUEST['rowId'] );
// do whatever you want to here.
?>

Related

PHP: sending a link to a results page

I am learning PHP:
A person will get a results page. I want to enable this person to be able to send the results page (via link) to their given email (form). The link won't be constant as the results page would be specific for that person/ possibly session?
The results page is simply a html script containing sequences that have been run through a prediction model.
Store the results in a database. Construct a php page that would fetch that data from the database according to a GET variable. Give a link with appropriate GET variables to the user.
The best way to do this would be to have the search terms in the URL itself.
For instance, if its one search term, it could very easily be:
http://www.yoursite.com/search.php?term=search+term
Just handle this in PHP as:
<?php
$term = $_GET['term'];
/* blah blah, process the search and echo results */
?>
You could even utilize paging, and put the page in the URL itself, so that when the person returns to the link, it brings them right back to where they emailed it from. The rest is up to you.

post/redirect/get and saving database query in SESSION

I'm new to post/redirect/get. I'm coding up the first real site that led me to discover the need for PRG.
So I had written and gotten working code that did the following:
1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database --
a 'frequency of lookups' -- to indicate the user searched and found what
he was looking for
4) then display the results he searched on
What I found was refreshing the page in the browser resulted in the user seeing his same search results again but we incremented the 'frequency of lookup' -- the user is limited in the frequency of lookup in the database -- so if the user refreshed the page too many times, they ran through their hourly ration of database lookups. That's when I discovered Post/Redirect/Get a few days ago.
I am now going through the site and altering all pages that alter the database and display results and switching them over to PRG. Here is the new process:
1) user enters a search string
2) we search the database and find their desired search results
3) if we found their search results successfully, we alter the database --
a 'frequency of lookups' -- to indicate the user searched and found
what he was looking for
4) PRG to a 'results' page
5) then display the results he searched on
And I ran into a problem. I need to somehow 'pass' their search results from step (2) above
to the new 'results' page in step (5) that I created to implement PRG.
So my question is -- is there a 'most common' way to do this? In reading around I've seen 'save the results in a .CSV file' and also 'save the database search results in the SESSION.'
I want to save the search results in the SESSION then on my 'GET' page I added for PRG to display the result, I'll read the search results from the session variable and display them.
By that I mean I want to do this:
$result = mysql_query($query, $theDbServer);
$_SESSION['theSearchResults'] = $result.
Then on the 'display' page, read back the search results from $_SESSION['theSearchResults']
and display them using:
$result = $_SESSION['theSearchResults'];
$row = mysql_fetch_row($result);
Is this a normal approach? And I suspect I cannot save the raw $result in a session variable like the above -- but I'm also not sure how to put the $result above into the $_SESSION -- how is that normally done?
Assuming you don't want to have the final landing page do the querying (without decrementing their quota of course), then you're going to need to use session.
Saving to a CSV is not standard and wouldn't scale very well. I would parse the results of the query into a more user-friendly form (a simple class or list or whatever you need). I'd then store the class and not the reader into the session. You'll probably want to clear out that portion of the session when they leave the results page (especially if it's a huge amount of data).
This is assuming you're using raw php. There are many frameworks with features for this exact case (you want to shuttle a piece of data from one page to the next).

Is there a way I can change where a PHP page selects from a MYSQL database by using a form?

I have a website and I want to make it easier for someone to change certain information being shown without them having to edit the HTML/PHP and using FTP.
At the moment I have this information in a php file which is included in the MYSQL query.
It would be a lot easier if this was done using a form, say a text field where a person can type the table name and it updates on the main page and starts displaying that table instead.
Sorry if I haven't explained this well. :(
I have a good news for you.
Every php/mysql-driven site in the world is made this exact way - to edit site contents using HTML form.
Even first PHP version name was PHP/FI, stands for Form Interpreter.
Even better, a site user doesn't have to deal with mysql - it's all being done in PHP. No need to type table names into form field - all table names already written in PHP code.
Usual PHP application being connected to just one mysql database - so, no need to choose.
As for the tables, it's being done this way: a user selects some human-readable matter, like "Latest news" and being redirected to the PHP script called, say, news.php. this script runs a query for the news table in the database and outputs some HTML formatted news highlights!
Even more, you don't even need to program! There are plenty of ready-made programs, such as Wordpress
store what you want to be editable in a mysql text field.
remove tags you dont want him to see
in the form echo the editable information in a textarea
have him edit
add tags
update the mysql
note depending on the users knowledge depends on how many tags you would like to remove/add. the less per a field the easier.
on more complicated things i like to have the person log in. if he has permission then all the editable fields have an edit button. if he clicks it it goes to a page with a form that he can use to edit that 1 field

php report button

I am working on a search script and on each search result I want a report link button. I am not sure how to make one. I have looked on google for answers but cannot come up with anything. Can anyone give me an idea of what to do? or does anyone know where there is an example of this? I plan on using the link id and making a new table on mysql to send reports to. I am just looking for a basic button to send reports to mysql I am just not sure what would be the best way to do it. I have the data for the link id's I just need to be able to report it to a new table I am assuming. Any suggestions or examples are very appreciated. Thanks.
Chris,
First you will want to create that new table in your database to capture this information. Lets assume to you will have the following fields (very basic, you may want to add more): ReportId, LinkId, DateReported. ReportId is our primary key, LinkId is the ID you reference in your question and the DateReported is the server time we logged the transaction.
With this table created you are going to want to create a new php page, lets call it report-link.php. You are going to want to make this page accept a querystring variable called linkid (accessible in the $_GET[] collection). In the code of this page you will want to write a SQL query that inserts the value of the linkid parameter into the new link report table in the database (along with the date()).
On your search page you will be able to have users report an entry by clicking a link with the href of /path/to/report-link.php?linkid=<?php echo $link_id; ?>
Please note this example is very simplistic in nature, and offers no security for spamming, pleasant end user experience after they click the link, but this is the process you will want to follow in setting this feature up. After you have it working you can spruce up the experience for your users.

fetching data from database in randomly generated button

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

Categories