When we assign more than one category to an article we execute a separate esky well insert statements
for each rijkaard in the article category table.
If we're inserting many Ricardos this would mean many trips to the database which could affect performance.
As we learnt earlier on you can insert multiple records at once with one insert statement specifying
multiple sets of values separated by commas.
So let's change this method to insert all the records at once using a single insert statement.
Let's start by removing the values from the Escuela statement.
Then let's create an array for each set of values and add a loop for the cat to give each I.D. array.
Inside this loop will create a string with each set of values just as before with the article.
I'd first followed by a place holder for the category Heidi.
I'll use a question mark instead of a name to place holder as we don't know how many place holders will
need and with a pend this string to the array of values.
Next we need to append each set of values to the ask you l string with each set of values separated
by a comma.
We can use the implode function for this to join the elements of the array together separating them
with a comma.
So let's call this function passing in a string with a comma to separate each set of values and the
array of values strings.
With append this to the existing contents of the esky Well string.
Before we continue let's dump out the ASK U L string.
So we can see what it looks like.
If I select several categories and submit the farm.
This is the esky Well string that we've generated a single insert statement with several sets of values
separated by commas.
Now we can delete this line and prepare the statement as before.
Next we need to bind the category I.D. values to the place holders.
As we using question marks instead of name to place holders when we bind the values instead of the place
holder name.
We can identify the place holder by the one indexed position of the parameter.
That is the first parameter is one.
The second is to and so on.
In this for each loop we can also get the index of the array elements like that.
This index will start from zero.
The first array element is zero.
The second one is 1 and so on.
So when we bind the value we bind it to the index plus one.
Then we can move the execute method to outside of the loop as we only have one statement to execute.
Let's give that a try.
If I select multiple categories and submit the farm.
We can see that multiple records have been added.
But in a single query set the database.