Jump to content

Creating A Logic In Best Way..


thara

Recommended Posts

can I know... there is a best way to do this logic than I have tried so far???

 

This is my code...

 

//------------ Check code search bar form submission ---------------//
if ( isset( $_POST['code_search_submitted'])) {
if ( isset( $_POST['searchCode']) && !empty( $_POST['searchText'])) {
if (is_numeric( $_POST['searchText']) && (strlen($_POST['searchText']) == 4)) {

$searchCode = $_POST['searchCode'];
if ( $searchCode == 1 ) {
$_SESSION['searchCode'] = 'TutorCode';
} elseif ( $searchCode == 2 ) {
$_SESSION['searchCode'] = 'InstituteCode';
}

$_SESSION['searchText'] = $_POST['searchText'];

} else {
$searchingError= 'Ennter four digits for code!';
echo '<script type="text/javascript">';
echo "alert('" . $searchingError . "')";
echo "</script>";
}


$url = BASE_URL . 'search/searching.php'; // Define the URL:
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.

} else {
$searchingError= 'Enter Tutor or Institute Code!';
echo '<script type="text/javascript">';
echo "alert('" . $searchingError . "')";
echo "</script>";
}
} //---- Check code search bar form submission ----//

 

any comments are greatly appreciated...

 

thank you...

Link to comment
https://forums.phpfreaks.com/topic/270191-creating-a-logic-in-best-way/
Share on other sites

I'd check for errors first, and if found: Show error message, and return to the calling function.

This makes the code a lot easier to read, especially if you have a lot of possible error conditions. Then your code won't have to end up in this list. ;)

 

Also, as a pet peeve of mine: You shouldn't be needing to use output buffers. That tells me that you haven't properly separated the processing from the displaying, which will make your code harder to maintain and a lot more brittle and less flexible than what it should have been.

Do all of the processing first, save all output into variables, and don't send a single thing (except headers) to the browser until all parsing is done.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.