Using repeatable element to update table in PHP plugin

waby

Member
Hello,

I use this script to update a table in a php plugin in a form on "start on form submisson (onBeforeProcess)"

$soid = '{jml_fb_sw_swaps_from___sender_offer_id}';
$snum = '{jml_fb_sw_swaps_from___offered_number}';
$db =&JFactory::getDBO();
$db->setQuery("SELECT stock_number from jml_fb_sw_offers_stocks WHERE parent_id = $soid");
$sstock = $db->loadResult();
$sstockcalc = $sstock - $snum;
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
$db->setQuery($squery);
$db->query();

Of course it works only with the first row of a repeatable group because I think I've to do an array / foreach but I don't know how.

Could you please provide me the right synthax ?



Eloise
 
Hello,

i think you can try this:

....
$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
$db->setQuery($squery);
$db->query();
}
 
Thanks for your help... but unfortunately it doesn't work yet.
If $snum = 4, -4 is updated into the table. Something wrong about the calculation ?
 
you can use print_r($value); or var_dump($value); to get the current values;

Try this:
$sstockcalc = $sstock->stock_number - $snum;
print_r($sstockcalc); exit;

to see what values do you get

same for query, try to add print_r($squery);exit;
after
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
 
I've tried this :

...
$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
print_r($sstockcalc); exit;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
print_r($squery);
exit;
$db->setQuery($squery);
$db->query();
}

and I've the following error :


Notice: Trying to get property of non-object in ../plugins/fabrik_form/php/php.php(282) : eval()'d code on line 11
-4

and nothing has been recorded.

I've tried also this :

$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
var_dump($sstockcalc); exit;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
print_r($squery);
exit;
$db->setQuery($squery);
$db->query();
}

and I've the following error :


Notice: Trying to get property of non-object in ../plugins/fabrik_form/php/php.php(282) : eval()'d code on line 11
int(-4)

and nothing has been recorded.
 
well, the exit; stop the script to run; but despite the above error, you can see that value you get for $sstockcalc is indeed -4

can you post full code here, please?
 
here is the full code :

$soid = '{jml_fb_sw_swaps_from___sender_offer_id}';
$snum = '{jml_fb_sw_swaps_from___offered_number}';
$db =&JFactory::getDBO();
$db->setQuery("SELECT stock_number FROM jml_fb_sw_offers_stocks WHERE parent_id = $soid");
$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
var_dump($sstockcalc); exit;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
print_r($squery);
exit;
$db->setQuery($squery);
$db->query();
}
 
seems to be ok, the message you got is actual a notice, not an error. As i said, the value for for $sstockcalc is -4, so this is the value that will be recorded in database.

Remove the var_dump($sstockcalc); exit; and add here the result, i think it will be something like this:

UPDATE jml_fb_sw_offers_stocks SET stock_number = '-4' WHERE parent_id = '123' // your id
 
hum... I m a little bit lost now...

I ve tried this :
$soid = '{jml_fb_sw_swaps_from___sender_offer_id}';
$snum = '{jml_fb_sw_swaps_from___offered_number}';
$db =&JFactory::getDBO();
$db->setQuery("SELECT stock_number FROM jml_fb_sw_offers_stocks WHERE parent_id = $soid");
$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
print_r($squery);
exit;
$db->setQuery($squery);
$db->query();
}

and no error message but nothing has been recorded into the table.
 
Hum... To clarify :

$sstockcalc should be the result of a calculation :
$sstock (stock_number) less $snum ({jml_fb_sw_swaps_from___offered_number})

So If try this :

$soid = '{jml_fb_sw_swaps_from___sender_offer_id}';
$snum = '{jml_fb_sw_swaps_from___offered_number}';
$db =&JFactory::getDBO();
$sstock = $db->loadObjectList();
$sstockcalc = $sstock->stock_number - $snum;
foreach ($sstock as $sstock1) {
$squery = "UPDATE jml_fb_sw_offers_stocks SET stock_number = $sstockcalc WHERE parent_id = $soid";
$db->setQuery($squery);
$db->query();
}
and if $snum = 4

The value recorded is -4
 
if you write first query like this, what value you get?

$test = ("SELECT stock_number FROM jml_fb_sw_offers_stocks WHERE parent_id = $soid");
print_r($test);exit;
 
If I put this code :

$soid = '{jml_fb_sw_swaps_from___sender_offer_id}';
$test = ("SELECT stock_number FROM jml_fb_sw_offers_stocks WHERE parent_id = $soid");
print_r($test);exit;

I've get this message :

SELECT stock_number FROM jml_fb_sw_offers_stocks WHERE parent_id = 14
 
Can you confirm me that for record with parent_id =14, the stock_number value is not negative or smaller than $snum so you will get a negative result? Like 10 -14 = -4 :)
 
For the record with parent_id 14, the stock_number is 5
So I confirm you that stock_number is not negative and not smaller than $snum.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top