alignment problems in php while loop - php

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>";
?>

Related

How to make an adaptive display of csv data using the csv's header in check boxes and select option as sum, avg and mode USING php?

As you can view in the image here, I am using a form which has check-boxes and a select option. Upon import of my CSV files using PHP, it displays the data correctly with the column headers. It also displays the CHECKED-boxes with its selected option only.
my csv file:
year, Exports of goods(F.O.B), Domestic exports, Re-exports.
1, 5, 10,20
1, 5, 10, 20
1, 5, 10, 20
1, 5, 10, 20
Thank you.
<!DOCTYPE html>
<html>
<head>
<title>Start1</title>
</head>
<body>
<table border="0" cellspacing="1" cellpadding="1" class="sortable" >
<?php #read CSV file
if (($handle = fopen("sample1.csv", "r")) !== false) {
$mycsv = array();
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$mycsv[] = $data;
}
fclose($handle);
#Find the length of the transposed row
$row_length = count($mycsv);
echo "<form method='post' action=''><table><tr>";
echo "<th><input type='checkbox' onClick='toggle(this)' /> Select/Deselect All<br/>
</th>";
echo "<th>Column</th>";
echo "<th>Filter</th>";
echo "</tr>";
#Loop through each row (or each line in the csv) and output all the columns for that row
foreach ($mycsv[0] as $col_num => $col) {
echo "<tr>";
for ($x = 0; $x < 1; $x++) {
echo "<td><input type='checkbox' name='lang[]' value='" . $mycsv[$x][$col_num] . "' ></td>";
echo "<td align='center'>" . $mycsv[$x][$col_num] . "</td>";
echo "<td>
<select name='method[]' >
<option value=''>Choose</option>
<option value='sum'>Sum</option>
<option value='mean'>Mean</option>
<option value='average'>Average</option>
<option value='highest'>Highest Value</option>
<option value='lowest'>Lowest Value</option>
</select>
</td>";
echo "</tr>";
}
}
echo "<tr>";
echo "<td><input type='checkbox' name='lang[]' ></td>";
echo "<td align='center'><input type='text' name='own_column' placeholder='Type your new column here'><br></td>";
echo "<td><input type='text' name='own_column' placeholder='Own formula'><br></td>";
echo "</tr>";
//echo "<tr>";
//echo "<td colspan='3' align='center'><input type='submit' name='Order'/></td>";
//echo "</tr>";
echo "<tr>";
echo "<td colspan='3'><div class='wrapper'><br/><br/><input type='button' class='button' value='Input Button'>
</div></td>";
echo "</tr>
<tr><td><input type='submit' value='submit' name='submit'</td></tr><table></form>";
$post_data = $_POST["lang"];
//$SelectedOption = $_POST['demo'];
for ($i = 0; $i < count($post_data); $i++) {
//echo "Selected : " . $post_data[$i]."</br>" ;
}
$row_length = count($mycsv);
$col_length = count($post_data);
$flag = true;
$sum = array();
foreach ($mycsv[0] as $key => $value) {
foreach ($post_data as $search_term) {
if (strcasecmp(trim($value), trim($search_term)) == 0) {
for ($row = 0; $row < $row_length; $row++) {
array_push($sum, $mycsv[$row][$key]);
if ($row == 0) {
echo "<tr>";
echo "<th colspan=100%>" . $mycsv[0][$key] . "</th>";
echo "</tr>";
} elseif ($row == 1) {
//echo "<td>" . $mycsv[$row][$key] . "</td>";
//echo "array sum =".array_sum($sum)."";
}
//echo " array sum =".array_sum($sum)."] ";
}
$method = $_POST["method"];
$method = $method[$key];
echo "<tr><td>";
if ($method == 'sum') {
echo "Sum of " . $value;
} elseif ($method == 'mean') {
echo "mean of " . $value;
} elseif ($method == 'average') {
echo "average of " . $value;
} elseif ($method == 'highest') {
echo "highest value of " . $value;
} elseif ($method == 'lowest') {
echo "lowest value of " . $value;
} else {
echo " Choose a method ";
echo "</td></tr>";
}
}
}
}
} ?>
</table>
</body>
</html>

A simple php script error result with tables

Hello guys i'm in need of php experts advice.
Here's my problem. A user has 2 inputs the start and the end this are all integers. this will able to identify odd and even. i have solve already odd and even and almost done. main problem ex. start value 1 end 5. 1 is odd it should be displayed on odd in table. but the problem is it is found in even table. initial value is the problem. the rest was good.
here's my code
<?php
$firstnum = $_POST['first_input'];
$secondnum = $_POST['second_input'];
$counter = 0;
echo "<table border='1'>";
if ($firstnum < $secondnum) {
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter <= $secondnum ; $counter++) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
} elseif ($firstnum > $secondnum) {
# code...
//first num is < second num
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter >= $secondnum ; $counter--) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
Your issue is you have invalid html resulting from your if/else blocks.
If your if you have
<tr>
<td><td>
and in your else you have
<td></td>
</tr>
Both of these need full row/cell tags
<tr>
<td></td>
<td></td>
</tr>
So your code should look like
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}

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.
}
}

How to display 2 variables in a single cell Mysql

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>";

PHP Re-submit form

I have a script that is activated when someone submits a form on that same page. The first time the user fills in data and presses submit, it works, the second time it does not respond.
NOTE: There is a second document where the sessions are created (SESSION_START is given).
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$_SESSION['aantalpogingen'] = $_SESSION['aantalpogingen'] + 1;
echo $_SESSION['aantalpogingen'];
echo "<br />";
$_SESSION['poging'][$_SESSION['aantalpogingen']] = substr($_SESSION['hetwoord'] , 0 , 1) . $_POST['PogingLetter2'] . $_POST['PogingLetter3'] . $_POST['PogingLetter4'] . $_POST['PogingLetter5'];
echo "<table width='450' height='75' border='1'>";
foreach($_SESSION['poging'] as $pogingnr=>$gok)
{
if($gok != "")
{
echo "<tr>";
echo "<th WIDTH='66'>";
echo $gok[0] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[1] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[2] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[3] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[4] ;
echo "</th>";
echo "<th>";
echo "Poging " . $pogingnr;
echo "</th>";
echo "</tr>";
}
}
echo " </table";
}
?>
I cannot find any syntax errors or anything, I hope you can help me :)
I found at least one mistake :->
echo " </table";
missing >
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
This condition meant to be for the not intended below too?
echo "<table width='450' height='75' border='1'>";
As it's not closed before the echo.

Categories