Display user defined number of MySql results with dynamicly updating content [php] - php

I'm trying to limit the number of rows that the page shows according to the users selection from a drop down(think like a store page "show number of items per page")
At the moment I'm using php to call MySql and then echo out my results. I know that php cant do anything after the page loads. The next thing that comes to mind is java script.
However I have no experience with java script, I'm fair normal with java.
What are other options that you would suggest?
note: I want to limit my while loop not the mysql result.
Here is my code as it stands now:
<form name="input" action="EditPartyP.php" method="post">
<tr>
<td>Party ID</td><td>Party Name</td>
<td>Start Date</td><td>End Date</td><td>Sales</td><td>VIEW:
The drop down selection options that are being offered.
<select>
<option value="10">10</option>
<option value="20">20</option>
<option value="40">40</option>
<option value="80">80</option>
</select>
</td>
The results from the query being displayed, I under stand that I will have to change this because the php is done when the user sees the output html.
<?php
$ID = $_SESSION['ID'];
$result = PartyData::PartyLookupByID($_SESSION['ID'], "DESC");
This loop now just runs until the result runs out of data.
I need to make it stop when the users number is reached as well.
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<form name="input" action="EditPartyP.php" method="post">';
echo "<td>" . $row['PartyID'] . "</td>";
echo "<td>" . $row['PartyName'] . "</td>";
echo "<td>" . $row['sDate'] . "</td>";
echo "<td>" . $row['eDate'] . "</td>";
echo "<td>" . $row['PartyOrderTotal'] . " </td>";
echo '<input type="hidden" value=" ' . $row['PartyID'] . '" name="PartyID">';
echo "<td>" . '<button type="submit" value="Edit" name="Action">Edit</button>' . "</td>";
echo '</form>';
echo "</tr>";
}
?>
</table>
Thank you for reading and a comment helps more than a down vote.

You might want to look into https://datatables.net if you want features such as per page row diaplay or so called pagination.

Based on the selected page (via a $_GET['var'] and the users choice of number of records per page, you have to adjust your query with the limit statement.
SELECT * FROM table LIMIT 10, 50
Would start at the 10th record and will select 50 records max.

You can use SQL Limit command to retrieve specific number of data.
SELECT column_name(s) FROM table_name LIMIT number;

Related

Dropdown menu inside a table - then update

I have a table populated from a mysql database. One of the fields is "status". I would like this cell to be a drop down box inside the table, so I can then update the particular field.
This code, correctly displays the table and currently it displays the "status" filed inside a text box that I can edit successfully. I would like this to be a drop down though.
<?php
require_once('db_connect.php');
$result = mysql_query("SELECT *
FROM queries
WHERE SR = '$_GET[SR]'
")
or die(mysql_error());
echo '<form name="Form" action="update.php" method="post">';
echo
"<table id='box-table-b'>
<thead>
<tr>
<th>SR</th>
<th>Product</th>
<th>Status</th>
</tr>
</thead>";
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['SR'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . '<input type="text" name="status" value="'.$row['status'].'" />' . "</td>";
echo "</tr>";
echo "</tbody>";
}
echo "</table>";
echo '<input type="submit" name="Save" value="Save" />';
echo '</form>';
?>
Can someone please show me how to do this ?
To answer the question, you should use the <select></select> tags. Example:
<select>
<option>Item 1</option>
<option>Item 2</option>
<option> ... </option>
<option selected="selected">Item N</option>
</select>
In this specific example, the dropdown would appear with "Item N" selected by default.
As a side note, isn't using mysql_* functions bad practice in general?
by drop down menu I guess you mean a select tag, anything more complicated that this requires a custom implementation.
This requires 2 steps: first you need to create the select tag and populate it with option tags, then you need to set the desired value as selected.
to create the select tag:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
$myselect.="<option value='$e'>$e</option>";
}
$myselect.="</select>";
$status_value is a array, you can have in your code or get it from a query.
To select the correct one you can add the following if to the code above:
$myselect="<select id='status' name='status'>";
foreach($status_values as $e){
if($e == $row['status']){
$myselect.="<option value='$e' SELECTED>$e</option>";
}else
$myselect.="<option value='$e'>$e</option>";
}
}
$myselect.="</select>";

mysql php updating multiple entries through populated drop down menu

I'm having an issue trying to update multiple entries in my database via a php populated drop down menu. Here is the code on my page that populates the table showing me all entries currently in my database:
$result = mysqli_query($con,"SELECT * FROM Submissions");
echo "<table border='1'>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Title</th>
<th>Text</th>
<th>Public Post OK?</th>
<th>Date/Time Submitted</th>
<th>Approved?</th>
<th>Test Approved</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['fname'] . "</td>";
echo "<td>" . $row['lname'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . nl2br($row['text']) . "</td>";
echo "<td>" . $row['publicpost'] . "</td>";
echo "<td>" . $row['time'] . "</td>";
echo "<td><select name=\"approved\"><option value=\"" . $row['approved'] . "\">" . $row['approved'] . "</option><option value=\"yes\">Yes</option><option value=\"no\">No Again</option></select></td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<br><br>
<form action="update.php" method="post">
<input type="submit" name="SubmitButton" value="Update" class="submit" style="cursor:pointer;">
</form>
<?php
mysqli_close($con);
?>
This is the php code for "update.php":
$approved = $_POST['approved'];
mysqli_query($con,"UPDATE Submissions SET approved = $approved");
$update_query= "UPDATE Submissions SET approved = '$approved'";
if(mysqli_query($con,$update_query)){
echo "updated";}
else {
echo "fail";}
?>
<form action="approvesubmissions.php">
<input type="submit" value="Approve Submissions page">
</form>
The goal is to have the ability to update the field "approved" with a drop down menu from "NO" to "YES" or vice versa. Instead, what is happening with this query, is that it is erasing the data in the "approved" field instead of updating it. I'm somewhat new to php and i have researched a TON on this and have come up with no solutions. Any help is GREATLY appreciated!
First, let's assume 'approved' is a TINYINT(1) or something.
Your select html should be more like this. It will autofill based on the DB value.
$selected = 'selected="selected"'; // pre-selection attribute
$isApproved = !!$row['approved']; // is this active? (approved is 1 or 0)
echo '<select name="approved">
<option value="1" ' . ($isApproved ? $selected : '') . '>Yes</option>
<option value="0" ' . (!$isApproved ? $selected : ''). '>No</option>
</select>';
Secondly, your form is at the bottom of the table, but your input that you want is in the table. When you submit your form, there is no $_POST['approved'], because that's technically not in a form. To fix, you'll need to put your opening form tag at the top before the table. Then, you'll want to put your submit button and closing form tag at the end, after you've echoed the table out.
Thirdly, your post.php page should NOT ever take user input directly into a query. But, simply do this:
// Convert input to boolean answer, then int (for the query).
$approved = isset($_POST['approved']) ? (int)!!$_POST['approved'] : 0;
mysqli_query($con,"UPDATE Submissions SET approved = '$approved'");
While we're on the topic, this would be a great time to jump into prepared statements for your project. It might sound scary, but it can save you from SQL injection.
// Make the prepared statement
$query = mysqli_prepare("UPDATE Submissions SET approved = ?");
// Safely bind your params
mysqli_stmt_bind_param($query, "i", $approved);
// Run it
mysqli_stmt_execute($query);
// "close" the statement (hint: it's reusable for things like bulk updates, etc)
mysqli_stmt_close($query);

Refreshing a php page after submit button - PHP - MySQL

I have a page that needs to be constantly refreshed multiple times a minute. The page has is a echo'd php table.
The page loads perfectly fine, all is good, I have used the META tag HTML, I have used the header tag with the refresh function in PHP... and yet a problem arises :
When I hit Start Session button the refresh stops. And the table bellow does does not get updated. So then I have to manually refresh the page. This is not the desired affect. Can some one explain to me how to refresh a page continually.
Edit 1:
Code that makes the include of the session start
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> <a href=student.php?anum=" . $row['anum'] . " target='_blank'>" .$row['anum'] . " </a></td>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['why'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['additional_req'] . "</td>";
echo "<td>" . $row['signintime'] . "</td>";
echo "
<td> <form action='counselor.php?id=" . $row['id'] . "' method='post' target='_new'>
<select name='namedrop'>
<option value=''>Counselor Name</option>
<option value='Admin-John'>Admin - John</option>
<option value='Admin-Christine'>Admin - Christine</option>
<option value='Admin-Dawne'>Admin - Dawne</option>
<option value='Counselor-Cherie'>Counselor - Cherie</option>
<option value='Counselor-Tootie'>Counselor - Tootie</option>
<option value='Counselor-Debbie'>Counselor - Debbi</option>
<option value='FrontDesk-Delores'>Front Desk - Delores</option>
<option value='FrontDesk-Kiana'>Front Desk - Kiana</option>
</select>
</td>
<td> <input type='submit' name='submit' value='Start Session'></td>
</form> </td>";
}
If you are looking to refresh the page when you click "Start Session", then you can edit your submit button to have this onClick listener. It should refresh the page.
<input type='submit' name='submit' value='Start Session' onClick="window.location.reload()" />
Is it possible for you to use Javascript to achieve the desired effect? I don't know enough about what you're doing to determine whether that would clear your changes or not (my assumption here is: no).
If you are able to use Javascript, you could write a simple function which refreshes the page on an interval.
<form action="<?php echo $PHP_SELF; ?>" method="post">
or
<form action="thispage.php" method="post">

Trying to get PHP checkboxes to hold data from a MySQL table

Ive got to make a web-based checkout for an assignment and have come across a problem, ive imported a database and set up the data in a table with added check boxes alongside it. I need to take the reference number (stored first in the array) onto another page using sessions. From using var_dump i cant seem to get anything from the selected from the table.
Code:
Button code
<p><tr>
<input type="submit" name="Select" id="Select" value="Add Selected To Cart"/>
</tr></p>
Access database code(values changed for saftey)
<?php
Accesses database
$con=pg_connect("host=hostname port=portnumbers
dbname=name user=user password=password");
if (!$con)
{
die('Could not connect to database');
}
?>
Database display
//Creates table
echo "<table border='1'>\n<thead>\n<tr>\n";
echo "<th>Title</th>
<th>Platform</th>
<th>Description</th>
<th>Price</th>
<th>Buy</th>\n";
while($row = pg_fetch_array($res)){
echo"<tr>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo '<td><input type="checkbox" name="selected[]" value="' . $row['0'] . '" /></td>';
echo"</tr>";
}
echo"</table>";
?>
Well, I think you can as follow because you are trying to improve a shopping car:
//Use the `$_SESSION` var to hold the values
foreach ($_POST['selected'] as $item)
$_SESSION['cart'][$item] = $item;
As the item's id will be indexed, each time a submit is performed:
If the item exists, will be replaced
If the item does not exist, will be added
In order to remove items, you should use a "view cart" page and then show the items:
foreach ($_SESSION['cart'] as $item)
echo '' . $item . '';

PHP and HTML Deleting [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I simply just want a button in my table to delete the specific row but everything I search for ends up being some completely different way compared to how I have set it up.
<?php
// Connects to your Database and if it cant it dies
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// this selects the db
mysql_select_db("test", $con);
// this passes the query into the variable result
$result = mysql_query("SELECT * FROM items");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Quantity</th>
<th>Delete</th>
<tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . 'Delete' . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
//end php
?>
<html>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="Name" />
Quantity: <input type="text" name="Quantity" />
<input type='submit' name='add' value='add' />
</form>
</body>
</html>
i would like the hyperlink to delete the row is contained in.
You need to add the ID of the record you want to delete to the delete url, e.g.:
echo "<td>Delete</td>";
Then, in your delete.php script, you'd do:
<?php
$id = intval($_GET['id']);
$sql = "DELETE FROM yourtable WHERE id=$id";
$result = mysql_query(sql);
Of course, that's not a full script, but shows you the basics of what needs to be done. Be careful with using the passed-in value in your query - don't want to let an SQL injection hole ruin your day.
However, be aware that using a 'GET' type query for this sort of thing is generally a bad idea. If this page gets spidered by Google (for one), you'll find that the simple act of spidering has nuked ALL of your records.
You need to pass the ID of the row as paramter to the delete.php script
echo "<td>" . 'Delete' . "</td>";
you can use the id of the current row and send it to delete.php:
echo "<td>" . 'Delete' . "</td>";
then in delete.php get the id by $deleteId = $_GET['id'] and use it...
Try changine the URL to something like echo '<a href="delete.php?ID=' . $row['ID'] . '">'
BTW - Not a good idea to use root!

Categories