Page 1 of 1

php code to display datewise stock in report

Posted: Wed Apr 05, 2017 3:20 am
by joinnavdev
Hi All,

Appreciate help in resolving the below issues. I had included the code below used but not able to get the exact output.
I am new to PHP so if any coding issues or improper way of presenting please ignore it.

[text]Emloyee Name 2016-04-02 2016-09-14 2017-02-15 2017-03-02 2017-04-02
Naveen 25 0 0 0 100
Srikanth 0 50 10 0 0
Sami 0 0 0 10 60
Total 25 50 10 10 160[/text]
=========================
[text]$distinctDate=array (size=5)
0 =>
array (size=1)
'field_5' => string '2016-04-02' (length=10)
1 =>
array (size=1)
'field_5' => string '2016-09-14' (length=10)
2 =>
array (size=1)
'field_5' => string '2017-02-15' (length=10)
3 =>
array (size=1)
'field_5' => string '2017-03-02' (length=10)
4 =>
array (size=1)
'field_5' => string '2017-04-02' (length=10)[/text]

=========================
[text]$dbData=array (size=6)
0 =>
array (size=3)
'field_1' => string 'naveen' (length=6)
'field_2' => string '25' (length=2)
'field_5' => string '2016-04-02' (length=10)
1 =>
array (size=3)
'field_1' => string 'srikanth' (length=8)
'field_2' => string '50' (length=2)
'field_5' => string '2016-09-14' (length=10)
2 =>
array (size=3)
'field_1' => string 'srikanth' (length=8)
'field_2' => string '10' (length=2)
'field_5' => string '2017-02-15' (length=10)
3 =>
array (size=3)
'field_1' => string 'sami' (length=4)
'field_2' => string '10' (length=2)
'field_5' => string '2017-03-02' (length=10)
4 =>
array (size=3)
'field_1' => string 'naveen' (length=6)
'field_2' => string '100' (length=3)
'field_5' => string '2017-04-02' (length=10)
5 =>
array (size=3)
'field_1' => string 'sami' (length=4)
'field_2' => string '60' (length=2)
'field_5' => string '2017-04-02' (length=10)[/text]
=======================

Code: Select all

<table border="1px solid #666" style="text-align:center;" cellpadding='0' cellspacing='0'>
<thead>
    <tr>
        <th>Employee Name</th>
        <?php foreach($distinctDate as $date):?>
            <?php $dates[] = $date['field_5'];?>
            <th><?php echo $date['field_5'];?></th>
        <?php endforeach;?>
    </tr>
</thead>
<tbody>
    <?php $j = 0;?>
    <?php foreach($dbData as $key => $value):?>
        <?php $names[] = $value['field_1'];?>
        <?php $uniValues = array_count_values($names);?>
        <?php if($uniValues[$value['field_1']] == 1):?>
        <tr>
            <td>
                <?php echo $value['field_1'];?>
            </td>
            <?php $i = 0;?>
            <?php foreach($distinctDate as $date):?>
                <td align="center">				
                    <?php if($names[$i] == $dbData[$j]['field_1']):?>
                        <?php echo $dbData[$j]['field_2'];?>
                    <?php else:?>
                        <?php foreach($dbData as $dat):?>
						<?php if($dat['field_1'] == $names[$j] && $dates[$i] == $dat['field_5']):?>                              
                         <?php echo $dat['field_2'];?>       
							<?php endif;?>
                        <?php endforeach;?>
                    <?php endif;?>
				
                </td>
                <?php ++$i;?>
            <?php endforeach;?>
        </tr>
        <?php endif;?>
        <?php ++$j;?>
    <?php endforeach;?>
</tbody>
</table>


Thanks,
Nick

Re: php code to display datewise stock in report

Posted: Wed Apr 05, 2017 5:28 am
by Celauran
Looks like you'll want to group that last array by user so you can iterate over it more effectively. You've got one row for each user, but you've got Naveen, Srikanth, Srikanth, Sami, then back to Naveen. If each row in your array corresponded to a user, things would be much easier. Especially if you filled in the zeroes where appropriate.

Re: php code to display datewise stock in report

Posted: Wed Apr 05, 2017 11:09 am
by joinnavdev
Hi,

Yes what you are saying is right but when user is entering details then in database separate record is getting created so those data is being pulled from database not much can be done.

Appreciate if you can throw some more light on it or any modification to existing code would be great help.

Will look forward for your help in resolving the issue.

Thanks,
Nick

Re: php code to display datewise stock in report

Posted: Wed Apr 05, 2017 11:19 am
by Celauran
joinnavdev wrote:Hi,

Yes what you are saying is right but when user is entering details then in database separate record is getting created so those data is being pulled from database not much can be done.
Are you saying you are unable to modify the queries being sent to the database?

Re: php code to display datewise stock in report

Posted: Wed Apr 05, 2017 12:13 pm
by Celauran
Assuming you've got no control over the data, you could try something like this:

Code: Select all

<?php

$distinctDate = [
    ['field_5' => '2016-04-02'],
    ['field_5' => '2016-09-14'],
    ['field_5' => '2017-02-15'],
    ['field_5' => '2017-03-02'],
    ['field_5' => '2017-04-02'],
];

$dbData = [
    ['field_1' => 'naveen',   'field_2' => '25',  'field_5' => '2016-04-02'],
    ['field_1' => 'srikanth', 'field_2' => '50',  'field_5' => '2016-09-14'],
    ['field_1' => 'srikanth', 'field_2' => '10',  'field_5' => '2017-02-15'],
    ['field_1' => 'sami',     'field_2' => '10',  'field_5' => '2017-03-02'],
    ['field_1' => 'naveen',   'field_2' => '100', 'field_5' => '2017-04-02'],
    ['field_1' => 'sami',     'field_2' => '60',  'field_5' => '2017-04-02'],
];

$dates = array_column($distinctDate, 'field_5');
$names = array_unique(array_column($dbData, 'field_1'));

$zeroed_dates = array_combine(array_values($dates), array_fill(0, count($dates), 0));
$mapped = array_combine(array_values($names), array_fill(0, count($names), $zeroed_dates));

foreach ($dbData as $data) {
    $mapped[$data['field_1']][$data['field_5']] = $data['field_2'];
}

?>

<table border="1px solid #666" style="text-align:center;" cellpadding='0' cellspacing='0'>
    <thead>
        <tr>
            <th>Employee Name</th>
            <?php foreach ($dates as $date):?>
                <th><?php echo $date; ?></th>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($mapped as $name => $values): ?>
            <tr>
                <td><?= $name; ?></td>
                <?php foreach ($values as $date => $number): ?>
                    <td><?= $number; ?></td>
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    </tbody>
</table>
A much better option, of course, would be to fix your queries.

Re: php code to display datewise stock in report

Posted: Wed Apr 05, 2017 1:54 pm
by joinnavdev
Hi,

It's confusing I tried to understand but being new to PHP couldn't get it but anyhow its working....: :P

I tried to get total of each column but as code is confusing to me so didn't know where to adjust it. Really if you can help with that small part that would be great help!!!!!!!!!!!...

Emloyee Name 2016-04-02 2016-09-14 2017-02-15 2017-03-02 2017-04-02
Naveen 25 0 0 0 100
Srikanth 0 50 10 0 0
Sami 0 0 0 10 60
Total 25 50 10 10 160

Not sure what changes to do...

Thanks,
Nick

Re: php code to display datewise stock in report

Posted: Mon Apr 10, 2017 5:49 am
by Celauran
What have you tried so far and where are you encountering problems?

Re: php code to display datewise stock in report

Posted: Mon Apr 10, 2017 10:52 am
by joinnavdev
Hi,

To be frank enough I have not done anything but I tried to get your logic but couldn't understand it as being new to PHP so could not think of any changes to get Total.

Thanks,
Nick