patternphpMinor
Submitting client reviews
Viewed 0 times
submittingclientreviews
Problem
I generally encounter a lot of
if/else. I would like to get some suggestions on my code:if( $blnLogged && isset( $_POST['postReview'] ) )
{
if( $_SESSION['REVIEW']['SUBMITTED'] != '' )
{
$arrSharedData['blnRetSave'] = $_SESSION['REVIEW']['SUBMITTED'];
$blnShowForm = false;
$blnReviewPosted = true;
}else{
if( $arrData['captcha'] != $_SESSION['REVIEW']['CODE'] )
{
$strErrMsg = 'Invalid Captcha Code...';
}else{
if( empty( $arrData['review_title'] ) || empty( $arrData[ 'review_description' ] ) || empty( $arrData[ 'overall_review' ] ) || empty( $arrData[ 'cleanliness' ] ) || empty( $arrData[ 'facilities' ] ) || empty( $arrData[ 'location' ] ) || empty( $arrData[ 'quality_of_service' ] ) || empty( $arrData[ 'room' ] ) || empty( $arrData[ 'value_of_money' ]) )
{
$strErrMsg = 'Required field missing...';
}else{
//do we need any processing...
$arrData['business_id'] = $bID;
$arrData['client_id'] = $_SESSION['site_user'];
$arrData['website_id'] = WEBSITE_ID;
$arrData['review_date'] = date('Y-m-d');
//If field Transformation required do it...
$objTripReview = SM_Loader::loadClass('Class_Reviews');
$blnRetSave = $objTripReview->saveReview( $arrData );
$_SESSION['REVIEW']['SUBMITTED'] = $blnRetSave;
$arrSharedData['blnRetSave'] = $_SESSION['REVIEW']['SUBMITTED'];
$blnShowForm = false;
$blnReviewPosted = true;
}
}
}
}
if( $blnShowForm === true )
{
$_SESSION['REVIEW']['CODE'] = rand(9999,99999);
$_SESSION['Review']['SUBMITTED'] = '';
}Solution
One simple way to improve this is to package it in a function, and return (or throw an exception) after each
Even better might be to move all the validation code to a separate function that throws an exception, and then handle it in the function that called it with the wrong parameters.
$strErrMsg = ... line. This will flatten the function, and allow you to put the main functionality at the "top level" of the function.Even better might be to move all the validation code to a separate function that throws an exception, and then handle it in the function that called it with the wrong parameters.
Context
StackExchange Code Review Q#985, answer score: 6
Revisions (0)
No revisions yet.