Checking Array for MT/Null then adding

I have some code which inserts a title and comments from the user. Could be 1 title and comment or up to 10. The problem is if the user leaves the comments section blank, just adding the title the insert statement fails.The title and comments come from a form post.

So for example if the user adds just one title ($collection_item_title_1) and no comment (collection_item_text_1), nothing gets inserted.

The data which comes from a $_POST:

[php]
$form = $_POST;
$category_name = $form[ ‘selectedcat’ ];
$collectionname = $form[ ‘title’ ];
$collection_title_comment = $form[ ‘commenttitle’ ];
$collection_item_title_1 = $form[ ‘number1’ ];
$collection_item_text_1 = $form[ ‘comment1’ ];
$collection_item_title_2 = $form[ ‘number2’ ];
$collection_item_text_2 = $form[ ‘comment2’ ];
$collection_item_title_3 = $form[ ‘number3’ ];
$collection_item_text_3 = $form[ ‘comment3’ ];
$collection_item_title_4 = $form[ ‘number4’ ];
$collection_item_text_4 = $form[ ‘comment4’ ];
$collection_item_title_5 = $form[ ‘number5’ ];
$collection_item_text_5 = $form[ ‘comment5’ ];
$collection_item_title_6 = $form[ ‘number6’ ];
$collection_item_text_6 = $form[ ‘comment6’ ];
$collection_item_title_7 = $form[ ‘number7’ ];
$collection_item_text_7 = $form[ ‘comment7’ ];
$collection_item_title_8 = $form[ ‘number8’ ];
$collection_item_text_8 = $form[ ‘comment8’ ];
$collection_item_title_9 = $form[ ‘number9’ ];
$collection_item_text_9 = $form[ ‘comment9’ ];
$collection_item_title_10 = $form[ ‘number10’ ];
$collection_item_text_10 = $form[ ‘comment10’ ];
[/php]

This places the data into the array.

[php]
//Build Array
$values = array($collection_item_title_1, $collection_item_text_1, $collection_item_title_2, $collection_item_text_2,
$collection_item_title_3, $collection_item_text_3, $collection_item_title_4, $collection_item_text_4,
$collection_item_title_5, $collection_item_text_5, $collection_item_title_6, $collection_item_text_6,
$collection_item_title_7, $collection_item_text_7, $collection_item_title_8, $collection_item_text_8,
$collection_item_title_9, $collection_item_text_9, $collection_item_title_10, $collection_item_text_10);

// get the empty array keys using array_keys
$keys = array_keys($values,"");

// foreach empty key, we unset that entry
foreach ($keys as $k)
unset($values[$k]);
[/php]

In this code the collection_item_text_1 is empty and it is unset using the example above. So the $values array will be without [1] index. But this index is used in the insert statement. In the end the users title and comment dose not get posted because the comment is empty.

[php]
$statement1 = $link->prepare(“INSERT INTO collection_items(collection_list_id, collection_item_number, username_id, collection_item_title, collection_item_text, vote_count, created_date)
VALUES(:collection_list_id, :collection_item_number, :username_id, :collection_item_title, :collection_item_text, :vote_count, now())”);
for ($i = 0, $p = 1, $initial_vote=10; $i < count($values); $i++, $p++,$initial_vote–) {
$statement1->execute(array(
“collection_list_id” => $last_id,
“collection_item_number” => “$p”,
“username_id” => $slusername,//$user_number,
“collection_item_title” => $values[$i],
“collection_item_text” => $values[++$i],
“vote_count” => “$initial_vote”
));
[/php]

So how do I check to see if there is a title and no comment and to insert a empty or null into the comment? I was thinking about looping through the array, if it finds a title without a comment it adds an empty or null, I’m not great at nested loops.

Any help would be appreciated.

Thanks

Well, either remove the comment from the insert if there is none or set up a pre-check to create one if empty.
Loosely like: If (empty($collection_item_text_1)) $collection_item_text_1=“No Comment!”;
Not sure why you would want a user to enter 10 different comments all at once, but, you need to validate the
inputs to the insert. Or, you can set the database to allow null’s. If it can have empty comments, that would
work.

Here is what worked, I just need to figure out how to loop the code rather then single lines. It replaces the empty variable with a new line code so it doesn’t get removed when the code checks for empty key values as long as there is $collection_item_title_1 is set and collection_item_text_1 is empty. The reason the user may have 10 comments is that the user is inputting a list, it maybe 1 item or 10 items. As long as the item has a title it needs to be added even it it doesn’t have a comment to go with it. If its just a comment under an empty title it gets removed as being a mistake on the users part.
Thanks

[php]
if ((isset($collection_item_title_1)) AND (empty($collection_item_text_1))){
$collection_item_text_1 = “\n”;
}

if ((isset($collection_item_title_2)) AND (empty($collection_item_text_2))){
$collection_item_text_2 = “\n”;
}

if ((isset($collection_item_title_3)) AND (empty($collection_item_text_3))){
$collection_item_text_3 = “\n”;
}

if ((isset($collection_item_title_4)) AND (empty($collection_item_text_4))){
$collection_item_text_4 = “\n”;
}

if ((isset($collection_item_title_5)) AND (empty($collection_item_text_5))){
$collection_item_text_5 = “\n”;
}

if ((isset($collection_item_title_6)) AND (empty($collection_item_text_6))){
$collection_item_text_6 = “\n”;
}

if ((isset($collection_item_title_7)) AND (empty($collection_item_text_7))){
$collection_item_text_7 = “\n”;
}

if ((isset($collection_item_title_8)) AND (empty($collection_item_text_8))){
$collection_item_text_8 = “\n”;
}

if ((isset($collection_item_title_9)) AND (empty($collection_item_text_9))){
$collection_item_text_9 = “\n”;
}

if ((isset($collection_item_title_10)) AND (empty($collection_item_text_10))){
$collection_item_text_10 = “\n”;
}
[/php]

I would do some major refactoring

Form, removing all the hard coded comment fields and using a comment prototype which we can duplicate using the add comment button:
[php]

None
Awesome
Great




Add comment
Save [/php]

Javascript that handles the add comment handler, clones the prototype and adds it to the form
[php]// Elements we will be using
var form = document.querySelector(‘form’),
buttons = {
addComment: document.querySelector(’#addComment’)
},
prototypes = {
comment: document.querySelector(‘fieldset[data-prototype-comment]’)
};

// Event listeners
buttons.addComment.addEventListener(‘click’, AddCommentClickHandler);

// Event handlers

// Add comment fields to form
function AddCommentClickHandler() {
// arrays are zero indexed, so we can use the current count to get the next index
var newCommentCount = form.querySelectorAll(‘fieldset[data-comment]’).length;

// Copy the comment prototype to our new comment and get some fields we will be using
var newComment = prototypes.comment.cloneNode(true),
newCommentTitle = newComment.querySelector(‘input[name=“title”]’),
newCommentText = newComment.querySelector(‘textarea[name=“text”]’);

// Remove the prototype attribute and show the new comment fieldset
newComment.removeAttribute(‘data-prototype-comment’);
newComment.setAttribute(‘data-comment’, ‘’);
newComment.style.display = ‘’;

// New names for the fields so they will be submitted as a proper array
newCommentTitle.setAttribute(‘name’, ‘comments[’ + newCommentCount + ‘][title]’);
newCommentText.setAttribute(‘name’, ‘comments[’ + newCommentCount + ‘][text]’);

// Add the new comment to the DOM
buttons.addComment.insertAdjacentHTML(‘beforebegin’, newComment.outerHTML);
}
[/php]

This will give you a data set like this, which means you can just loop over the comments that are actually submitted.
[php]array(4) {
[“selectedcat”]=>
string(1) “1”
[“title”]=>
string(0) “”
[“text”]=>
string(0) “”
[“comments”]=>
array(2) {
[0]=>
array(2) {
[“title”]=>
string(11) “comment one”
[“text”]=>
string(8) “asfaosfh”
}
[1]=>
array(2) {
[“title”]=>
string(11) “comment two”
[“text”]=>
string(10) “aslfhasofj”
}
}
}[/php]

JimL,

Thanks for the reply. I got the form modified using your code and the JavaScript working. It a great idea, works and looks good.

Working on looping and converting

[php]
if ((isset($collection_item_title_1)) AND (empty($collection_item_text_1))){
$collection_item_text_1 = “\n”;
}
[/php]

to find where this is true to replace the empty with “\n”

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service