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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.