Pagination in table that gets created in PHP - php

I have a PHP file that also has HTML/JS in it, and inside that file i use AJAX to call another PHP file which runs some queries and then echoes the results as a table back to the original php file. What i want to do is paginate the resulting table.
Searching i found this https://stackoverflow.com/questions/19605078/how-to-use-pagination-on-html-tables#= older question so i tried replicating the accepted asnwer which points to this site https://datatables.net/examples/basic_init/alt_pagination.html ,but to no result (not even an error message, which hints that i definitely lack understading of something basic).
Here is my code in the php file that gets called by AJAX:
echo '<div class="table-responsive">';
echo '<table id="resultsTable" class="table table-hover table-condensed" align="center" style="width:80%">';
echo '<thead>';
echo '<tr>';
echo '<th>Πληροφορίες</th>';
echo '<th>Εξώφυλλο</th>';
echo "<td><a href='search.php?Order=1&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τίτλος</button></a></td>";
echo "<td><a href='search.php?Order=2&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Συγγραφέας</button></a></td>";
echo "<td><a href='search.php?Order=3&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Εκδότης</button></a></td>";
echo "<td><a href='search.php?Order=4&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Είδος</button></a></td>";
echo "<td><a href='search.php?Order=5&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Θέμα</button></a></td>";
echo "<td><a href='search.php?Order=6&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>ISBN</button></a></td>";
echo '<th>Θέση</th>';
echo "<td><a href='search.php?Order=7&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Τωρινός Κάτοχος</button></a></td>";
echo "<td><a href='search.php?Order=8&SearchOption=".$SearchOption."&search_db=".$SearchValue."'><button type='button' class='btn btn-primary'>Προστέθηκε</button></a></td>";
if($LoggedIn == 1)
{
echo "<th>Αλλαγή Καταχώρησης</th>";
}
echo '</tr>';
echo '</thead>';
//body
echo '<tbody>';
for($i=0;$i<$counter;$i++)
{
echo ' <tr>';
echo "<td href='#' onclick=\"alert('".$ResultTable[$i][10]."');\" style='text-align: center;'><img src='IconImage.png' width='25' height='25' title='".$ResultTable[$i][10]."'/></td>";
echo "<td><img src='BookCovers/". $ResultTable[$i][0] ."' class='img-thubnail' alt='Δεν υπάρχει' width='50' height='50'/></td>";
echo "<td>".$ResultTable[$i][1]."</td>";
echo "<td>".$ResultTable[$i][2]."</td>";
echo "<td>".$ResultTable[$i][3]."</td>";
echo "<td>".$ResultTable[$i][4]."</td>";
echo "<td>".$ResultTable[$i][5]."</td>";
echo "<td>".$ResultTable[$i][6]."</td>";
echo "<td>".$ResultTable[$i][7]."</td>";
echo "<td>".$ResultTable[$i][8]."</td>";
echo "<td>".$ResultTable[$i][9]."</td>";
if($LoggedIn == 1)
{
echo "<td style='text-align: center;'><button type='button' class='btn btn-info'><span class='glyphicon glyphicon-wrench'></span></button>
<button type='button' class='btn btn-danger'><span class='glyphicon glyphicon-remove'></span></button></td>";
}
echo '</tr>';
}echo '</tbody>';echo '</table>';echo "</div>";
And in the original php file i included the following in the head block
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src ="jquery-1.12.3.js"></script>
<script src ="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
And just before closing the body i have
<script>$(document).ready(function() {
$('#resultsTable').DataTable( {
"pagingType": "full_numbers"
} );} );</script>
When i open the page, the results are displayed correctly, but the table is not formatted at all. Any help would be greatly appreciated.

Ok, i found the answer. The problems in the code were actually 2.
I had included 2 different versions of the jquery.
The AJAX call that was creating the table was...well..asynchronous, so the execution flow was reaching the jquery part while the table was not constructed yet. This is why i never saw an error message in the console.
I moved the jquery code from the head tag to the end of the AJAX call and it worked correctly.

Related

PHP Hiding a button if data exist

I'm trying to Hide the button if "name" is empty
no error show but the button is still showing I tried adding css to it but the button still show
<table style="width:100%" border="1px">
<tr align="center">
<th>Name</th>
<th>Image</th>
<th>PDF</th>
<th>Grade</th>
<th>Date</th>
</tr>
<?php
foreach ($records as $value) {
$id = $row['id'];
echo '<tr align="center">';
echo "<td>" . $value['name'] . "</td>";
if ($value['name'] != ''){
echo "<td> <a class='design' style='display:none;' href='".$value['img_name']."'>Download image</a></td>";
}else{
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
echo "<td><a class='cert' href='".$value['pdf_name']."' download>Download PDF</a></td>";
echo "<td>". $value['score']."</td>";
echo "<td>". $value['date']."</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Just add the class hide to the first if condition and add display: none to that class
HTML:
if ($value['name'] != ''){
echo "<td> <a class='design hide' href='".$value['img_name']."'>Download image</a></td>";
}else{
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
CSS:
.hide { display: none; }
PHP has a function called empty, which can be used to determine if a variable is empty or not.
For your situation, I don't see why you would want PHP to output a button if it's simply going to be hidden by CSS afterwards.
In PHP, you could use the empty function like this:
<?php
$var4you = "";
if (!empty($var4you)) {
//Display only when there is a value
}
?>
For you to you this within your code, you'd likely want to do this:
<?php
if (!empty($value['name'])) {
echo "<td> <a class='design' href='".$value['img_name']."'>Download image</a></td>";
}
?>
You can learn more about using the empty function in PHP in the docs: https://www.php.net/manual/en/function.empty.php

multiple results with mysql and php in modal of bootstrap

I am trying to print in the modal the information of the selected user but only the first row is printed in the modal.
I am using PHP with MySQL but it is not working. I don't understand why it prints only the first row.
Please help to resolve my issue.
This is the php code:
include ("conex.php");
$query = "SELECT * FROM empleado WHERE cargo='personal'";
$datos=mysqli_query($conex,$query);
echo "<div class='tables'>";
echo "<h3 class='title1'>Listado de Empleados :</h3>";
echo "<div class='panel-body widget-shadow'>";
echo "<table class='table'>";
echo "<button class='btn btn-success add-prod'>
<span class='fa fa-plus'></span>
Agregar Empleado
</button>";
echo "<thead><tr><th>Rut</th><th>Nombre</th><th>Apellido</th><th class='action'>Accion</th></thead>";
while($fila=mysqli_fetch_assoc($datos))
{
echo "<tbody>";
echo "<tr title='informacion detallada' class='selector-empleado' data-toggle='modal' data-target='#infodetallada'>";
echo "<td>".$fila["rut"]."</td>";
echo "<td>".$fila["nombre"]."</td>";
echo "<td>".$fila["apellido"]."</td>";
echo "<td class='buttons-admin'><button title='modificar empleado' class='btn btn-primary btn-admin1'><span class='fa fa-refresh'></span></button>
<button onClick='window.location=\"eliminarempleado.php?rut=".$fila["rut"]."\";'/ title='eliminar empleado' class='btn btn-danger btn-admin2'>
<span class='fa fa-times'></span></button></td>";
echo "</tr>";
echo "</tbody>";
echo "<div class='modal fade' id='infodetallada' role='dialog'>";
echo "<div class='modal-dialog'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal'>×</button>";
echo "<h4 class='modal-title'>Informacion detallada :</h4>";
echo "</div>";
echo "<div class='modal-body'>";
echo "<b>Rut : </b>" .$fila["rut"]."<br>";
echo "<b>Nombre : </b>" .$fila["nombre"]."<br>";
echo "<b>Apellido : </b>" .$fila["apellido"]."<br>";
echo "<b>Cargo : </b>" .$fila["cargo"]."<br>";
echo "<b>Correo : </b>" .$fila["correo"]."<br>";
echo "<b>Contraseña : </b> ************";
echo "</div>";
echo "<div class='modal-footer'>";
echo "<button title='modificar empleado' class='btn btn-primary'><span class='fa fa-refresh'></span> Modificar</button>";
echo "<button onClick='window.location=\"eliminarempleado.php?rut=".$fila["rut"]."\";'/ title='eliminar' class='btn btn-danger'><span class='fa fa-times'></span> Eliminar </button>";
echo "<button type='button' class='btn btn-default' data-dismiss='modal'>Cerrar</button>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
echo "</table>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='clearfix'></div>";
echo "</div>";
echo "</div>";
mysqli_close($conex);
You just have to make couple of minor changes.
Append some unique id to the data-target attribute in the clickable element, so that it is unique for each record
<tr title='informacion detallada' class='selector-empleado' data-toggle='modal' data-target='#infodetallada".$fila['rut']."'>
Append same unique id to the id attribute of the modal, so that it is same to the respective clickable element.
<div class='modal fade' id='infodetallada".$fila['rut']."' role='dialog'>
I assumed that $fila['rut'] will be unique for each of the records. If it is not so, then you can use some other unique elements.
Issue: Each model box should be connected on its own unique ID. It should have unique 'data-target' in the clickable element & 'id' in the respective modal.

how to make a link from the records in mysql?

I have here the code for displaying the records form database..what i want is that i want to make the username a link,and when i click the link username it will open a new window..can somebody please help me with it..because i tried to search from the internet but it not working..
heres the code
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td> ".$row['user_id']." </td>";
echo "<td> ".$row['username']." </td>";
echo "<td> ".$row['password']." </td>";
echo '<td><a style="float:left" href="VIEWSAMPLE.PHP?user_id=' . $row["user_id"] . '>'.$row["username"].'"<input name="image" type="image" value="edit"><image src="image/EDIT.png" class="img-responsive" width="25px"></a>
<a style="float:left" href="delete.php?user_id=' . $row["user_id"] . '>" <input name="image" type="image" value="delete" onclick="return confirm(\'are you sure?\')"><image src="image/DELETE.png" class="img-responsive" width="25px"> </a></td>';
echo "</tr>";
}
}
else
{
echo "<tr>";
"<td>Nothing here...</td>";
"</tr>";
}
}
}
?>
here is the code for pop_up(this)
function pop_up(url){
window.open(url,'win2','status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=800,height=600,directories=no,location=no')
}
Please post the Javascript code for
pop_up(this)
If any codes written for new window under js method then remove
target="_blank"
in anchor tag.

Bootstrap 3 Model displaying the same row from MYSQL database

I am new to PHP and MYSQL, I am trying to display news data from a MYSQL db into a models. Each model should display a different [longDesc] I have the headlines and date showing correctly on the html page but when you click the button and the model pops up its only displaying the first row [longDesc] from the database across all 4 models.Instead of seperate rows, so effectively only one news story in each of the models. Can anyone point my in the right direction please.
Here is the code I am using:
<?php
$query2 = mysql_query("SELECT headline, byline, source, longDesc, newsAbbr, addedOn FROM tbl_news ORDER BY addedOn DESC LIMIT 3");
while ($temp2 = mysql_fetch_array($query2)) {
echo "<div class='row'>";
echo "<h3>".$temp2['headline']."</h3>";
echo "<p>".date("F d, Y",strtotime($temp2['addedOn']))."</p>";
echo "<a href='#' class='btn btn-sm btn-danger' data-toggle='modal' data- target='#largeModal'>Read More";
echo "</a>";
echo "</div>";
echo "<div class='modal fade' id='largeModal' tabindex='-1' role='dialog' aria-labelledby='largeModal' aria-hidden='true'>";
echo "<div class='modal-dialog modal-lg'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×";
echo "</button>";
echo "<h5 class='modal-title' id='myModalLabel'>News</h5>";
echo "</div>";
echo "<div class='modal-body'>";
//this is the value called from the db that displays across all models (need the diff rows to be called????
echo "<p>".stripslashes($temp2['longDesc'])."</div>";
echo "</div>";
echo "<div class='modal-footer'>";
echo "<button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>";
echo "</div>";
echo "</div>";
echo "</div>";
}?>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>

redirect a page after button click and database update

I am trying to get a page to redirect after I click the button and after the database updates but it doesnt seem to work.
I have a form that completes in itself, to cut down on the number of pages used (e.g no, thanks content has been edited" page) but after the content has been edited i wish to redirect back home.
form and database update:
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
$tagbrief = $_POST['tag'];
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
?>
JS validation for ref
<script type="text/javascript">
function valText(){
var text = document.getElementById('tag');
var div = document.getElementById('errormsg');
var lets = /^[0-9a-zA-Z\s\-\'(\)\&\,\:\.\!\?]+$/;
if((text.value == '') || (text.value == ' ')){
div.innerHTML="<b>Please enter your changes</b>";
text.focus();
return false;}
else if(text.value.match(lets)){
div.innerHTML="<b>Content updated</b>";
return true;}
else {
return false;}
}
Any help appreciated, thanks.
I'm not sure I fully follow but I would rejigger it so it was like this:
<?
if($_POST)
{
$tagbrief = mysql_real_escape_string($_POST['tag']);
mysql_query("UPDATE quackedup SET tagbrief='$tagbrief' WHERE id='1'");
header("Location: /path/to/script");
exit;
}
?>
<div id = "errormsg"> </div>
<?php
echo "<table border=\"0\" align=\"center\" cellpadding=\"2\" cellspacing=\"7\" style=\"font-family:Arial;font-size:11px\">";
echo "<tr>";
echo "<td> </td>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><form name=\"confhome\" method=\"post\" action=\"\" onsubmit=\"return valText()\"><textarea name=\"tag\" cols=\"20\" rows=\"3\" id=\"tag\">$row[tagbrief]</textarea></td></tr>";
echo "<tr><td><input type=\"submit\" value=\"Edit\"></form></td></tr>";
echo "<tr></tr>";
echo "</table><br/>";
?>
Now, it only updates the field if you are actually posting the form, and the form contents are escaped for the database (to prevent SQL injection). Just change the /path/to/script to be the path/url you want to go to.
I think you just need to write
header("Location:/");
I hope this will help you

Categories