PHP pagination on table - php

So for a school project, I'm making an inventory system, one of the pages contains a table with all the available stock. But there is way too much data to display in the table and I don't want the table to be 300 records long. So i decided I wanted to implement pagination on the already existing table. Now the thing is that I have no idea where to begin. I'm doing this with Object-oriented PHP. I hope someone can send me into the right direction of where to begin.
PHP code where i fill my table with data:
<?php
require_once('classes/Database.php');
$db = new Database;
$query = $db->query("SELECT * FROM producten");
$count = $query->rowCount();
if($count > 0) {
while($row = $query->fetch())
{
echo "<tr>";
echo "<td id='td1'>" . $row[0] . "</td>";
echo "<td id='td2'>" . $row[1] . "</td>";
echo "<td id='td3'>" . $row[2] . "</td>";
echo "<td id='td4'>" . $row[3] . "</td>";
echo "<td id='td4'>" . $row[4] . "</td>";
echo "<td id='td5'><a onclick='document.getElementById(\"id\").value=".$row[0]."' data-toggle='modal' data-target='#exampleModal' href=''><img data-toggle='tooltip' data-id='$row[0]' data-placement='top' title='Edit' src='img/edit.svg' height='25'></a></td>";
echo "<td id='td6'><a id='btn_link' href='php/Delete.php?id=".$row[0]."'><img data-toggle='tooltip' data-placement='top' title='Delete' src='img/delete_2.svg' height='25'></a></td>";
//echo "<td id='td5'><button type='button' class='btn btn-danger'>Delete</button></td>";
echo "</tr>";
}
}
?>
Then in my HTML file, I just include this PHP file so it's being displayed on the page.

Try using jQuery DataTables https://datatables.net/, its very easy and useful for handle many record and support Server-side processing .

You can use Bootstrap datatable with zero configuration.
Here is the link
You can easily configure as per your preferences and requirements. Go through the docs they are easy to understand.
Other way would be:
Use Mysql query with limit and offset but that would be another long way around though.

Related

Dynamically creating buttons that remove a row from a table

I'm trying to create a button for each row in my database that, when pressed, will delete this particular row. I should also mention that the data from the database is displayed correctly and the table I'm using is also completely fine.The buttons appear at the side of each row, when the button is clicked, the row dissapears but the data is not deleted from the database, when the page is reloaded the rows that were previously "deleted", reappear. After pressing the button i also get this "Fatal error: Uncaught Error: Call to undefined function mysql_query() in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php:56 Stack trace: #0 {main} thrown in C:\xampp\htdocs\INDUSTRIALPROJECT\records.php on line 56".
line 56 is : $del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);. The same query works fine when placed directly into phpMyAdmin.
<?php
// Check connection
include_once 'config.php';
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "SELECT * FROM records";
$result = $link->query($sql);
function post($key){ return(isset($_POST[$key]) ? htmlspecialchars($_POST[$key]) : ""); }
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if(post('rowButton'.$row['id']) =="Delete"){
$del = mysql_query("DELETE FROM records WHERE id=" . $row['id']);
$deleted = '<p>Entry ' . $row['id'] . ' was succesfully deleted</p>';
}
else {
echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row["visitingdate"]. "</td>";
echo "<td>" . $row["department"] . "</td>";
echo "<td>" . $row["visitingreason"]. "</td>";
echo "<td>" . $row["importance"]. "</td>";
echo "<td>" . $row["visitorname"]. "</td>";
echo "<td>" . $row["company"]. "</td>";
echo "<td>". $row["internalrecipientname"]. "</td>";
echo "<td>". $row["visitinglocation"]. "</td>";
echo "<td>". $row["ETA"]. "</td>";
echo "<td>". $row["ETD"]. "</td>";
echo "<td>". $row["HRverification"]. "</td>";
echo "<td>". $row["visitcompleted"]. "</td>";
echo '<td><input type="submit" name="rowButton'. $row['id'] .'" value="Delete"/> </td>';
echo "</tr>";
echo "</form>";
}
}
echo "</table>";
echo $deleted;
}
else { echo "0 results"; }
$link->close();
?>
First, this appears to be rather vulnerable to SQL injection attacks. StackOverflow is quite font of pointing this out up front, because it's really a solved problem that you should account for in the early stages of development. You're taking untrusted data (that was submitted by the user, without sanitizing it) and putting it directly in an SQL query. Bad things can happen when that occurs. Now with that aside, on to your actual question.
"Nothing happens" means the page doesn't change at all, right? So the browser doesn't know what to do when the button is clicked.
I think you haven't put any <form...> declaration here, which would be required for <input type="submit"> to do anything useful. You could use JavaScript with the stand alone submit button, but I don't see that in your code, either. You'll need something to tell the browser what to do when the submit button is pressed.
I haven't really tested the rest of your code, but based on what you've got already you might add the following:
else {
+ echo '<form action ="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo "<tr>";
and
echo "</tr>";
+ echo "</form>";
}
(don't add the plus sign, that's just to show which line is added). I should add that I don't usually use submit buttons like this, so there's a chance I missed some additional details about how you're calling this, but putting the form in a <form> tag is at least a good start.
Edit
The mysql_query() function was removed in PHP 7; if you're using an older PHP you need to add support for the MySQL functions or if you're on PHP 7, you should use the MySQLi or PDO_MySQL functions instead. The warning box on the PHP manual page for mysql_query has some links for alternatives, how to select an alternative, and other supporting documentation that may help you. This StackOverflow answer may help, as well.

Why is only one row from this query displayed?

I am trying to print the results of this query, but it only prints out the first row. Why is that?
if (isset($_GET['consumiperstanza'])) {
$num_stanza=$_GET['num_stanza'];
$data1=$_GET['data1'];
$data2=$_GET['data2'];
$query="SELECT stanze.num_stanza,consumi.cod_consumo, servizi.nome,
consumi.quantita, servizi.prezzo, consumi.data_c
FROM consumi, servizi, stanze
WHERE stanze.num_stanza=consumi.num_stanza
AND servizi.cod_servizio=consumi.cod_servizio
AND stanze.num_stanza='$num_stanza'
AND consumi.data_c BETWEEN '$data1' AND '$data2'
GROUP BY stanze.num_stanza";
$risultato = $conn->query($query);
if(mysqli_num_rows($risultato) > 0){
echo "<table border=1 bgcolor= 'white' align='center'>";
echo "<tr>";
echo "<th>NUMERO STANZA</th>";
echo "<th>CODICE CONSUMO</th>";
echo "<th>NOME</th>";
echo "<th>QUANTITÀ</th>";
echo "<th>PREZZO</th>";
echo "<th>DATA</th>";
echo "</tr>";
while($riga = mysqli_fetch_array($risultato)){
echo "<tr>";
echo "<td>" . $riga[0] . "</td>";
echo "<td>" . $riga[1] . "</td>";
echo "<td>" . $riga[2] . "</td>";
echo "<td>" . $riga[3] . "</td>";
echo "<td>" . $riga[4] . "</td>";
echo "<td>" . $riga[5] . "</td>";
echo "</tr>";
}
echo "</table><br>";
}
}
This is part of your criteria
AND stanze.num_stanza='$num_stanza'
But you're also doing this.
GROUP BY stanze.num_stanza
So, you'll only get one group.
Additionally, all the other columns in your SELECT are not well-defined since they are not included in the GROUP BY and are not aggregate expressions. Newer versions of MySQL actually will not allow you to do this by default. It is possible in older versions, but may not give you the results you expect.
The MySQL 5.6 manual:
... this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic.

How to put user input into a SQL Query

I have a database and I want the user to be able to have an input into what comes out. i.e
Select from Table where example = user input from box **(input by the user)**
Im guessing what I need is a variable to hold the value that then goes into the statement. I know how to get the value from the input box with script but can I use it like:
select * From handover WHERE hdate = variable. However I am guessing someone is going to talk to me about security if its even possible.
<html><body>
<input>User input</input> //That needs to go into statement
<?php
include 'config.php';
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = **user input**;");
echo "<table border='1'>
<tr>
<th>hdate</th>
<th>Delay</th>
<th>Health and Safety</th>
<th>Non Vsa</th>
<th>VSA</th>
<th>Dar</th>
<th>Other</th>
<th>Hour</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['hdate'] . "</td>";
echo "<td>" . $row['hdelay'] . "</td>";
echo "<td>" . $row['hs'] . "</td>";
echo "<td>" . $row['nv'] . "</td>";
echo "<td>" . $row['vsa'] . "</td>";
echo "<td>" . $row['dar'] . "</td>";
echo "<td>" . $row['other'] . "</td>";
echo "<td>" . $row['hour'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Any help is welcome and advice on the best language to use for this.
Kind Regards
Fintan
first of all, this question has nothing to do with javascript & ajax. so you can delete those tags.
you want to show/search data from mysql.
$result = mysqli_query($con,"SELECT * FROM handover WHERE hdate = '".$_POST['abc']."' ");
this is when you want to check if hdate column have exact data as user input ( $_POST['abc'] ).
and also don't forget to use mysqli_real_escape_string
you can learn common mysql pattern queries from here: http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

id not going through the url

my id is not going through the url. my code is as follows
<?php
include 'library/connect.php';
$result = mysql_query("SELECT * FROM meetings ");
echo "<table border='1'><tr><th>Title</th><th>Chairman</th><th>Secretary</th><th>Terms of Reference</th><th>Named membership</th><th>Occurences</th><th>Book Room</th></tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title']. "</td>";
echo "<td>" . $row['chairman']. "</td>";
echo "<td>" . $row['secretary']. "</td>";
echo "<td>" . $row['termsOfReference']. "</td>";
echo "<td>" . $row['named_membership']. "</td>";
echo "<td>" . $row['occurences']. "</td>";
?>
<td><font color="#CC3300">Book: room/date/time</font></td>
<?php
}
echo "</tr>";
echo "</table>";
include 'library/closedb.php';
?>
have you got any idea of what the problem can be?
where is $meeting_id set? seems to me like it should be $row['meeting_id'].
Where $meeting_id is defined?
It seems like you did a select but forgot to retrieve the meeting id.
Try to change the link to:
<a href ="secretary_booksRoom.php?meeting_id=<?php echo $row['meeting_id']; ?>">
In case you have column named meeting_id in your table of course.
<tr> tag need to be closed in the while loop
You need to define the $meeting_id variable before adding it to the URL, otherwise your link will simply be "secretary_booksRoom.php?meeting_id=". I am going to assume that your meetings table has an id column as a primary key. In your while loop, try declaring "$meetind_id = $row['your meeting id column']". This should get the meeting id and pass it to the URL.
Hope this helps.

PHP MySQL Stored Procedures still slow to output (almost crashes browser)

Hi I asked a question on here a couple of weeks ago about speeding up mysql output for my db of about 5000 records. I used the advice to use ob_start() and stored procedures. However its still almost crashing the browser and being extremely slow to output the records, any ideas how to optimise this:
ob_start();
$conn = new Mysqli("xxxxxxxxxx", "xxxxxxxxx", "xxxxxxxxx", "xxxxxxxxxx");
$result = $conn->query(sprintf("call list_products(%d)", 6000));
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>" . $row['xxxxxxx'] . "</td>";
echo "<td>" . $row['xxxxx'] . "</td>";
echo "<td>" . $row['xxxxx'] . "</td>";
echo "<td>" . $row['xxxxxx'] . "</td>";
echo "<td>" . $row['xxxx'] . "</td>";
echo "<td>" . $row['xxx'] . "</td>";
echo "<td>" . $row['xx'] . "</td>";
echo "<td>" . $row['xxxx'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
$result->close();
$conn->close();
ob_end_flush();
using ob isn't good at all whoever said that have misinformed you... what you should do isn't to much about how you're outputting your data but look in to your mysql query and how you could optimize it as possible, using key columns to find whatever you looping, try to limit the rows as much as possible, using index all of these has great importance when you want to optimize your database script
You can use the EXPLAIN word to find out where the bottleneck might be, where you might need to index and so on

Categories