paging is not working for jquery datatable - php

i am using jquery table to show table on a web page.The table is coming up properly but paging for the table and showing number of records and search options are not working for the table generated.All the records are populating at the load of the page.This is my code
PHP code
$qry = " SELECT AssetId,";
$qry .= $data;
$qry .= " from Completedetails";
mysql_select_db($database_finalkms, $finalkms);
$query_getcolumns = $qry;
$getcolumns = mysql_query($query_getcolumns, $finalkms) or die(mysql_error());
$row_getcolumns = mysql_fetch_assoc($getcolumns);
$totalRows_getcolumns = mysql_num_rows($getcolumns);
if (($getcolumns)||(mysql_errno == 0))
{
echo "<table width='50%' id='sample_2'><thead><tr>";
if (mysql_num_rows($getcolumns)>0)
{
//loop thru the field names to print the correct headers
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
//display the data
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tbody><tr >";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Java Script
<script>
jQuery(document).ready(function() {
// initiate layout and plugins
$("#sample_2").dataTable({"bPaginate": true});
});
</script>
<input name="hdnfld" id="hdnfld" type="hidden" value="<?php echo $qry;?>"/>
Please help me in this regard.

Try the following modified code of yours, if still not working then give the live URl where ur html output is displyed.
if (($getcolumns)||(mysql_errno == 0))
{
// Displying the headers
$i = 0;
echo "<table width='50%' id='sample_2'><thead><tr>";
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
// Data Section
echo "<tbody>";
if (mysql_num_rows($getcolumns)>0)
{
while ($rows = mysql_fetch_array($getcolumns, MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
echo "</tr>";
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}

Related

select table from PHP MyAdmin using PHP file

Good day,
I am trying to select a table from MySQL and generaly I use this code:
$sql="CALL selectCreatedTableByName('".$tableNameIn."')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='restable'>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['nameE'] . "</td>";
echo "<td>" . $row['nameN'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
$conn->close();
but now, when I don't know the names of the table's columns, how can I select this table?
Thank you so much.
You are not selecting the table, you are selecting from the table. (at least I'm guessing that is the case since we don't know what your procedure actually does).
The PHP array returned uses the attribute names from the resultset as keys hence....
while($row = $result->fetch_assoc()) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}
If you want a header row, then use a state variable to flag the first row.
$fetched=0;
while($row = $result->fetch_assoc()) {
if (!fetched) {
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<th>$name</th>\n";
}
echo "</tr>\n";
}
$fetched++;
echo "<tr>\n";
foreach($row as $name=>$value) {
echo "<td>$value</td>\n";
}
echo "</tr>\n";
}

Decrypt data from MySQL database

I have the below code to show all data from a MySQL database in a HTMl database:
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
This code works fine and the data is displayed in the table correctly, my problem is that most of the data in the DB is encrypted (simply)!
For example, here is how someones first name is stored 5cGs+mhdNN/SnHdqEbMk6zlKRu8uW7pR1zRjQHV9DGQ=
Is there a way to decrypt the data before displaying it in the table?
I usually decrpyt the date in the following way:
$name_firstD = simple_decrypt($name_first , "secured");
You need to have an array of columns which are encrypted.
<?php
$result = mysqli_query($con,"SELECT * FROM Persons");
$encrypted_columns = array('password','code', 'first_name');
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td>" . (in_array($key,$encrypted_columns))? simple_decrypt($value , "secured") : $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
put the name of your encrypted column inside $encrypted_columns array.

Gather Table Names + Data using mysqli and php

I am trying to get the column names and data from a table using mysqli and php. Any help is appreciated.
I know how to do this in java by more or less this code:
String [] header= new String[numberOfColumns-1];
//Code to get column names in header array
String table = "<table><tr>"
for(int i=0;i<numberOfColumns-1;i++){
table= table + "<td>" + header[i] + </td>;
}
table = table + </tr>;
while(result.next()!=null){
table = table + <tr>
for (int j = 0 ; j < numberOfColumns-1 ; j++){
table = table + <td> + result.getString[j] + </td>
}
table = table + </tr>
}
But i have no idea how to do it in php. Each table is different but what i have to far for the one table:
include("config.php");
session_start();
$sql = ($_POST['q']);
$result = mysqli_query($db,$sql);
echo "<tr>";
echo "<td>First Name</td>";
echo "<td>Last Name</td>";
echo "<td>Date of Birth</td>";
echo "<td>Contact Details</td>";
echo "</tr>";
while($rowitem = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo "<tr>";
echo "<td>" . $rowitem['fname'] . "</td>";
echo "<td>" . $rowitem['lname'] . "</td>";
echo "<td>" . $rowitem['dob'] . "</td>";
echo "<td>" . $rowitem['contact'] . "</td>";*/
echo "</tr>";
}
echo "</table>"; //end table tag
EDIT: Is the implementation of the fetch_fields correct?
include("config.php");
session_start();
$sql = ($_POST['q']);
$result = mysqli_query($db,$sql);
$finfo = mysqli_fetch_fields($result);
echo "<tr>";
foreach ($finfo as $val) {
echo "<td>" . $val->name . "</td>"
}
echo "</tr>";
while($rowitem = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo "<tr>";
for($x =0; $x < count($finfo);$x++){
echo "<td>" . $rowitem[$x] . "/td";
}
echo "</tr>";
}
echo "</table>";
EDIT 2: Database Connection.php
<<?php
include("config.php");
session_start();
$sql = ($_GET['q']);
$result = mysqli_query($db,$sql);
echo "<table border='1' class='table_info'>";
$finfo = mysqli_fetch_fields($result);
echo "<tr>";
foreach ($finfo as $val) {
echo "<td>" . $val->name . "</td>";
}
echo "</tr>";
while($rowitem = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo "<tr>";
foreach ($row as $value) { (line 18)
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>"; //end table tag
?>
Error:
Notice: Undefined variable: row in databaseConnection.php on line 18
Warning: Invalid argument supplied for foreach() in databaseConnection.php on line 18
You can call mysqli_fetch_fields on the $result object, and retrieve the name property for each object in the returned array. See documentation for examples.
There are a couple ways to do this:
Use array_keys() to grab the names of the keys from an associative array:
$rowitem = mysqli_fetch_array($result, MYSQLI_ASSOC);
$header = array_keys($rowitem);
Use mysqli_fetch_fields() to grab the field objects, and parse out the names into an array using array_map():
// Helper function to use in array_map
// (For PHP < 5.3. If >= 5.3.0, use the second version)
function getFieldName($field) { return $field->name; }
// For PHP < 5.3:
$header = array_map("getFieldName", mysqli_fetch_fields($result));
// For PHP >= 5.3.0:
$header = array_map(function($o) { return $o->name; }, mysqli_fetch_fields($result));
As mentioned you can use $result->fetch_fields() to get the field information from the result and this includes the name of each field.
For what you're trying to acheive this code should work for pretty much any table/query:
<?php
/* Connect to mysql */
$mysqli = new mysqli("localhost", "username", "password", "database");
/* Check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Your query */
$query = "SELECT * from table";
if ($result = $mysqli->query($query)) {
/* Output HTML table */
echo "<table border='1'>";
/* Get field information for all returned columns */
$finfo = $result->fetch_fields();
/* Output each column name in first table row */
echo "<tr>";
foreach ($finfo as $val) {
echo "<td>".$val->name."</td>";
}
echo "</tr>";
/* Fetch the result and output each row into a table row */
while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
$result->free();
echo "</table>";
}
$mysqli->close();
(Obviously you should never query a database from a $_POST variable without some serious sanitisation of the input, but I'll assume that was just for your convenience.)
You can Echo in a table..
$result = mysqli_query($db,$sql)
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysqli_num_fields($result);
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysqli_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysqli_free_result($result);`

Database query info to dropdown menu

I am taking info from a database query and adding it to a dropdown menu form (that is placed inside a table). The query is in a separate function that is called from within the form. It adds the info from the database to the correct location on the table, but it is not in the dropdown menu. I used variables $a, $b, and $c to test my syntax, and it works fine with those variables. Is it an issue with the function call? Any ideas?
Here is the code:
<?php
function fill_dropdown(){
include("../secure/database.php");
$conn = pg_connect(HOST." ".DBNAME." ".USERNAME." ".PASSWORD)
or die('Could not connect: ' . pg_last_error()); //error if could not connect to database
$query = "SELECT country_code, name FROM lab5.country ORDER BY name ASC";
$result = pg_query($query) or die("Unable to execute: " . pg_last_error($conn));
$numRow = 0;
//results are good so output them to HTML
//echo "test<br />";
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
//echo "end test<br />";
}
echo "<table border = \"1\">";
echo "<form method=\"POST\" action=\"exec.php\">"; //save and cancel buttons
for($i=1; $i<5; $i++) //building initial table
{
echo "\t<tr>\n";
echo "\t\t<td>";
if($i == 1)
echo "Name";
elseif($i == 2)
echo "Country Code";
elseif($i == 3)
echo "District";
else
echo "Population";
echo "</td>\n";
echo "<td>\n";
if($i == 1){
echo "<input type=\"text\" name=\"name\">";
}
elseif($i == 2){
echo "<select name=\"country_code\">"; //dropdown box
$c = 0; //these are just to show that this way works
$a = "IOT";
$b = "test2";
$numRow = 1;
echo "<option value=\"IOT\">British Indian Ocean Territory</option>";
echo "<option value=$a>$b</option>";
//echo "<option value=" . $country_name[$numRow] . ">" . $country_name[$numRow] . "</option>";
fill_dropdown();
//echo "<option value=\"Brunei\">Brunei</option>";
echo "</select>";
}
elseif($i == 3){
echo "<input type=\"text\" name=\"district\">";
}
else{
echo "<input type=\"text\" name=\"population\">";
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
echo "\t\t<input type=\"submit\" value=\"Save\" name=\"save\" />";
echo "<input type=\"button\" value=\"Cancel\" onclick=\"top.location.href='" . $_SERVER['HTTP_REFERER'] . "';\" />\n";
echo "</form>";
?>
It would seem you can replace
while ($line1 = pg_fetch_array($result, null, PGSQL_ASSOC)){
$counter = 0;
//echo "test<br />";
foreach ($line1 as $col_value){ // then add all data for attributes in succeeding columns
if($counter == 0){
$code[$numRow] = $col_value;//array($numRow => $col_value);
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$code[$numRow]\" />";
//echo $code[$numRow] . "<br />";
}
elseif($counter == 1){
$country_name[$numRow] = $col_value;
echo "<option value=$country_name[$numRow]>$country_name[$numRow]</option>";
//echo $country_name[$numRow] . "<br />";
}
$counter++;
}
$numRow++;
}
with
while ($row = pg_fetch_assoc($result)) {
// why do you want this line at all?
echo "\t\t<input type=\"hidden\" name=\"code\" value=\"$row[country_code]\"/>";
echo "<option value=\"$row[name]\">$row[name]</option>";
}
The only thing I can see that is wrong, is not having quotes around the option's value attribute. I don't understand what you are expecting to achieve by interleaving hidden inputs with options, though. Judging by your $a, $b, $c template, maybe what you actually want is:
while ($row = pg_fetch_assoc($result)) {
echo "<option value=\"$row[country_code]\">$row[name]</option>";
}

how to generate random password

i just want to have random password from all the query results i did of the students. on my situation, i tried to generate but it returns only 1 generated password to all queried students(about 45 students). how should i make a random one. plss help..
heres my code
<?php
if(isset($_POST['generate'])){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
$generated_pass = substr(str_shuffle($charset), 0, 12);
$generate = $_POST['generate'];
}
?>
<form method="post" action='enroll_student.php' name ='register'>
<?php
$yr = date("Y");
if ($result = $mysqli->query("SELECT
tbl_studentreg.studId,
tbl_studentreg.fname,
tbl_studentreg.lname,
tbl_studentreg.mname,
tbl_studentreg.dob,
tbl_studentreg.address,
tbl_department.departmentName,
tbl_studentreg.sy
FROM tbl_studentreg
Inner Join tbl_department ON tbl_studentreg.departmentId = tbl_department.departmentId WHERE tbl_studentreg.sy = '$yr' "))
{
if ($result->num_rows > 0)
{
echo "<table width= '1000'>";
echo "<tr><th>StudentID</th><th>Name</th><th>Date of Birth</th><th>Address</th><th>Department</th><th>School Year</th><th>Password</th></tr>";
while ($row = $result->fetch_object())
{
echo "<tr>";
echo "<td align='center'>" . $row->studId . "</td>";
echo "<td align='center'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
echo "<td align='center'>".$row->dob."</td>";
echo "<td align='center'>" . $row->address. "</td>";
echo "<td align='center'>".$row->departmentName."</td>";
echo "<td align='center'>".$row->sy."</td>";
if(isset($generated_pass)) {
//how could i make this one generate random password for every students..?
for($i=0; $i <= $row->studId; $i++){
echo "<td>$generated_pass</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
else
{
echo "No Results.";
}
}
else
{
echo "Error: " . $mysqli->error;
}
$mysqli->close();
echo '<br>';
include 'count.php'; //this one will give the total no. of results, just ignore.
?>
<br />
<tr><td></td></tr><tr><td><input type='submit' name='generate' value='Generate'/></td></tr>
</table>
</form>
function genpass(){
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$%^&*()_+';
return substr(str_shuffle($charset), 0, 12);
}
// INSIDE THE LOOP
$generated_pass = genpass();
echo "<td>$generated_pass</td>";
Something like that.

Categories