Data table not working - php

I have a table which I populate using data from MySQL via PHP.
<table id="tab" class="table">
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody>
<?php
session_start();
$id=$_SESSION['id'];
$val=$_SESSION['val'];
if($val=="1")
{
$result = $obj->getAllVals();
}
else
{
$result = $obj->getVal($id);
}
foreach($result as $row) { ?>
<tr style="cursor:pointer" onclick="gp('<?php echo $row['name']; ?>')">
<td> <?php echo $row['name']; ?> </td> </tr>
<?php } ?>
</tbody>
</table>
I'm not able to see my data as a datatable. Here is how I'm calling Data tables:
$(document).ready( function () {
$('#tab').dataTable();
});
Here are the scripts I'm using:
<script src="static/js/jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="static/lib/jquery.dataTables/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="static/lib/jquery.dataTables/js/DT_bootstrap.js"></script>
<script type="text/javascript" src="static/lib/jquery.dataTables/js/datatables.responsive.js"></script>
<script src="static/js/jquery-ui.js" type="text/javascript" charset="utf-8"></script>

Are you sur the variable $result is populated?
What if you replace this:
foreach($result as $row) { ?>
<tr style="cursor:pointer" onclick="gp('<?php echo $row['name']; ?>')">
<td> <?php echo $row['name']; ?> </td> </tr>
<?php } ?>
By this :
foreach($result as $row) { ?>
<tr><td>Test</td></tr>
<?php } ?>
Does it shows a lot of "Test" cells?
If yes, then there is something wrong with getting the infos out of the "$row" variable.
If not, then there is something wrong with either the foreach loop, or with the "$obj->" calls.

You have at least one error, that is outside the
<?php ?>
body:
<tr style="cursor:pointer" onclick="gp(\''.$row['name'].'\')">
=>
<tr style="cursor:pointer" onclick="gp('<?php echo $row['name']; ?>')">

session start methode should be in first line of php file
kindly write
<?php
session_start();
?>

Try to use a while loop, instead a foreach.
while($row = mysql_fetch_array($result)) {
echo "<tr style='cursor:pointer' onclick='gp('".$row['name']."')'>
<td> ".$row['name']." </td>
</tr>";
}

I assume that there are rows in the table and as you say "The console is not throwing up any data table related errors"
in that case you may want to check the non-related errors.
if there is an error it may stop all further script execution.
you should check if this line $('#tab').dataTable(); is executed at all (you can just replace it with alert and see)
and if this is the case the problem will be solved by correcting thous errors

Related

Increament id of td by Loop and getting id in function

I'm populating a table with database data, i want to get value of td that is row1, to perform action in PHP function, I dont know how to do this,
<?php
$sql = "SELECT DISTINCT name,c_no,email,speciality FROM doctor_temp";
$result = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($result))
{?>
<?php $count=1;
$count++;?>
<tr>
<td><?php echo $row[0];?></td>
<td id=<?php echo $count;?>><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><button type="button" class="btn btn-success" onclick="insert()">register</button></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
function insert()
{
var a = document.getElementById($count).innerHTML;
alert(a)
}
</script>
</body>
Watch out! Your code is wrong because you need to put the ID number instead of the PHP variable. If you want to use count you have to put it inside a PHP tag and echo it, but you are mixing the JS with the PHP code.
getElementById(<?php echo $count; ?>).innerHTML
Just look at the console log when you are debugging, please, that makes things a lot easier and it saves you from asking questions that you can answer yourself.
I don’t recommend that you preprocess JS however, that is an extremely bad practise for any programming language.

Mysql and php w/ html database issue

I want to get data from my sql database into a table with html.
I have writed 1st part of code ended with ?>
and started with html table and end with html
My code getting only Empty table without getting data from my Database, here the Code, i dont know how to get data with that
<td>
<?php echo $result['nazwiskoimie'] ?>
</td>
Here's the full code :
<?php
$con=mysqli_connect("localhost","root","","listap");
if (mysqli_connect_errno()) {
echo "Blad przy polaczeniu z baza: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM 'lista'");
?>
<html>
<table border='1'>
<tr>
</tr>
<tr>
<td> <?php echo $result['nazwiskoimie'] ?> </td>
//in this part have problem to get my data from database
</html>
Here's also a screenshot of a result: Final After a Save my file
Please try this:
$result = mysqli_query($con,"SELECT * FROM lista");;
?>
<html>
<table border='1'>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['nazwiskoimie']?></td>
</tr>
<?php
}
} ?>
</table>
</html>

How to set table row to trigger jQuery hover event

When a user hovers the mouse over a table row, to trigger a preview event, only it doesn't react in any way. I think the problem is that the table rows are generated via PHP.
HTML & PHP
<table>
<thead>
<tr>
<th width="200">User</th>
<th width="900">Description</th>
</tr>
</thead>
<tbody>
<?php
require_once './Classes/MainController.php';
$mc = MainController::getInstance();
$threads = $mc->getAllThreads();
foreach ($threads as $thread) {
?>
<tr id='thread_video_preview'>
<td><?php print $thread->original_poster; ?></td>
<td><?php print $thread->description; ?></td>
<td id='see_thread_video_url' class="hide"><?php print $thread->url; ?></td>
</tr>
<?php } ?>
<script type="text/javascript" src="./Javascript/MainFunctions.js"> HoverPreview();
</script>
</tbody>
</table>
Jquery
function HoverPreview() {
$('tr#thread_video_preview').hover(function() {
var url = $(this).$('#see_thread_video_url').html();
$('#dynamicPreview').html('<iframe width="320" height="240" src="//www.youtube.com/embed/' + url + '?autoplay=1" frameborder="0""></iframe>')
});
}
There are several issues here. Firstly, id attributes should be unique. If you want to group elements, use a class. Secondly, the location of your script tag could cause issues. You should place it in the head, or just before </body>. Finally, when you set a src property of a script tag, you cannot place any code in it. You need a separate script tag for that. With all that in mind, try this:
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="./Javascript/MainFunctions.js"></script>
<script type="text/javacript">
$(function() {
$('tr.thread_video_preview').hover(function() {
var url = $(this).find('.see_thread_video_url').html();
$('#dynamicPreview').html('<iframe width="320" height="240" src="//www.youtube.com/embed/' + url + '?autoplay=1" frameborder="0""></iframe>')
});
});
</script>
</head>
In the body:
<?php
require_once './Classes/MainController.php';
$mc = MainController::getInstance();
$threads = $mc->getAllThreads();
foreach ($threads as $thread) { ?>
<tr class='thread_video_preview'>
<td><?php print $thread->original_poster; ?></td>
<td><?php print $thread->description; ?></td>
<td class='see_thread_video_url' class="hide"><?php print $thread->url; ?></td>
</tr>
<?php }
?>
Change this
<tr id='thread_video_preview'>
To :
<tr class='thread_video_preview'>
And in jquery event use
$(".thread_video_preview").
And I think it is good practice to use properties or event listener in td instead of tr

hyperlink trouble in php echo'd table

I"m having trouble with my code hopefully someone can help.
I'm trying to call information using "php echo" to display information in table form and it works except for the links which doesn't recognize the $id. If I don't put it in the table form it works fine but it is not aesthetically appealing.
Any suggestions would be greatly appreciated!
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location: ../index.php");
exit();
}
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("../scripts/connect.php");
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete messages with ID of ' . $_GET['deleteid'] .'? Yes | No';
exit();
}
if (isset($_GET['yesdelete'])) {
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM `mystore`.`messages` WHERE `messages`.`id` = '$id_to_delete' LIMIT 1") or die (mysql_error());
}
$messages = "";
$sql = mysql_query("SELECT * FROM messages ORDER BY msg_date DESC LIMIT 20");
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td>'.$row['msg_name'].'</td>';
echo '<td>'.$row['msg_email'].'</td>';
echo '<td>'.$row['msg_subject'].'</td>';
echo '<td>'.$row['msg_date'].'</td>';
echo '<td>Reply</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
}else{
$messages = "<b>There are no messages in the database at this moment</b>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Messages</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style/forms.css" media="screen">
<link rel="stylesheet" href="style/main.css" media="screen">
</head>
<body>
<div id="main_wrapper">
<?php include_once("templates/tmp_header.php"); ?>
<?php include_once("templates/tmp_nav.php"); ?>
<section id="main_content">
<h2 class="page_title">Messages</h2>
<br/>
<table width="730" cellspacing="0" cellpadding="3" border="1">
<tr>
<td align="center" width="100">From</td>
<td align="center" width="300">Email</td>
<td align="center" width="300">Subject</td>
<td align="center" width="100">Date</td>
<td align="center" width="100">Actions</td
></tr>
<?php echo $messages; ?>
</table>
</section>
<?php include_once("templates/tmp_aside.php"); ?>
<?php include_once("templates/tmp_footer.php"); ?>
</div>
Please change
echo '<td>Delete</td>';
to
echo "<td><a href='admin_messages.php?deleteid=$id'>Delete</a></td>";
when trying to print out a variable the main string has to be wrapped in double quotes.
If you want to interpolate variables in PHP, you need to use double quotes. echo '$id' will literally print $id, whereas echo "$id" will print the value of the variable. However, I would recommend an alternative approach. Don't use PHP where it isn't needed. There's no need to use echo so much.
I would change the contents of your loop to this:
?>
<tr>
<td><?=$row['msg_name']?></td>
<td><?=$row['msg_email']?></td>
<td><?=$row['msg_subject']?></td>
<td><?=$row['msg_date']?></td>
<td>Reply</td>
<td>Delete</td>
</tr>
<?php
The <?=$id?> is shorthand for <?php echo $id?> and is supported by default in PHP versions >=5.4.0. You can also use it in previous versions if you enable short_open_tags.
As stated in the comments, you should really be using mysqli functions, as mysql functions are deprecated.

Let a div get a value from a fetch-array-table row after click on a cell with jquery

first I want to say that this question is related to jquery replacewith to get data with Ajax after click on a cell and if this against the rule I am sorry and delete this post.
I tried to get the value of a cell in a MySQL-generated table (with fetch_array). I want to click on a cell in the same and receive the value of the first cell in the row in a div-container.
Thanks to several entries here I found an example for "static" tables. Like this:
<table id="choose-address-table" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name/Nr.</th>
<th>Street</th>
<th>Town</th>
<th>Postcode</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td class="nr"><span>50</span>
</td>
<td>Some Street 1</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td class="use-address">
Use
</td>
</tr>
<tr>
<td id="chip" class="nr">49</td>
<td>Some Street 2</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td>
<button type="button" class="use-address">Use</button>
</td>
</tr>
</tbody>
</table>
<br><br>
<div id="glasgow">Hello</div>
and
$("#chip").click(function () {
var scotland = $(this).closest("tr").find(".nr").text();
$("#glasgow").html(scotland);
});
http://jsfiddle.net/FnDvL/231/
Works fine. I insert this to my file, php, and get table data from mysql.
This is my code:
<html>
<head>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?
$con = mysqli_connect('localhost','root','','boerse');
$sql="SELECT short, name FROM markets";
$result = mysqli_query($con,$sql);
?>
<div class="markets">
<?
echo "<table>
<thead border='0'>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
</thead>
<tbody border='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='nr'>" . $row['short'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td class='information'>Use</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
<div class="clicked_info">Hello</div>
</div>
<br><br>
</body>
</html>
Now my problem is that I can click on the td with class information, but div class 'clicked_info' doesn't change. But I did like in the fiddle. So what went wrong? Or where is the problem? It must have to do with the MySQL-matter. But why?
This way I want to check if I got the content from the cell and I can continue working with it (as seen in my post before).
Maybe anyone can help. And again I am sorry if I break rules here.
Thank you to everyone.
You need to wrap your code in document-ready handler
$(document).ready(function () {
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
});
Currently when your script executes there is no $(".information") thus event is not binded properly.
OR
Move your script at the bottom of your page. like
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
</body>
OR
You can use event-delegation
$(document).on("click", ".information",function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});

Categories