Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
Hi everyone and Happy Monday to y'all ;)

I'm trying to achieve the following, adding the values from repeatable regions together to then being able output some calculated numbers. But I'm stuck, or maybe it's not possible to use repeatable regions for this, I don't know?

I created one page for holdings, for lets say stocks:

Code: Select all
<cms:repeatable name='stocks' label='Stock Holdings'>
    <cms:editable type='text' name='stock_name' label='Code' col_width='10'/>
    <cms:editable type='text' name='stock_quantity' label='Quantity' col_width='10'/>
    <cms:editable type='text' name='stock_mktprice' label='Market Price' col_width='10'/>
    <cms:editable type='text' name='stock_avgprice' label='Average Price' col_width='10'/>
</cms:repeatable>


Let's say I have two different stocks and enter them in the 2 repeatable rows:
Entry 01:
stock_name: AAPL
stock_quantity: 2
stock_mktprice: 199
stock_avgprice: 180

Entry 02:
stock_name: GOOG
stock_quantity: 1
stock_mktprice: 1218
stock_avgprice: 1000

I now want to display the following calculations:
stock_quantity* stock_mktprice
stock_quantity* stock_avgprice

And then calculate the Gain/loss in % and in value per stock, and also add those together and get a total gain/loss % and Value and display those. How is this possible?

Many thanks :roll:
Mind your editable name - stocks_quantity, not hodl_stocks_quantity. Maybe that was the problem in the calculations? I presume you have tried <cms:mul> tag https://docs.couchcms.com/tags-reference/mul.html?
trendoman wrote: Mind your editable name - stocks_quantity, not hodl_stocks_quantity. Maybe that was the problem in the calculations? I presume you have tried <cms:mul> tag https://docs.couchcms.com/tags-reference/mul.html?

Thanks for the quick reply :D

Sorry, that was just me mixing up names from two different repeatable regions - the names are correct inside the file.

Code: Select all
                            
<cms:repeatable name='stocks' label='Stock Holdings'>
    <cms:editable type='text' name='stocks_code' label='Code' col_width='10'/>
    <cms:editable type='text' name='stocks_quantity' label='Quantity' col_width='10'/>
    <cms:editable type='text' name='stocks_mktprice' label='Market Price' col_width='10'/>
    <cms:editable type='text' name='stocks_avgprice' label='Average Price' col_width='10'/>
</cms:repeatable>

<table class="table mb-0">
    <thead>
    <tr>
        <th>#</th>
        <th>Stock</th>
        <th>Quantity</th>
        <th>Avg. Price</th>
        <th>Mkt. Price</th>
        <th>Current Value</th>
    </tr>
    </thead>
    <tbody>
      <cms:pages masterpage='hodl.php'>
      <cms:show_repeatable 'stocks' >
        <tr>
          <th scope="row"><cms:show k_count /></th>
          <td><cms:show stocks_code /></td>
          <td><cms:show stocks_quantity /></td>
          <td><cms:show stocks_avgprice /></td>
          <td><cms:show stocks_mktprice /></td>
          <td><cms:mul stocks_quantity stocks_mktprice /></td>
        </tr>
      </cms:show_repeatable>
      </cms:pages>
    </tbody>
</table>


What I want to be able to do, is to calculate from outside the loop - for example counting the % gain/loss for each row of repeated data and add all them together to get a total.. And I want to input that In a graph :D

Sorry if unclear
If you need a helping hand to code the whole thing quickly and also have at your disposal a working HTML with a couple of sample rows (at least) to make the graph and all the needed formula for calculations, you can PM me a zip with full HTML/CSS/JS which I can review and code it for a small fee (like $50).

Otherwise, you have demonstrated a clear ability to code the loop, output the Repeatable and calculate the product. You can repeat the same loop in the place where you need calculations. I do not perceive your troubles with the code, honestly.

As a side note, we often use <cms:capture> and global variables to show something outside the loop if needed. It is essential to make everything in one loop for performance reasons if number of rows is expected to be big. Certain calculations can be wrapped within a new custom tag or a function (cms:func).
Thanks for the offer Trendoman, I would deffo get you to review the code if it was for a project.
I'm doing a savings dashboard for myself and I want to learn how to use couch in a more advanced way. I been using couch for a couple of years now and I love it! There is a lot of functions in couch that I haven't yet used, but would like to learn. SO I see this as my couch training, reading the documentation and try out the code for myself :)

I'm trying to wrap more complex calculations and save them as variables, but I have not yet succeeded, but I'm not giving up :lol:

PS. I have some ideas for websites that is more complex and that I think couch would be awesome for, maybe I can consult you for some coding and help for that :)
Thank you for posting back. I am now absolutely sure, you better check cms:func viewtopic.php?f=8&t=11368&start=10#p30174 - it will be a great help in your experiments.

And welcome with any ideas you'd want to discuss and review. :)
@quickie

hope it can help you

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='calculate_repetable'>
    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' /> 
    <cms:repeatable name='business' >   
<cms:editable name='product' label='Product Name' type='text' required='1'/>
<cms:editable name='product_squ' label='Product Code' type='text' required='0'/>
<cms:editable name='qty' label='Product quantity' type='text' required='1'/>
<cms:editable name='price' label='Product price' type='text' required='1'/>
<cms:editable name='discount' label='Product discount' type='text' required='1'/>
<cms:editable name='payment_amount' label='Payment Amount' type='text' required='1'/>   
<cms:editable name='order_date' label='Product order date' type='text' required='1'/>
<cms:editable name='current_statuse' label='Product current_status' type='text' required='1'/>
<cms:editable
  name="paymeny_mode"
  label="Payment Mode"
  label="Select one from these propert type"
  opt_values='Cash | Cheque | Others  | lobby'
  type='dropdown'
  required=''
/>
<cms:editable name='ectra_note' label='product_note' type='text' required='0'/>
</cms:repeatable>
</cms:template>
<!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>
   <!-- Strat settings Global value for calculate -->
      <cms:set quanity_total='0' scope='global' /><!-- vARIABLE SET total Qanty Value -->
      <cms:set price_total='0' scope='global' /><!-- "   total price Value -->
      <cms:set discount_total='0' scope='global' /><!-- " total discount Value -->
      <cms:set payment_total='0' scope='global' /><!-- " total payment value -->      
      <cms:set groupnetamnt='0' scope='global' /><!-- " total amount to pay value -->
      <cms:set pending_total='0' scope='global' /><!--" total PENDING value -->
   <!-- Wnd settings Global value for calculate  kindly folow repetable are for more-->
   <h2>Couchcms Repeatable Regions (Row) Table Calculation</h2>
   <cms:pages masterpage=calculate_repetable>
   <h2><cms:show first_name /> <cms:show last_name/></h2>
                     <table id="table">
                          <tr>
                           <th>Product Name</th>
                           <th>Product SQU</th>
                           <th>Quantity </th>
                           <th>Price </th>
                           <th>Discount</th>
                           <th>Ammount to pay</th>
                           <th>Payment</th>                           
                           <th>Pending Amount</th>
                           <th>Order Date</th>
                           <th>Status</th>                              
                          </tr>
                          <cms:show_repeatable 'business'>
                         
                        <cms:set quanity_total = "<cms:add quanity_total qty />" scope='global' /><!-- total Sum quantity Calculation -->
                        <cms:set price_total = "<cms:add price_total price />" scope='global' /><!-- total Row Sum Calculation -->
                        <cms:set discount_total = "<cms:add discount_total discount />" scope='global' />   <!-- total Sum discount Calculation -->                     
                        <cms:set payment_total = "<cms:add payment_total payment_amount />" scope='global' /> <!-- total Sum payment Calculation -->      
                        
                        <cms:set netv = "<cms:mul qty price />" scope='global' /> <!-- Calculate  amount to pay each for row -->
                        <cms:set netvt= "<cms:sub netv discount />" scope='global'/><!-- Final Calculate Amount to pay   for each  row -->
                     
                        <cms:set penv= "<cms:sub netvt payment_amount/>" scope='global'/> <!-- Calculate pending amount for row -->
                        <cms:set groupnetamnt= "<cms:add groupnetamnt netvt/>" scope='global'/><!-- total sum   amount to pay -->
                        <cms:set pending_total= "<cms:add pending_total penv/>" scope='global'/><!-- total Sum pending Calculation -->
                     
                          <tr>
                           <td><cms:show product/></td> <!-- Show Product Name-->
                           <td><cms:show product_squ/></td><!-- Show Product SQU-->
                           <td style="text-align:right"> <cms:show qty/></td> <!-- Show Product Quantity-->
                           <td  style="text-align:right"><cms:show price/></td> <!-- Show Product price-->
                           <td  style="text-align:right"><cms:show discount/></td>   <!-- Show Discount Name-->                     
                           <td  style="text-align:right"><cms:show netvt/></td> <!-- Show Commision Amount-->
                           <td  style="text-align:right"><cms:show payment_amount/></td>      <!-- Show payment amount-->                     
                           <td  style="text-align:right"><cms:show penv/></td> <!-- Show pending amount-->
                           <td class="date"><cms:show order_date/></td> <!-- Show order Date Name-->
                           <td class="status"><cms:show current_statuse/></td>   <!-- Show Product Status-->                           
                          </tr> 
                        
                           </cms:show_repeatable>
                            </tbody>
                           <tfoot>
                         <tr class="bg-info">
                           <td>Total-</td>
                           <td></td>
                           <td  style="text-align:right"><cms:show quanity_total/></td><!-- total quantity Value -->
                           <td  style="text-align:right"><cms:show price_total/></td><!-- total Price Amount -->
                           <td  style="text-align:right"><cms:show discount_total/></td> <!-- total Discount Amount -->
                           <td  style="text-align:right"><cms:show groupnetamnt/></td><!-- total Comission Calculate Value -->
                           <td  style="text-align:right"><cms:show payment_total/></td> <!-- total payment Amount-->
                           <td  style="text-align:right"><cms:show pending_total/></td><!-- total pending Amount-->
                           <td>-</td>
                           <td>-</td>                              
                          </tr>                    </table>
                           </cms:pages>

</body>
</html>
<?php COUCH::invoke(); ?>
Subhamoy wrote: @quickie

hope it can help you

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template title='calculate_repetable'>
    <cms:editable name='first_name' required='1' type='text' />
    <cms:editable name='last_name' required='1' type='text' /> 
    <cms:repeatable name='business' >   
<cms:editable name='product' label='Product Name' type='text' required='1'/>
<cms:editable name='product_squ' label='Product Code' type='text' required='0'/>
<cms:editable name='qty' label='Product quantity' type='text' required='1'/>
<cms:editable name='price' label='Product price' type='text' required='1'/>
<cms:editable name='discount' label='Product discount' type='text' required='1'/>
<cms:editable name='payment_amount' label='Payment Amount' type='text' required='1'/>   
<cms:editable name='order_date' label='Product order date' type='text' required='1'/>
<cms:editable name='current_statuse' label='Product current_status' type='text' required='1'/>
<cms:editable
  name="paymeny_mode"
  label="Payment Mode"
  label="Select one from these propert type"
  opt_values='Cash | Cheque | Others  | lobby'
  type='dropdown'
  required=''
/>
<cms:editable name='ectra_note' label='product_note' type='text' required='0'/>
</cms:repeatable>
</cms:template>
<!DOCTYPE html>
<html>
<head>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
</style>
</head>
<body>
   <!-- Strat settings Global value for calculate -->
      <cms:set quanity_total='0' scope='global' /><!-- vARIABLE SET total Qanty Value -->
      <cms:set price_total='0' scope='global' /><!-- "   total price Value -->
      <cms:set discount_total='0' scope='global' /><!-- " total discount Value -->
      <cms:set payment_total='0' scope='global' /><!-- " total payment value -->      
      <cms:set groupnetamnt='0' scope='global' /><!-- " total amount to pay value -->
      <cms:set pending_total='0' scope='global' /><!--" total PENDING value -->
   <!-- Wnd settings Global value for calculate  kindly folow repetable are for more-->
   <h2>Couchcms Repeatable Regions (Row) Table Calculation</h2>
   <cms:pages masterpage=calculate_repetable>
   <h2><cms:show first_name /> <cms:show last_name/></h2>
                     <table id="table">
                          <tr>
                           <th>Product Name</th>
                           <th>Product SQU</th>
                           <th>Quantity </th>
                           <th>Price </th>
                           <th>Discount</th>
                           <th>Ammount to pay</th>
                           <th>Payment</th>                           
                           <th>Pending Amount</th>
                           <th>Order Date</th>
                           <th>Status</th>                              
                          </tr>
                          <cms:show_repeatable 'business'>
                         
                        <cms:set quanity_total = "<cms:add quanity_total qty />" scope='global' /><!-- total Sum quantity Calculation -->
                        <cms:set price_total = "<cms:add price_total price />" scope='global' /><!-- total Row Sum Calculation -->
                        <cms:set discount_total = "<cms:add discount_total discount />" scope='global' />   <!-- total Sum discount Calculation -->                     
                        <cms:set payment_total = "<cms:add payment_total payment_amount />" scope='global' /> <!-- total Sum payment Calculation -->      
                        
                        <cms:set netv = "<cms:mul qty price />" scope='global' /> <!-- Calculate  amount to pay each for row -->
                        <cms:set netvt= "<cms:sub netv discount />" scope='global'/><!-- Final Calculate Amount to pay   for each  row -->
                     
                        <cms:set penv= "<cms:sub netvt payment_amount/>" scope='global'/> <!-- Calculate pending amount for row -->
                        <cms:set groupnetamnt= "<cms:add groupnetamnt netvt/>" scope='global'/><!-- total sum   amount to pay -->
                        <cms:set pending_total= "<cms:add pending_total penv/>" scope='global'/><!-- total Sum pending Calculation -->
                     
                          <tr>
                           <td><cms:show product/></td> <!-- Show Product Name-->
                           <td><cms:show product_squ/></td><!-- Show Product SQU-->
                           <td style="text-align:right"> <cms:show qty/></td> <!-- Show Product Quantity-->
                           <td  style="text-align:right"><cms:show price/></td> <!-- Show Product price-->
                           <td  style="text-align:right"><cms:show discount/></td>   <!-- Show Discount Name-->                     
                           <td  style="text-align:right"><cms:show netvt/></td> <!-- Show Commision Amount-->
                           <td  style="text-align:right"><cms:show payment_amount/></td>      <!-- Show payment amount-->                     
                           <td  style="text-align:right"><cms:show penv/></td> <!-- Show pending amount-->
                           <td class="date"><cms:show order_date/></td> <!-- Show order Date Name-->
                           <td class="status"><cms:show current_statuse/></td>   <!-- Show Product Status-->                           
                          </tr> 
                        
                           </cms:show_repeatable>
                            </tbody>
                           <tfoot>
                         <tr class="bg-info">
                           <td>Total-</td>
                           <td></td>
                           <td  style="text-align:right"><cms:show quanity_total/></td><!-- total quantity Value -->
                           <td  style="text-align:right"><cms:show price_total/></td><!-- total Price Amount -->
                           <td  style="text-align:right"><cms:show discount_total/></td> <!-- total Discount Amount -->
                           <td  style="text-align:right"><cms:show groupnetamnt/></td><!-- total Comission Calculate Value -->
                           <td  style="text-align:right"><cms:show payment_total/></td> <!-- total payment Amount-->
                           <td  style="text-align:right"><cms:show pending_total/></td><!-- total pending Amount-->
                           <td>-</td>
                           <td>-</td>                              
                          </tr>                    </table>
                           </cms:pages>

</body>
</html>
<?php COUCH::invoke(); ?>


Awesome mate, thank you for that!
8 posts Page 1 of 1