How to display 2 variables in a single cell Mysql - php

I would like to display two picture variables in a single cell
http://i.imgur.com/9ZuRhnD.gif
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='140'>" . "<img src=\"{$row['Limgt']}\">" ."</td>";
echo "<td align='center' width='40'>" . "<img src=\"{$row['Limg']}\">" ."</td>";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
What am I missing

You're putting them each in their own cell () - try this:
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n<td>";
}
echo "<img src=\"{$row['Limgt']}\">";
echo "<img src=\"{$row['Limg']}\">";
$i++;
if ($i == $items) {
echo "</td></tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>

you are making them in two TDs so you have to make them in one TD like that
echo "<td align='center' width='140'>
<img src=\"{$row['Limgt']}\">
<img src=\"{$row['Limg']}\">
</td>";

Related

How can i echo image size in PHP?

I try to put image size in image src tag, but it still does not work. The image can be displayed fine.
<?php
$files = glob("uploads/*.*");
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 4; $row++) {
echo "<tr> \n";
for ($col=1; $col<=4; $col++) {
$f=$f+1;
$getfile = $files[$f];
echo "<td>";
echo "<img src=$getfile > ";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
You can use getimagesize() function http://php.net/manual/en/function.getimagesize.php to fetch image size
<?php
$files = glob("uploads/*.*");
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 4; $row++) {
echo "<tr> \n";
for ($col=1; $col<=4; $col++) {
$f=$f+1;
$getfile = $files[$f];
$size = getimagesize($getfile);
echo "<td>";
echo "<img src=$getfile $size[3]> ";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
PHP Function filesize will hep you here
echo filesize($filename) . ' bytes';
http://php.net/manual/en/function.filesize.php

Highlight first row in php while loop

I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}

Split a table into three rows

I've got a PHP page, some of which is something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
How can I change this so the table goes C1 C1 C1?
So if I have the following set of values - 1 2 3 4 5 6 7 8 9 - the table returns the following
C1 C1 C1
1 2 3
4 5 6
7 8 9
How can I do this?
something like this:
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<td>" . $row['C1'] . "</td>";
if ($cpt%3==0)
echo "</tr><tr>";
$cpt++;
}
echo "</tr>";
will create a new row after every 3 result
Define in $N how many cols you want.
$N=3;
$col=0;
while($row = mysql_fetch_array($raw_results))
{
$col++;
if ($col==1) echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
if ($col==$N)
{
$col=0;
echo "</tr>";
}
}
if ($col>0) //if there is an open <tr>...
echo "</tr>";
if(mysql_num_rows($raw_results) > 0) {
echo "<table border='1'>
<tr><th>C1</th><th>C1</th><th>C1</th></tr>";
$col = 0;
while($row = mysql_fetch_array($raw_results)) {
if ($col == 0) { echo "<tr>"; }
echo "<td>" . $row['C1'] . "</td>";
if ($col == 2) { echo "</tr>"; }
$col = ($col + 1) % 3
}
echo "</table>";
}
This is also Working (little bit short one)
$a=1;
while($row = mysql_fetch_array($raw_results))
{
echo "<td>".$row['c1']."</td>".(($a%3==0)?'</tr><tr>':'');
$a++;
}
Change it to
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th><th>C1</th><th>C1</th>";
$x = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($x % 3 == 0) {
echo "</tr><tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$x++;
}
echo "</table>";
I assume you have just 3 columns and count of your result is submultiple of 3
pay attention to </tr><tr>
No test so it might be some syntax error but you can do something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
<th>C1</th>
<th>C1</th>
</tr><tr>";
$i = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($i == 2) {
$i = 0;
echo "</tr>";
echo "<tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$i++;
}
if ($i > 0) {
echo "</tr>";
}
echo "</table>";

alignment problems in php while loop

Here i don't have any coding related problem. I'm trying to fetch the details from multiple database tables. fetch results working fine. But alignment is my problem here.
You can see a image.. In that image voucher status column is working fine. But, another two column ( room status & meal status values ) is always on the last row. How to align those (room and meal status) values properly (like voucher status column)? i hope you can understand my problem...
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while($row = $stmt->fetch())
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **// FIRST TD**
echo "<td>$ref</td>"; **// SECOND TD**
echo "<td>$gname</td>"; **// THIRD TD**
if( $vstatus != 'empty' ) **// FOURTH TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
}
while($r_row = $r_stmt->fetch())
{
$rstatus = $r_row['ActiveStatus'];
// same like above if else statement **// FIFTH TD**
}
while($m_row = $m_stmt->fetch())
{
$mstatus = $m_row['ActiveStatus'];
// same like above if else statement **// SIXTH TD**
}
echo "</tr>";
}
echo "</table>";
?>
You have 6 th elements in your first row, but I only see 3 td elements and no tr closing tags on subsequent rows. This is likely your issue.
I would suspect that a cursory inspection of your source output would have made this pretty clear.
try this
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while($row = $stmt->fetch())
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **// FIRST TD**
echo "<td>$ref</td>"; **// SECOND TD**
echo "<td>$gname</td>"; **// THIRD TD**
if( $vstatus != 'empty' ) **// FOURTH TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id_meal= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
while($r_row = $r_stmt->fetch())
{
$rstatus = $r_row['ActiveStatus'];
// same like above if else statement **// FIFTH TD**
}
while($m_row = $m_stmt->fetch())
{
$mstatus = $m_row['ActiveStatus'];
// same like above if else statement **// SIXTH TD**
}
echo "</tr>";
}
}
echo "</table>";
?>
I got it working using this while condition while(($row = $stmt->fetch()) && ($r_row = $r_stmt->fetch()) && ($m_row = $m_stmt->fetch())). Before i've used three separate while loops. Now, i've some other ideas. i tried it, now its working fine and alignment also perfect. Before i didn't post my if else statement code. So, everyone thought i've used just 3 td's. Sorry for confused everyone... I've posted my correct coding below..
<?php
echo "<table width=1090 border=1 style=\"border: #ddd;\" align=center cellspacing=4 cellpadding=10>";
echo "<tr class=thvoucher>";
echo "<th width=30>Voucher Number</th>";
echo "<th width=80>Reference Number</th>";
echo "<th width=200>Guest Name</th>";
echo "<th width=150>Voucher Status</th>";
echo "<th width=150>Room Status</th>";
echo "<th width=150>Meal Status</th>";
echo "</tr>";
for($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
while(($row = $stmt->fetch()) && ($r_row = $r_stmt->fetch()) && ($m_row = $m_stmt->fetch()))
{
$voucherid = $row['VoucherID'];
$ref = $row['VoucherReference'];
$gname = $row['GuestName'];
$vstatus = $row['ActiveStatus'];
echo "<tr class=voucherstyle" . $cls . ">";
echo "<td>$voucherid</td>"; **1st TD**
echo "<td>$ref</td>"; **2nd TD**
echo "<td>$gname</td>"; **3rd TD**
if( $vstatus != 'empty' ) **4th TD**
{
if( $vstatus == 'Y' )
{
echo "<td class=green >Active</td>";
}
else if( $vstatus == 'N' )
{
echo "<td class=red >Inactive</td>";
}
}
else
{
echo "<td><form method=post onsubmit=\"return mstatusvalidate(this)\"; action='voucherstatus_update.php?id= $voucherid '><select name=mealstatus><option value=empty></option><option value=active>Active</option><option value=inactive>Inactive</option></select><input type=submit class=update_status title=\"Update $gname voucher status\" value=Update></form></td>";
}
$rstatus = $r_row['ActiveStatus'];
// if else statement **5th TD**
$mstatus = $m_row['ActiveStatus'];
// if else statement **6th TD**
echo "</tr>";
}
}
echo "</table>";
?>

Pagination not working when replacing Table by Dynamic DataTable in JQuery

I am generating a Dynamic table based on user's search result with those searched columns only.
The table is generated in AJAX page and coming back as response in another page.The pagination alone is not working.
please help.
My Code:
<?php
require_once('../Connections/finalkms.php');
$data = json_decode(stripslashes($_POST['data']), true);
$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%' class='table table-striped table-bordered table-hover' align='center' id='sample_2'>
<thead><tr id='vstr'>";
if (mysql_num_rows($getcolumns)>0)
{
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
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></tr>";
}
echo "</tbody></table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Here's My Java script code:
<script>
$("#sample_2").dataTable();
</script>
This is the solution:
<?php
require_once('../Connections/finalkms.php');
$data = json_decode(stripslashes($_POST['data']), true);
$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%' class='table table-striped table-bordered table-hover' align='center' id='sample_2'>
<thead><tr id='vstr'>";
if (mysql_num_rows($getcolumns)>0)
{
$i = 0;
while ($i < mysql_num_fields($getcolumns))
{
echo "<th align='center'>". mysql_field_name($getcolumns, $i) . "</th>";
$i++;
}
echo "</tr></thead>";
while ($rows = mysql_fetch_array($getcolumns,MYSQL_ASSOC))
{
echo "<tr>"; //removed body tag here
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}
?>
Here's My Java script code:
<script>
$("#sample_2").dataTable();
</script>

Categories