How to redirect form?

redirect a form based on a database sum condition.

Overview

The issue was caused by using incorrect PHP database methods to calculate the sum.
Use the correct Joomla database object method to fetch the sum value and then apply the conditional redirect.

Answered
go goliath 19 May, 2015
I tried to redirect my form to a specified url when the sum of the column 'aantal' of all records reaches a certain amount (e.g. 15).

I tried to put this code in a custom code action in the 'On load' action but this doesn't work:

<?php
$db =& JFactory::getDBO();
$query = "
    SELECT *
        FROM `#__my_table` ;
";
$db->setQuery($query);
$aantal = mysql_fetch_array(mysql_query("SELECT SUM(aantal) FROM #__my_table"));
if ( $aantal >= 15 ) {
  // Redirect to my url
  $app =& JFactory::getApplication();
  $app->redirect('myurl');
}
?>
Gr GreyHead 19 May, 2015
Answer
Hi geertmartens,

Please try
<?php
$db = JFactory::getDBO();
$query = "
  SELECT SUM(`aantal`)
    FROM `#__my_table` ;
";
$db->setQuery($query);
$aantal = $db->loadResult();
if ( $aantal >= 15 ) {
  // Redirect to my url
  $app = JFactory::getApplication();
  $app->redirect('myurl');
}
?>

Bob
go goliath 19 May, 2015
Works like a charm!
Thanks Bob!
This topic is locked and no more replies can be posted.