PHP- insert innerhtml with javascript dynamically in each row of table - javascript

InnerHtml not working in this case, i want to do something like this:
<table width="100%" border="0" id="table2">
<?php
include("connection.php");
$sql=mysql_query("select* from b1");
while($res=mysql_fetch_array($sql))
{
$img=$res["img"];
$url=$res["url"];
?>
<tr>
<td><script>document.body.innerHTML="<a href='"+<?php echo '$url' ; ?>.value+"'><img src='"+<?php echo '$img' ; ?>+"' / ></a>" ;</script></td>
</tr>
<?php
}
?>
</table>

Why don't you just do this?
<table width="100%" border="0" id="table2">
<?php
include("connection.php");
$sql=mysql_query("select* from b1");
while($res=mysql_fetch_array($sql))
{
$img=$res["img"];
$url=$res["url"];
?>
<tr>
<td><img src="<?php echo $img; ?>" / ></td>
</tr>
<?php
}
?>
</table>

Related

Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\3s_shop\admin\pembelian_data.php on line 6 error paging: [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
This is my form distributor data code.
If I click the button before it would be an error
Warning: mysql_query() expects parameter 2 to be resource, null given in C:\xampp\htdocs\3s_shop\admin\pembelian_data.php on line 6
error paging:
What's wrong in the code?
<?php
# UNTUK PAGING (PEMBAGIAN HALAMAN)
$baris = 50;
$hal = isset($_GET['hal']) ? $_GET['hal'] : 0;
$pageSql = "SELECT * FROM pembelian";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());
$jml = mysql_num_rows($pageQry);
$maksData = ceil($jml/$baris);
?>
<table width="800" border="0" cellpadding="2" cellspacing="1" class="table-border">
<tr>
<td colspan="2" align="right"><h1><b>DATA PEMBELIAN</b></h1></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" align="right"><img src="../images/btn_add_data.png" height="30" border="0" /></td>
</tr>
<tr>
<td colspan="2"><table class="table-list" width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="35" align="center" bgcolor="#CCCCCC">No</th>
<th width="250" bgcolor="#CCCCCC">Nama Reseller</th>
<th width="100" bgcolor="#CCCCCC">Tanggal Pembelian</th>
<th width="250" bgcolor="#CCCCCC">Nama Produk</th>
<th width="150" bgcolor="#CCCCCC">Harga Produk </th>
<th width="50" bgcolor="#CCCCCC">Jumlah Produk </th>
<th width="150" bgcolor="#CCCCCC">Total Harga </th>
<td colspan="2" align="center" bgcolor="#CCCCCC"><strong>Tools</strong></td>
</tr>
<?php
$mySql = "SELECT * FROM pembelian ORDER BY nm_reseller, tgl_pembelian, nama_barang, hrg_barang, qty ASC LIMIT $hal, $baris";
$myQry = mysql_query($mySql, $koneksidb) or die ("Query salah : ".mysql_error());
$nomor = $hal;
while ($myData = mysql_fetch_array($myQry)) {
$nomor++;
$Kode = $myData['kd_pembelian'];
// Warna baris data
if($nomor%2==1) { $warna=""; } else {$warna="#F5F5F5";}
?>
<tr bgcolor="<?php echo $warna; ?>">
<td align="center"><?php echo $nomor; ?></td>
<td><?php echo $myData['nm_reseller']; ?></td>
<td><?php echo $myData['tgl_pembelian']; ?></td>
<td><?php echo $myData['nama_barang']; ?></td>
<td><?php echo $myData['hrg_barang']; ?></td>
<td><?php echo $myData['qty']; ?></td>
<td width="44" align="center">Edit</td>
<td width="44" align="center">Hapus</td>
</tr>
<?php } ?>
</table></td>
</tr>
<tr class="selKecil">
<td><b>Jumlah Data :</b> <?php echo $jml; ?> </td>
<td align="right"><b>Halaman ke :</b>
<?php
for ($h = 1; $h <= $maksData; $h++) {
$list[$h] = $baris * $h - $baris;
echo " <a href='?open=Pembelian-Data&hal=$list[$h]'>$h</a> ";
}
?> </td>
</tr>
</table>
I think you are not establishing connection to your database. The second input should be that connection variable. like this:
$link = mysql_connect("localhost","root","");
$koneksidb = mysql_select_db('databaseName', $link);
$pageSql = "SELECT * FROM pembelian";
$pageQry = mysql_query($pageSql, $koneksidb) or die ("error paging: ".mysql_error());

Bootstrap dataTable not working with database in Codeigniter

I am new in bootstrap and codeigniter, I don't know why dataTable not working with codeigniter.
I've tried to use it just in Bootstrap. It's working fine (pagination, data entry, sorting, and searching). But when I use it in Codeigniter with same code in my View file, it seems like datatable is not working. It does load the data, but the features (pagination, data entry, sorting, and searching) is not showing.
anybody can help me? thanks a bunches !
my old file using Bootstrap only
<table id="myTable" class="table table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>NPM</th>
<th>Kelas</th>
<th>MP</th>
<th>PK</th>
<th>Pesan</th>
</tr>
</thead>
<tbodys>
<?php $no=0 ; $sql=m ysqli_query($con, "SELECT * FROM form"); while($row=m ysqli_fetch_array($sql)){ $no++; ?>
<tr>
<td>
<?php echo $no ?>
</td>
<td>
<?php echo $row[ 'Nama'] ?>
</td>
<td>
<?php echo $row[ 'NPM'] ?>
</td>
<td>
<?php echo $row[ 'Kelas'] ?>
</td>
<td>
<?php echo $row[ 'MP'] ?>
</td>
<td>
<?php echo $row[ 'PK'] ?>
</td>
<td>
<?php echo $row[ 'Pesan'] ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script>
$(document).ready(function() {
$('#myTable').DataTable();
});
</script>
here is my View file in Codeigniter + Bootstrap
<script type="text/javascript">
jQuery(function($) {
var oTable1 =
$('#dynamic-table')
.dataTable({
bAutoWidth: false,
"aoColumns": [{
"bSortable": false
},
null, null, null, null, null, null, null, {
"bSortable": false
}
],
"aaSorting": [],
});
</script>
<table id="dynamic-table" method="post" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<a class="thumbnail" href="<?php echo base_url().'uploads/'.$row->file ?>">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" width="50" height="50">
</a>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
the dataTable include in its template, before I display data from database, it working. But when I display data from database, it just a normal table
Put javascript code after end of table tag,
<table id="dynamic-table" method="post" class="table table-striped table- bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Nama Lengkap</th>
<th>Tanggal Mulai</th>
<th>Tanggal Akhir</th>
<th>Alasan</th>
<th>File</th>
<th>Nopeg</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php foreach ($hasil->result() as $row) { ?>
<tr>
<td>
<?php echo $row->no ?></td>
<td>
<?php echo $row->nama ?></td>
<td>
<?php echo $row->tglm ?></td>
<td>
<?php echo $row->tgla ?></td>
<td>
<?php echo $row->alasan ?></td>
<td>
<a class="thumbnail" href="<?php echo base_url().'uploads/'.$row->file ?>">
<img src="<?php echo base_url().'uploads/'.$row->file ?>" width="50" height="50">
</a>
</td>
<td>
<?php echo $row->nopeg; ?></td>
<td>
<button type="button" class="btn btn-pink btn-sm">Terima</button>
<button type="button" class="btn btn-info btn-sm">Tolak</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
jQuery(function($) {
var oTable1 =
$('#dynamic-table')
.dataTable({
bAutoWidth: false,
"aoColumns": [{
"bSortable": false
},
null, null, null, null, null, null, null, {
"bSortable": false
}
],
"aaSorting": [],
});
</script>
Try like this :)

javascript unkown error on css code line

I have this html table:
<center><input type="button" value="Print last visit report" id="printpagebutton" onclick="printData()"/>
<input type="button" value="Print all reports" id="printpagebutton2" onclick="printData1()"/>
</center></BR>
<center></center>
<div class=WordSection1 id="mydiv1">
<?php while($rows = mysqli_fetch_array($result)) { $data2 =$rows['echo_files'];
$dataFile2 = str_replace('/', '\\', $data2); ?>
<div id="mydiv">
<form action="/clinic form/update/update.php" id="Form2" method="post">
<table border="1" align="center" id="table">
<tr align="center">
<th colspan="3" bgcolor="#7a7878">DR. Omar GHNEIM - Patient Medical History</th>
</tr>
<tr align="center">
<th colspan="3"><?php echo 'Medical History of '.$rows['name']?></th>
</tr>
<tr>
<th>Medicaments</th>
<th>Illness</th>
<th>Echo Results</th>
</tr>
<tr>
<?php if(!empty($rows['remarcs']) && trim($rows['remarcs'])!=''):?>
<td width="250px"><?php echo $rows['remarcs'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['illness']) && trim($rows['illness'])!=''):?>
<td width="250px"><?php echo $rows['illness'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['echo']) && trim($rows['echo'])!=''):?>
<td width="250px"><?php echo $rows['echo'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
</tr>
<tr>
<th>Clinic Test Result</th>
<th>Habbits</th>
<th>Allergy</th>
</tr>
<tr>
<?php if(!empty($rows['test_res']) && trim($rows['test_res'])!=''):?>
<td width="250px"><?php echo $rows['test_res'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['habbits']) && trim($rows['habbits'])!=''):?>
<td width="250px"><?php echo $rows['habbits'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['allergy']) && trim($rows['allergy'])!=''):?>
<td width="250px"><?php echo $rows['allergy'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
</tr>
<tr>
<th>Occupation</th>
<th>PMHx</th>
<th>PSHx</th>
</tr>
<tr>
<?php if(!empty($rows['occup']) && trim($rows['occup'])!=''):?>
<td width="250px"><?php echo $rows['occup'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['pmhx']) && trim($rows['pmhx'])!=''):?>
<td width="250px"><?php echo $rows['pmhx'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<?php if(!empty($rows['pshx']) && trim($rows['pshx'])!=''):?>
<td width="250px"><?php echo $rows['pshx'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
</tr>
<tr>
<th colspan="3">Echo Files</th>
</tr>
<tr>
<?php if(!empty($data2) && trim($data2)!=''):?>
<td colspan="3" align="center"><center><a href='download_PopUp.php?data=<?php echo $dataFile2; ?>'>Echo Test files exist</a></center></td>
<?php else: ?>
<td colspan="3" align="center"><center>No echo files</center></td>
<?php endif;?>
</tr>
<tr>
<th>P.E</th>
<?php if(!empty($rows['pe']) && trim($rows['pe'])!=''):?>
<td width="250px"><?php echo $rows['pe'] ?></td>
<?php else: ?>
<td width="250px" align="center">Not Available</td>
<?php endif; ?>
<th rowspan="4" align="left">Signature</th>
</tr>
<tr>
<th>Date</th><td><?php echo $rows['date'] ?></td>
</tr>
<tr>
<th>Address of Patient</th><td><?php echo $rows['address'] ?></td>
</tr>
<tr>
<th>Phone Number of patient</th><td><?php echo $rows['phone_num'] ?></td>
</tr>
</table>
And this javascript file to print a div in a page with a css, but dreamweaver software give an error:
function printData()
{
var divToPrint=document.getElementById("mydiv");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
var css =`table, td, th {
border: 1px solid black;
text-align:justify;
}
th {
background-color: #7a7878;
text-align:center
}`;
var div = $("<div />", {
html: '­<style>' + css + '</style>'
}).appendTo( newWin.document.body);
newWin.print();
newWin.close();
}
$('button').on('click',function(){
printData();
});
But in the browser when I hit F12, nothing is given as error, and the css style won't work.
Template strings are not supported in IE. That's probably why dreamweaver throws an error on that line.
Either escape newlines:
var myString = 'some \n\
multiline \n\
string';
Or concatenate lines:
var myString = 'some \n' +
'multiline \n' +
'string';
A multiline string in javascript can be made like that in most modern browsers but does not work in IE and older browsers.
You should change
var css =`table, td, th {
border: 1px solid black;
text-align:justify;
}
th {
background-color: #7a7878;
text-align:center
}`;
to:
var css = "table, td, th { \n\
border: 1px solid black; \n\
text-align:justify; \n\
} \n\
\n\
th { \n\
background-color: #7a7878; \n\
text-align:center \n\
}";
It is even better to make it:
var css = "table, td, th { \n"+
" border: 1px solid black; \n"+
" txt-align:justify; \n"+
"} \n"+
"th { \n"+
" background-color: #7a7878; \n"+
" text-align:center; \n"+
"}";
The reason that your css is not displayed is:
Once IE has processed all the styles loaded with the page, the only reliable way to add another stylesheet is with document.createStyleSheet(url)
See the MSDN article on createStyleSheet for a few more details.
edit
So in your case, you can add the above css code to table.css, upload it to your server. And at the moment you want to add the style, you use
document.createStyleSheet('http://yourserver.com/table.css');

How to redesign a table when there isn't some data

I have a table that shows some data from database. I want if there isn't data in some <td> , the column from next row, to be displayed in its place. This is what I mean:
If I have this kind of table:
When there is no data for example for $data['created_at'], it should be displayed $data['due_date'] in its place.
How could I do that?
<table>
<tr>
<td colspan="3">
<?php if(!empty($data['created_at'])){ ?>
<strong><?php echo "Created Date:";?>:</strong> <?php echo $data['created_at']; ?>
<?php } ?>
</td>
<td colspan="3">
<?php if(!empty($data['username'])){ ?>
<strong><?php echo "Username:";?>:</strong> <?php echo $data['username']; ?>
<?php } ?>
</td>
</tr>
<tr>
<td colspan="3">
<?php if(!empty($data['due_date'])){ ?>
<strong><?php echo "Date delivery:";?>:</strong> <?php echo $data['due_date']; ?>
<?php } ?>
</td>
<td colspan="3">
<?php if(!empty($data['copmany'])){ ?>
<strong><?php echo "Company:";?>:</strong> <?php echo $data['copmany']; ?>
<?php } ?>
</td>
</tr>
</table>
Are you looking to just omit a row if the result is empty? Simply change the place of your if statement to encompass the td
<table>
<tr>
<?php if(!empty($data['created_at'])){ ?>
<td colspan="3">
<strong><?php echo "Created Date:";?>:</strong> <?php echo $data['created_at']; ?>
</td>
<?php } ?>
<?php if(!empty($data['username'])){ ?>
<td colspan="3">
<strong><?php echo "Username:";?>:</strong> <?php echo $data['username']; ?>
</td>
<?php } ?>
</tr>
<tr>
<?php if(!empty($data['due_date'])){ ?>
<td colspan="3">
<strong><?php echo "Date delivery:";?>:</strong> <?php echo $data['due_date']; ?>
</td>
<?php } ?>
<?php if(!empty($data['copmany'])){ ?>
<td colspan="3">
<strong><?php echo "Company:";?>:</strong> <?php echo $data['copmany']; ?>
</td>
<?php } ?>
</tr>
</table>

unable to auto refresh two tables in HTML

I would like to ask, why I can't able to refresh two html tables? only 1? is there something wrong with the Javascript code? I already tried to declare 2 variables and tried to create to script for each and still no luck and I tried to change the variables name to unique one but still no luck. My full code below:
<!-- SALES.HTML -->
<div class="col-sm-5 shadow">
<p>
<h3><center>Hyukies</center></h3>
<center>Catbalogan, City</center>
<center><span class="glyphicon glyphicon-time"></span> <?php include('time.php'); ?></center> <br />
<div id="transactions_left">
<?php include_once('salestable.php')?>
</div>
</p>
<div class="row">
<div class="col-sm-12"><b>
<p>
<?php include_once('sales_count.php')?>
<script>
var table = $("#salestableid");
var refresh = function() {
table.load("salestable.php", function() {
setTimeout(refresh, 1);
});
};
refresh();
var table1 = $("#sales_countid");
var refresh1 = function() {
table.load("sales_count.php", function() {
setTimeout(refresh1, 1);
});
};
refresh1();
</script>
<button type="button" class="btn btn-danger btn-md" data-toggle='modal' data-target='#cancel'>Cancel</a>
<button type="button" class="btn btn-warning btn-md" data-toggle='modal' data-target='#hold'>Hold</button>
<button type="button" class="btn btn-success btn-md pull-right" data-toggle='modal' data-target='#payment' >Payment</button>
<br /><br />
<center>Thank you and have a Nice day!</center>
</p>
</div>
</div>
</div>
<!-- End of Page SALES.HTML -->
<!-- SALESTABLE.PHP -->
<?php require_once("../includes/db_connection.php"); ?>
<table class="table table-hover" id='salestableid'>
<thead>
<tr class= "default" style="background-color: #e18728">
<th><font color = "white">Action</font></th>
<th width="200"><font color = "white">Product</th>
<th><font color = "white">Quantity</th>
<th width="75"><font color = "white">Price</th>
<th width ="90"><font color = "white">Total</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$user_query = mysqli_query($connection, "SELECT saleslog.itemid, saleslog.qty, saleslog.date, saleslog.total, saleslog.id, items.`name`, items.description, items.retailprice FROM saleslog LEFT JOIN items ON items.id = saleslog.itemid")or die(mysqli_error());
while($row = mysqli_fetch_array($user_query)){
$id = $row['id'];
?>
<tr>
<td></td>
<td><?php echo $row['name']; ?></td>
<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<?php echo $row['qty']; ?></td>
<td>&nbsp<?php echo number_format($row['retailprice'],2); ?></td>
<?php
$total = $row['qty'] * $row['retailprice'];
?>
<td><?php echo number_format($total,2); ?></td>
</tr>
<?php } ?>
</tr>
</tbody>
</table>
<!-- End of Page SALESTABLE.PHP -->
<!--SALES_COUNT.PHP -->
<?php require_once("../includes/db_connection.php"); ?>
<?php
$count_client= mysqli_query($connection, "SELECT * FROM saleslog");
$count = mysqli_num_rows($count_client);
?>
<?php
$totalsales= mysqli_query($connection, "SELECT SUM(total) AS totalpritems FROM saleslog");
while($totalperitems = mysqli_fetch_array($totalsales)){
$total_sales = $totalperitems['totalpritems'];
}
?>
<table class='table' style="border-style: dotted" id="sales_countid">
<tr class='' style="border-style: dotted">
<td width="50" >Total Items: </td>
<td width="50"align="right"><?php echo $count; ?></td>
<td width="50" >Total: </td>
<td width="50"align="right" >P <?php echo number_format($total_sales,2); ?></td>
</tr>
<tr class='' style="border-style: dotted">
<td width="50">Discount: </td>
<td width="50"align="right">0.00 </td>
<td width="50">Tax: </td>
<td width="50"align="right">0.00 </td>
</tr>
<tr style="border-style: dotted">
<td width>Total Payable: </td>
<td align="right">P <?php echo number_format($total_sales,2); ?></td>
<td></td>
<td></td>
</tr>
</table>
<!--End of Page SALES_COUNT.PHP -->
In your refresh code for table1, you refresh table instead.
table1.load(...) instead of table.load(...) in the code below.
var table1 = $("#sales_countid");
var refresh1 = function() {
table.load("sales_count.php", function() {
setTimeout(refresh1, 1);
});
};

Categories