title: Show specific text from a php loop of database results using jquery
body: <p>I have selected data from my database and I have looped through the results to display them on my page. I have 3 results which are shown. Each result has a button with an id called decrease. When I click this button, an alert with the name of the item containing the alert button should be displayed. I am using jQuery to achieve this.  </p>
  
  <p>My problem is that whenever I click the button, the name of only the first item in the results array is displayed. The buttons in the other two results don't seem to work. What am I doing wrong?</p>
  
  <p>The code that loops through the result and displays the items in the database table:</p>
  
  <pre><code>if(mysqli_num_rows($run) &gt;= 1){
      while($row = mysqli_fetch_assoc($run)) {
          $name = $row['name'];
          $quantity = $row['quantity'];
          $price = $row['price'];
          $image = $row['image'];
          $category = $row['category'];
          $total = $price * $quantity;
          echo "&lt;div class=\"post-container\"&gt;\n";
          echo "&lt;div class=\"post-thumb\"&gt;\n";
          echo "&lt;img src='$image'&gt;\n";
          echo "&lt;/div&gt;\n";
          echo "&lt;div class=\"post-title\"&gt;\n";
          echo "&lt;h4 style=\"font-weight:bold;\"&gt;\n";
          echo "&lt;a href=\"view.php?name=$name&amp;category=$category\" class=\"links\" target=\"_blank\"&gt;$name&lt;/a&gt;\n";
          echo "&lt;span id=\"deletion\"&gt;Delete&lt;/span&gt;\n";
          echo "&lt;/h4&gt;\n";
          echo "&lt;/div&gt;\n";
          echo "&lt;div class=\"post-content\"&gt;\n";
          echo "&lt;ul style=\"list-style-type:none;\"&gt;\n";
          echo "&lt;li&gt;Cost Per Item: &lt;span id=\"cost\"&gt;$price&lt;/span&gt;/=&lt;/li&gt;\n";
          echo "&lt;li&gt;\n";
          echo "Quantity: \n";
          echo "&lt;button type=\"submit\" id=\"decrease\" class=\"glyphicon glyphicon-minus\" title=\"Decrease Quantity\"&gt;&lt;/button&gt;\n";
          echo "\n";
          echo "&lt;span id=\"cost\" class=\"quantity\"&gt;&amp;nbsp$quantity&amp;nbsp&lt;/span&gt;\n";
          echo "\n";
          echo "&lt;button type=\"submit\" id=\"increase\" class=\"glyphicon glyphicon-plus\" title=\"Increase Quantity\"&gt;&lt;/button&gt;\n";
          echo "&lt;/li&gt;\n";
          echo "&lt;li&gt;Total Cost: &lt;span id=\"cost\"&gt;$total&lt;/span&gt;/=&lt;/li&gt;\n";
          echo "&lt;/ul&gt;\n";
          echo "&lt;/div&gt;\n";
          echo "&lt;/div&gt;";
      }
  }
  </code></pre>
  
  <p>And here's the jquery:</p>
  
  <pre><code>$("#decrease").click(function(){
      var name = $(this).parent("li").parent("ul").parent("div.post-content").siblings("div.post-title").find("a").text();
      alert(name);
  });
  </code></pre>