0

$_SESSION['lines'] is populated from a textbox that a user pastes into. Before I do anything with the data, I want to run it through a filter to see if any of the values they've input are NOT in the database.

foreach ($_SESSION['lines'] as $q) {
$multiSupplierQuery      = "SELECT distinct supplier from allparts where quotePartNumber = '$q'";
$multiSupplierResult    = mysqli_query($con, $multiSupplierQuery);

while ($row = mysqli_fetch_array($multiSupplierResult)) {
  $multiSupplier = $row['supplier'];
}
 if (!multiSupplier) {
  unset($_SESSION['lines'[]);
 }
}

The crux of this revolves around this statement:

 if (!multiSupplier) {
  unset($_SESSION['lines'[]);
}

What I'm trying to say is: each time we cycle through this, if multiSupplier doesn't exist, remove this particular element from the array.

my unset syntax is wrong though... how do I make it right?

1
  • 1
    Looks like you have an extra [ at unset($_SESSION['lines'[]). Commented Feb 3, 2015 at 21:52

2 Answers 2

2

You probably want to add a $ to your if statement. Change

if (!multiSupplier) {

to

if (!$multiSupplier) {  

Also, to unset from the $_SESSION you need a key. Try changing

foreach ($_SESSION['lines'] as $q) {

to

foreach ($_SESSION['lines'] as $key=>$q) {

and then

unset($_SESSION['lines'][$key]);
Sign up to request clarification or add additional context in comments.

2 Comments

Yup - I saw that typo too. Thanks! What's the difference between the answer that @Dagon provided above, unset($_SESSION['lines'][$q]); where no key is used and your answer? After some testing, your solution worked while the one above did not.
When using the foreach, $q contains a copy of the item in the $_SESSION array; modifying $q doesn't change $_SESSION at all. Also, $q is not an array key, so it doesn't make sense to try to unset $_SESSION['lines']'s $q key. When you add the $key, it tells you where the actual data is stored in the $_SESSION['lines'] variable so php knows which item to unset.
1

It's not your unset usage, you just have an unnecessary opening square bracket.

unset($_SESSION['lines'[]);

should be

unset($_SESSION['lines']);

And you'll be good to go.

3 Comments

no - what this does is it completely unsets the entire 'lines' array that I've made, (e.g. - removes all data from it.) I just want to remove THAT specific element from the entire array if the query comes back with no results.... So - if $_SESSION['lines'] is comprised of 6 elements, and element 3 isn't in the database, I want to remove item 3 from the $_SESSION['lines'] array, but keep items, 1, 2, 4, 5, and 6.
then you want unset($_SESSION['lines'][$q]);
perfect - that's exactly what I needed... the [$q] part. Awesome, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.