make php code shorter with conditional statements?

A few years ago I did a course of PHP but most of the knowledge has faded. So I've got a question. In a Joomla form (RSForms by RSJoomla) I am using repetitive code like this:

$locatie01 = $_POST['form']['locatie_01'];
$locatie02 = $_POST['form']['locatie_02'];
$locatie03 = $_POST['form']['locatie_03'];
$locatie04 = $_POST['form']['locatie_04'];
$locatie05 = $_POST['form']['locatie_05'];
$locatie06 = $_POST['form']['locatie_06'];

and this goes on till it reaches 100. (the location (spelled 'locatie' in Dutch) is posted to the database. A bit further down, we're doing something similar but then with more elaborate code, so I'll just list 3:

 $db->setQuery("
     INSERT INTO #__instellingen
     (type,instelling,locatie,contactpersoon,telefoon,email,ivvu,vgu,token)
     VALUES
     ('".$type."','".$instelling."','".$locatie01."','".$contactpersoon."','".$telefoon."','".$email."','".$ivvu."','".$vgu."','".$token."')
 ");
 $db->query();

 if(!empty($locatie02)){
 $db->setQuery("
     INSERT INTO #__instellingen
     (type,instelling,locatie,contactpersoon,telefoon,email,ivvu,vgu,token)
     VALUES
     ('".$type."','".$instelling."','".$locatie02."','".$contactpersoon."','".$telefoon."','".$email."','".$ivvu."','".$vgu."','".$token."')
 ");
 $db->query();
 }

 if(!empty($locatie03)){
 $db->setQuery("
     INSERT INTO #__instellingen
     (type,instelling,locatie,contactpersoon,telefoon,email,ivvu,vgu,token)
     VALUES
     ('".$type."','".$instelling."','".$locatie03."','".$contactpersoon."','".$telefoon."','".$email."','".$ivvu."','".$vgu."','".$token."')
 ");
 $db->query();
 }

And this also continues to 100

What we are doing here, is: the first line is always filled (required field) and from then on: if 2 is not empty, send the data to the database, same for 3 etc.

Now I can tell this is not the shortest way of coding, and I vaguely remember we were able to write shorter code; was it with coditional coding? if .... a++. I'm sorry, I can't remember.... Can anyone refresh my memory?

Thanx,

Thom

Upvotes: 0

Views: 52

Answers (2)

JesusValera
JesusValera

Reputation: 647

Your code is incomplete, I don't know where the $token var comes from so I am going to suppose it comes from the form. Also, I guess for the DB you are using the PDO object but it is almost the same for mysqli driver.

You can do something like that:

foreach($_POST['form'] as $key) {
    $sql = 'INSERT INTO #__instellingen (type, instelling, locatie, 
    contactpersoon, telefoon, email, ivvu, vgu, token) VALUES (?,?,?,?,?,?,?,?,?)';
    $stmt = $pdo->prepare($sql);
    $stmt->execute([$key['type'], $key['instelling'], $key['locatie'], $key['contactpersoon'], $key['telefoon'], 
    $key['email'], $key['ivvu'], $key['vgu'], $key['token']);
}

The '?' mean bound parameter. It is important you use a prepared statement in order to add a layer for avoiding SQL injections in your code.

Upvotes: 1

EternalHour
EternalHour

Reputation: 8621

Since the query is the same for every element, you could do something like this. Also no need to check if it exists since you will only be handling the values in the array. You need to use prepared statements and sanitize here, do not insert raw form data into the database.

foreach($_POST['form'] as $key => $locatie) {
  $db->setQuery("
     INSERT INTO #__instellingen
     (type,instelling,locatie,contactpersoon,telefoon,email,ivvu,vgu,token)
     VALUES ('".$type."','".$instelling."','".$locatie."','".$contactpersoon."','".$telefoon."','".$email."','".$ivvu."','".$vgu."','".$token."')
 ");
}

Upvotes: 0

Related Questions