/    Sign up×
Community /Pin to ProfileBookmark

Need help suppressing 0’s

I AM PREPARING A CODE FOR PAYSLIP FOR MY COMPANY
IN OUR DATA SOME COLUMN ARE “O” , I WANT TO DISPLAY AS TABLE
BUT NULL VALUE SHOULD SKIP
HERE THE CODE

[code=html]
<?php
session_start();
if (!isset($_SESSION[‘staff_id’]))
{
die(header(‘Location: ../index.php’));
}

?>
<?php
//database connection
include(‘../admin/connection.php’);
include(‘../sanitise.php’);
$staff_id = sanitise($_GET[‘staff_id’]);

$qry =(“SELECT * FROM sal_08_12 WHERE staff_id = ‘$staff_id'”);
$update = mysql_query($qry) or die(mysql_error());
$row_update = mysql_fetch_assoc($update);
$totalRows_update = mysql_num_rows($update);
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Pay Slip</title>
<script language=”javascript” type=”text/javascript” >
// Disen
document.onselectstart=new Function(‘return false’);
function dMDown(e) {return false;}
function dOClick() {return true;}
document.onmousedown=dMDown;
document.onclick=dOClick

–>
</script>
<style>
body, html
{
margin:0;
padding:0;
font-family:”Lucida Sans Unicode”, “Lucida Grande”, sans-serif;
font-size:11px;
background-color:#fff;
}
#outerwrapper
{
width:800px;
height:auto;
margin:auto;

}

#slip
{
width:747px;
height:auto;
margin:auto;
font-size: x-small;
}

#slip2
{
width:418px;
height:auto;
margin-left:150px;
margin-top:40px;
text-align: right;
}

.text
{
font-size:14px;
font-family:Tahoma, Geneva, sans-serif;
font-weight:bold;
}
</style>
</head>

<body>
<div id=”outerwrapper”>
<div id=”slip”>
<table width=”747″ border=”1″>
<tr>
<td colspan=”4″ align=”center”><span class=”text”>RAMESH FLOWERS</span><br />
<span class=”text”>TUTICORIN</span><br />
H628007<br />
ACCOUNTS SECTION<br />
<br />
<br />
<table width=”200″ border=”0″>
<tr>
<td align=”center”>PAYSLIP</td>
</tr>
</table></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><strong>Generated At</strong></td>
<td><i><?php echo $row_update[‘date_s’]; ?></i></td>
</tr>
<tr>
<td><strong>Staff ID</strong></td>
<td><?php echo $row_update[‘staff_id’]; ?></td>
<td><strong>Name</strong></td>
<td width=”181″><?php echo $row_update[‘NAME’]; ?></td>
</tr>
<tr>
<td width=”87″><strong>payband</strong></td>
<td width=”368″><?php echo $row_update[‘PAYBAND’]; ?></td>
<td width=”83″><strong>designation</strong></td>
<td><?php echo $row_update[‘DCODE1’]; ?></td>
</tr>
<tr>
<td><strong>accno</strong></td>
<td><?php echo $row_update[‘ACCTNO’]; ?></td>
<td><strong>pay</strong></td>
<td>GL<?php echo $row_update[‘PAY’]; ?></td>
</tr>

<tr>

<td colspan=”4″>
<div id=”slip2″>
<table width=”418″ border=”1″>
<?php
if(isset($PAYARREARS))
{
?>
<tr>

<td width=”180″><strong>payarrears</strong></td>
<td width=”222″><?php echo $row_update[‘PAYARREARS’]; ?></td>
</tr>
<?php
}
?>

<tr>
<td><strong>payreco</strong></td>
<td><?php echo $row_update[‘PAYRECO’]; ?></td>
</tr>
<tr>
<td><strong>dpay</strong></td>
<td><?php echo $row_update[‘DPAY’]; ?></td>
</tr>
<tr>
<td><strong>dparrs</strong></td>
<td><?php echo $row_update[‘DPARRS’]; ?></td>
</tr>
<tr>
<td><strong>REC</strong></td>
<td><?php echo $row_update[‘REC’]; ?></td>
</tr>
<tr>
<td><strong>FPAARRS</strong></td>
<td><?php echo $row_update[‘FPAARRS’]; ?></td>
</tr>
<tr>
<td><strong>GROSS</strong></td>
<td><?php echo $row_update[‘GROSS’]; ?></td>
</tr>
<tr>
<td><strong>TOTAL SALARY</strong></td>
<td><strong>N<?php echo $row_update[‘TOTSAL’]; ?></strong></td>
</tr>
</table>
</div></td>
</tr>
</table>
<table width=”747″ border=”1″>
<tr>
<td align=”center”><br />
……………………………………………………<br />
Acountant </td>
<td align=”center”><br />
……………………………………………………<br />
Finance Manager</td>
</tr>
</table>

Click <a href=”print3.php”>here</a> to go back<br />
<a href=”javascript:self.print()”>Print This Page</a> <br />
</div>
</div>
</body>
</html>
[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 20.2014 — An awful lot of code but the question is murky. Can you make more sense?
Copy linkTweet thisAlerts:
@jedaisoulAug 20.2014 — I'm no SQL expert, but I'd suggest amending:

[code=php]
$row_update = mysql_fetch_assoc($update);
[/code]


to:
[code=php]
$row_update = mysql_fetch_assoc($update);
if ($row_update == 0) {$row_update = ' ';}

No guarantee, but give it a try...
[/code]


Oh, and while you are amending the code, get rid of the "mysql_" calls. They are obsolete.
Copy linkTweet thisAlerts:
@ginerjmAug 21.2014 — First you should check if the value of $update is true or not. Then check if the number of rows returned is > 0. THEN use a loop to process all the rows that you get from your query:
[code=php]
while ($row_update = MySQL_fetch_assoc($update)
{
(do stuff)
}
[/code]
Copy linkTweet thisAlerts:
@NogDogAug 21.2014 — You can leverage SQL to handle this, perhaps, e.g.:
<i>
</i>SELECT
CASE
WHEN col_1 &gt; 0 THEN col_1
ELSE NULL
END AS alias_name,
col_2,
col_n
FROM ...
Copy linkTweet thisAlerts:
@srinivashwpauthorAug 21.2014 — As per the database ,certain attributes values are zero for few of the employees.In the html code we have created table structure to display the payslip.We want to skip those values and labels corresponding to that attribute also where values are zeros.We have tried using do while in php but it is not working.

Any help is appreciated.

Thank you for response received so far.
Copy linkTweet thisAlerts:
@deathshadowAug 21.2014 — Your broken Engrish is making it hard to decipher what you are asking -- are you asking for it to not output the entire ROW if a certain field is zero, or are you saying you want an empty cell instead of showing zero in the table?

If the former, just change the query:

SELECT *
FROM sal_08_12
WHERE staff_id = '$staff_id'
AND TOTSAL &gt; 0


changing "TOTSAL" to whatever field you want to have zero's skipped on. You were really unclear WHICH of your dozen plus fields you want skipped.

Meanwhile if you just want it to output blanks instead of 0...

PHP 5.3/earlier
&lt;?php echo $row_update['TOTSAL'] ? $row_update['TOTSAL'] : ''; ?&gt;

PHP 5.4/later
&lt;?= $row_update['TOTSAL'] ?: '' ?&gt;

Now, that said, you've got a laundry list of issues with that code. First up this is 2014, not 2006, stop using the mysql_ functions. See the [url=http://us1.php.net/manual/en/function.mysql-connect.php]giant red warning boxes in the manual[/url] waving you off from their use!

Your markup... why are you using a colspanned TD to do CAPTION's job? Why are you using TD+STRONG to do TH's job? What's with the bandwidth wasting empty row of nothingness? What's with the table for nothing inside the table? Hell, I'm not even certain looking at the output that this is even tabular data; at least not in the manner you are presenting it... but even if it were tables you're not using them right.

Though VERY much what I expect when I see a tranny doctype; which basically means "in transition from 1997 to 1998 coding practices".
Copy linkTweet thisAlerts:
@srinivashwpauthorAug 21.2014 — Actually the data column head and table label are different (table label is elaborate) .our interest is individually skip the

data and table label so that the pay slip may be compact
Copy linkTweet thisAlerts:
@jedaisoulAug 21.2014 — Right, it would have helped if you made that clear in the first place. I took it that you just wanted to suppress zero values (like in a spreadsheet). Ho hum. Anyway, I suspect that you are looking for a "quick and dirty" solution to your immediate problem, rather than a complete rewrite. So, whilst I entirely agree with [b]deathshadow[/b] comments, I'll leave that aside and work within the document structure "as is". On that basis, I think that the output lines that potentially could be omitted if the respective value is blank are:

[code=html]
<?php
if(isset($PAYARREARS))
{

?>
<tr>
<td width="180"><strong>payarrears</strong></td>
<td width="222"><?php echo $row_update['PAYARREARS']; ?></td>
</tr>
<?php
}
?>

<tr>
<td><strong>payreco</strong></td>
<td><?php echo $row_update['PAYRECO']; ?></td>
</tr>
<tr>
<td><strong>dpay</strong></td>
<td><?php echo $row_update['DPAY']; ?></td>
</tr>
<tr>
<td><strong>dparrs</strong></td>
<td><?php echo $row_update['DPARRS']; ?></td>
</tr>
<tr>
<td><strong>REC</strong></td>
<td><?php echo $row_update['REC']; ?></td>
</tr>
<tr>
<td><strong>FPAARRS</strong></td>
<td><?php echo $row_update['FPAARRS']; ?></td>
</tr>
[/code]


The problems with this code are:

  • 1. You are testing for a variable $PAYARREARS whereas the value you are outputting is $row_update['PAYARREARS']. Which suggests at the very least that $PAYARREARS is redundant.


  • 2. There is no test for the remaining values, hence the lines are output even when the value is zero.


  • So it's not the cleanest way of doing it, but the simplest re-write would be:

    [code=html]
    <?php
    if($row_update['PAYARREARS'] > 0)
    {

    ?>
    <tr>
    <td width="180"><strong>payarrears</strong></td>
    <td width="222"><?php echo $row_update['PAYARREARS']; ?></td>
    </tr>
    <?php
    }
    if($row_update['PAYRECO'] > 0)
    {

    ?>
    <tr>
    <td><strong>payreco</strong></td>
    <td><?php echo $row_update['PAYRECO']; ?></td>
    </tr>
    <?php
    }
    if($row_update['DPAY'] > 0)
    {

    ?>
    <tr>
    <td><strong>dpay</strong></td>
    <td><?php echo $row_update['DPAY']; ?></td>
    </tr>
    <?php
    }
    if($row_update['DPARRS'] > 0)
    {

    ?>
    <tr>
    <td><strong>dparrs</strong></td>
    <td><?php echo $row_update['DPARRS']; ?></td>
    </tr>

    <?php
    }
    if($row_update['REC'] > 0)
    {

    ?>
    <tr>
    <td><strong>REC</strong></td>
    <td><?php echo $row_update['REC']; ?></td>
    </tr>

    <?php
    }
    if($row_update['FPAARRS'] > 0)
    {

    ?>
    <tr>
    <td><strong>FPAARRS</strong></td>
    <td><?php echo $row_update['FPAARRS']; ?></td>
    </tr>
    <?php
    }
    ?>
    [/code]

    Now, that assumes that each row is a separate entity requiring it's own value test. If that is not the case, just group the relevant rows under the appropriate test.
    Copy linkTweet thisAlerts:
    @jedaisoulAug 21.2014 — On second thoughts, if the printout of the whole lot depend upon the variable $PAYARREARS, then it is the closing [b]<?php } ?>[/b] that is in the wrong place. It needs to be placed after the last row that is to be omitted if the variable is not set.

    Anyway, I hope that this helps...
    Copy linkTweet thisAlerts:
    @srinivashwpauthorAug 22.2014 — Thank you very much sir,

    Last one month we struggled for this code

    It works nicely

    we will complete our task with your great help

    thank you once agin
    Copy linkTweet thisAlerts:
    @rootAug 22.2014 — This should do the same, assuming what jediasoul wrote worked for you

    [code=php]$fields = array(
    'PAYARREARS',
    'PAYRECO',
    'DPAY',
    'DPARRS',
    'REC',
    'FPAARRS'
    );

    foreach( $fields as $name){
    if($row_update[$name] > 0){

    $display = strtolower( $name );
    echo <<<HEREDOC
    <tr>
    <td width="180"><strong>{$name}</strong></td>
    <td width="222">{$row_update[$name]}</td>
    </tr>
    HEREDOC;

    }
    }
    [/code]


    If you wanted to have nicer headings then you could do something like

    [code=php]$fields = array(
    'PAYARREARS'=>'Pay Arrears',
    'PAYRECO'=>'Pay Record',
    'DPAY'=>'D Pay',
    'DPARRS'=>'D Parrs',
    'REC'=>'Rec',
    'FPAARRS'=>'FPA Arrears'
    );

    foreach( $fields as $fieldname=>$name){
    if($row_update[$fieldname] > 0){

    echo <<<HEREDOC
    <tr>
    <td width="180"><strong>{$name}</strong></td>
    <td width="222">{$row_update[$fieldname]}</td>
    </tr>
    HEREDOC;

    }
    }
    [/code]
    ×

    Success!

    Help @srinivashwp spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...