0

so I am trying to remove array in if statement inside foreach loop...

<?php
        foreach ($politics as $tag => $key):?>


        <?php    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                    $ip = $_SERVER['HTTP_CLIENT_IP'];
                    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                    } else {
                        $ip = $_SERVER['REMOTE_ADDR'];
                      }
                      $vienodas = "SELECT * FROM pol WHERE (politikas = '$Veryga' AND ip = '$ip')";
                      $vienodasres = mysqli_query($conn, $vienodas);
                     if ( mysqli_num_rows($vienodasres) > 0){
                         //IT DOESN'T WORK 
                         unset($politics[$tag]);
                         //IT DOESN'T WORK 
                     }
                      ?>
 <div class="mySlides">
<?php $Veryga= $key['vardas'];
 $Veryga2 = str_replace(' ', '', $Veryga);
 ?>
  <div <?php echo 'id="'.$Veryga2.'"'; ?> class="politikai">

<..Not important code..>



  </div>
  <?php
  endforeach; ?>

It doesn't want to work, I was trying to do it, the if statement just checks if there are lines which is equal to user IP and the slide which they are

4
  • In the first line why is there a colon? Commented Mar 12, 2018 at 12:56
  • Because there is html code inside, and it works and at the and i close foreach loop with endforeach, this is instead of curly brackets :) Commented Mar 12, 2018 at 13:00
  • 1
    @Jessedegans It's a alternative syntax for control structures. I prefer to use curly braces. Commented Mar 12, 2018 at 13:03
  • @AndrewSchultz thanks :) Commented Mar 12, 2018 at 13:29

2 Answers 2

1

You have the value and key elements around the wrong way it should be defined like this.

foreach ($politics as $key => $tag):
    unset($politics[$key]);
Sign up to request clarification or add additional context in comments.

7 Comments

though this shouldn't matter for the code execution
@ikdekker it won't matter but it's incorrectly named and is misleading.
So where should I put if statement?
@JonasTamoševičius The if statement is correctly nested but what does mysqli_query return?
@AndrewSchultz It returns name and ip, so if there are lines in table where politics (names) is equal to the key and IP from which they have they already voted for that person :)
|
0

you're not accessing that $politics array-element later, so it is unset, but the control flow continues inside the element. if you'd like to prevent the output use a continue; statement at that point.

5 Comments

I believe so too. But then i'd expect him to want to skip only when there is no results from the database. Still probably the right answer
Where to put that statement exactly? :)
where you unset the element, if i understood your intent correctly ;) and if you plan to make this code available somewhere in the net - please have a look into prepared statements, as you have a wide open sql injection vector sitting there!
check the amount of returned lines & if the condition is met (and the statements are executed). -> debug output or attached debugger and step-through.
From PHP's help: The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved. so please check again if the value is really what you expect it to be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.