REDIRECTING A PAGE IF A CONDITION ISN’T MET - Mar 31st, 2015


According to Dave Edis at Interactive Tools, if you wanted to redirect a page if there were no records in a table,
here’s how:

Just put this at the top of your page, after the get records call, and before any viewer code is output.



<?PHP
list($your_tableRecords, $your_tableMetaData) = getRecords(array(
'tableName' => your_table',

));

if (!$your_tableRecords) {
header("Location: http://www.example.com/newurl.php");
exit;
}
?>


If you want to change this to when a condition is met, take out the ! Which is the “not” operator in PHP.


And, if you wanted to redirect a page if there was only one record in the table, here's how you'd do that:


<?PHP
list($your_tableRecords, $your_tableMetaData) = getRecords(array(
'tableName' => your_table',

));

if(
$your_tableMetaData['totalRecords'] == 1)
{
header("Location: http://www.example.com/newurl.php");
exit;
}
?>


If you wanted to redirect if there was more than one record, just change the == 1 to > 1

______________________________________________

Expanding on this, if you want to redirect and pass one or more variables to the new page, try a variation of this.

In this case there are 2 variables;
group_code which is the value of a field in the portfolio_title_details table, and
noback which is a way to hide certain code on the new page

Right after the load records calls, insert the following code:


<?php if($portfolio_title_detailsMetaData['totalRecords'] == 1):?>
<?php foreach ($portfolio_title_detailsRecords as $record): ?>
<?php $group_code= $record['group_code'] ?>
<?php endforeach; ?>
<?php header("Location: http://www.your_domain.com/your_new_page.php?group_code=$group_code&no_back=1");
exit;
?>


Then on the new page, define the variables $group_code and $no_back

<?php $var1= $_REQUEST['group_code']; ?>
Use $var1 however you see fit.


and


<?php $var2= $_REQUEST['no_back']; ?>
<?php if (!$var2 == 1 ):?>
Your code to hide...
<?php endif ?>




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service