S
S
Speakermen2020-10-04 09:01:11
MySQL
Speakermen, 2020-10-04 09:01:11

How to rebuild an array with elements?

How can you reduce the code, for example, you need to process data from email password forms.
If each input is checked through if and the session is displayed about a filling error, then a bunch of the same code is obtained.
If you do 'email' => 'required|max:255' in this way, then how can you put it all together later. I would be grateful for the component in packagist/ as a basis for understanding the implementation.

$_POST['email']
$_SESSION['email']
 'email' => 'required|max:255',


function validate()
  {
    $getForm = ['name', 'image', 'platforms', 'chipsets', 'manufacturers'];

    for ($i=0; $i < count($getForm); $i++)
    { 
      if (isset($_GET[$getForm[$i]]) && empty($_GET[$getForm[$i]])) return false; //$_SESSION['error']['required']
      else return true;
    }
  }

  if (validate())
  {
    if (setMotherboard($_GET['name'], $_GET['image'], $_GET['platforms'], $_GET['manufacturers'], $_GET['chipsets']))
    {
      header('Location: /get_motherboard.php');
      exit;
    }
  }

<code lang="php">
class FormValidate
  {
    public static function validate ($params)
    {
      foreach($params as $key => $value) 
      {
              //echo  $key . ' ' . $value . '<br>';

              //echo $params[$key] . '<br>';;

              $params[$key] = explode('|', $params[$key]);
          }



      echo '<pre>';
      var_dump($params);
      echo '</pre>';
    }
  }

  FormValidate::validate([
    'email' => 'required|max:255',
        'username' => 'required',
        'password' => 'required'
  ]);
</code>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
L
lega, 2016-01-10
@lega

You need one composite index for these 2 fields, now you apparently have 2 indexes (one per field) and, accordingly, they do not work at the same time.

A
Aleksey Ratnikov, 2016-01-10
@mahoho

If you have mysql version 5.6+, partitioning by the owner_id field will help you.

A
Alexander, 2016-01-10
@cz_350

Leave one index per owner_id

A
alexalexes, 2020-10-04
@Speakermen

If you see that you are writing the same code repeatedly, then it's time to move the common part of the code into a function or method. And this situation may not necessarily happen in the validation module.
Write basic functions:
a) checking an ordinary string for a minimum and a maximum in length;
b) checking the radio button;
c) checking the checkbox;
d) drop-down list.
All other functions will call something from this basis and check the properties of the field, for example, you check email with function A for the correctness of the length, and then the validity of the regular expression. If the length is not the same, then you will display warnings from function A, but you will not check the regular expression, and if it messes up according to the regular expression, then there will already be a warning that the email is incorrect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question