Stupid question: Cant follow the FAQ: How can I build a product list?

wlwan 29 Jun, 2015
Ref: FAQ: How can I build a product list?
https://www.chronoengine.com/faqs/70-cfv5/5251-how-can-i-build-a-product-list.html

I know it is a stupid question, but I just can't get the result finally and need to seek help here.... 😟

Code in "Design" as "Custom"
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Name</th>
      <th>Price</th>
      <th>Quantity</th>
    </tr>
  </thead>
  <tbody>
<?php
foreach ( $form->data['products'] as $p ) {
  echo "<tr>
  <td>{$p['id']}</td>
  <td>{$p['name']}</td>
  <td>${$p['price']}</td>
  <td><input type='text' name='quantity[{$p['id']}]' id='quantity_{$p['id']} size='4' /></td>
  </tr>";
}
?>
  </tbody>
</table>


"Custom Code" @ "Setup" "OnSubmit"
<?php
$sales = array();
foreach ( $form->data['quantity'] as $p => $q ) {
  if ( (int) $q > 0 ) {
    $sales[$p] = $q;
  }
}
// get the array of product ids
$products = array_keys($sales);
// quote the list of product ids from use in a db query
$product_list = "'".implode("', '", $products)."'";

$db = JFactory::getDBO();
$query = "SELECT `cf_id`, `p_id`, `p_name`, `p_description`, `p_price`, `p_status`
  FROM `#__product_data`
  WHERE `p_id` IN ({$product_list}) ;
";
$db->setQuery($query);
$product_info = $db->loadAssocList('p_id');
$total = 0;
foreach ( $products as $p ) {
  // get the quantity
  $product_info[$p]['p_quantity'] = $sales[$p];
  // calculate the price for this quantity
  $product_info[$p]['p_value'] = $sales[$p] * $product_info[$p]['p_price'];
  // update the total
  $total += $product_info[$p]['p_value'];
}
// add the results to the $form->data array
$form->data['sales'] = $product_info;
$form->data['total'] = $total;
?>


And finally I got the form like this...
https://dummysite.demojoomla.com/index.php/form

Any ideas? Thanks a lot.~

Wing
wlwan 29 Jun, 2015
And... the debugger @ db read, seems like it can load the db, but cant show it up....

Array
(
    [option] => com_chronoforms5
    [chronoform] => productListing
    [products] => Array
        (
            [0] => Array
                (
                    [cf_id] => 1
                    [p_id] => 1
                    [p_name] => productname1
                    [p_description] => productdesc1
                    [p_price] => 100
                    [p_status] => available
                )

        )

)
GreyHead 30 Jun, 2015
Hi Wing,

What is the actual problem here? You've posted a lot of stuff but I have no idea where it is going wrong?

It looks as though you have the DB Read action in the form On Load event but the Code to output the results of the read in the On Submit event? That doesn't look as if it will work.

Bob

PS The link to your test form says 'Database connection failed miserably:'
wlwan 02 Jul, 2015
Dear Bob,
I need to do something similar to the FAQ, which need to load data from form, edit field and update to database, so I follow the FAQ but somehow it fails.

At this moment, my problem is fail to display any record from database.

So I need a custom code @designer instead?
I did try to add custom code @designer as below (get this from previous post in the forum)
<p>There are <?php echo $form->data['products']['p_name']; ?> product name</p>

but somehow it fails and said "syntax error, unexpected '[', expecting ',' or ';' "

Would you please give me some sample code which can show multi row for me to test?
Tons of thanks.

P.S. the database connect is fixed... sorry that i have done some testing and forgot to recover it after posting this post....

Wing
GreyHead 02 Jul, 2015
Hi Wing,

I'm sorry, I still don't understand what you are trying to do :-(

Your code is failing because there is no $form->data['products']['p_name'] variable. There is a $form->data['products'][0]['p_name'] variable in your previous post - but this presumably shows a product name, not the number or products. You could get the number of products with count($form->data['products']) if that is what you need.

Bob
wlwan 03 Jul, 2015
Dear Bob,
Sorry for my bad english. Actually I would like to learn from this FAQ step by step and see if it works on my machine.
So the main goal is: select data from database, show the product listing, have input field to update the number of products.
I did everything I got from it but it fails, seems like it has some missing parts and need to fill in myself.😟

I'm not a typical programmer / expert in joomla or chronoforms so I have difficulties to make it works by my own.
the $form->data['products']['p_name'] variable i used in my previous post is just some test script to see if i can show something from the database. however, nothing display even if i change it to $form->data['products'][0]['p_name'] nor count($form->data['products']), and fyi, i hv tried to put this code @ designer and setup>dbread>on success but both fail.

I think the 1st step i need is to show the product listing first. Would you teach me how to get it work based on the setting I posted previously?
This topic is locked and no more replies can be posted.