title: How can I conditionally render a container that's been outputted by PHP with Vue.js?
body: <p>I've encountered a problem in which I get data from a database with PHP I then store the data into a PHP array like so :</p>
  <pre><code>$query = mysqli_query($db, &quot;SELECT id, name, description, experience, focus, fileformat, file, edit FROM trainers;&quot;);
  $i = 0;
  while($query_run = mysqli_fetch_assoc($query)){
      $id = $query_run['id'];
      $name = $query_run['name'];
      $description = $query_run['description'];
      $experience = $query_run['experience'];
      $focus = $query_run['focus'];
      $format = $query_run['fileformat'];
      $file = $query_run['file'];
      $edit = $query_run['edit'];
      $edit = explode(' ', $edit);
      $trainers_meta[$i] = array('Id' =&gt; $id, 'Name' =&gt; $name, 'Description' =&gt; $description, 'Experience' =&gt; $experience, 'Focus' =&gt; $focus, 'Fileformat' =&gt; $format, 'File' =&gt; $file, 'Edit' =&gt; $edit);
      $i++;
  }
  </code></pre>
  <p>Afterwards I display the data from the array in the HTML code using a PHP foreach loop like so :</p>
  <pre><code>&lt;div class=&quot;container-lg px-lg-4 px-0 pt-3 border-right border-left border-bottom border-radius-bottom mb-5&quot;&gt;
      &lt;?php foreach ($services_meta as $service):?&gt;
          &lt;div class=&quot;container-lg bg-dark-white py-3 px-3 my-3 border-radius&quot;&gt;
              &lt;form action=&quot;admin.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
                  &lt;div class=&quot;row no-gutters&quot;&gt;
                      &lt;div class=&quot;col-lg-5 col-12 d-flex align-items-center&quot;&gt;
                          &lt;i class=&quot;fas fa-code-branch mr-3 ml-2 text-brand-primary&quot; style=&quot;font-size: 1.9em&quot;&gt;&lt;/i&gt;
                          &lt;h5 class=&quot;font-bolder text-truncate&quot;&gt;&lt;?php echo $service['Name']?&gt;&lt;/h5&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-lg-5 col-8 d-flex align-items-center pt-lg-0 pt-md-3 pt-sm-2 pt-1&quot;&gt;
                          &lt;h6 class=&quot;font-normal text-brand-primary pr-1&quot;&gt;Úprava :&lt;/h6&gt;
                          &lt;h6 class=&quot;text-truncate&quot;&gt;&lt;?php echo $service['Edit'][0]?&gt; &lt;?php echo $service['Edit'][1]?&gt;&lt;/h6&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-lg-2 col-4 d-flex align-items-center justify-content-end&quot;&gt;
                          &lt;button class=&quot;btn btn-brand-secondary font-bold px-22 py-2 mx-1&quot; type=&quot;submit&quot; name=&quot;services-submit-&lt;?php echo $service['Id']?&gt;&quot;&gt;&lt;i class=&quot;fas fa-edit&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                          &lt;button class=&quot;btn btn-brand-primary font-bold px-3 py-2 mx-1&quot; type=&quot;submit&quot; name=&quot;services-delete-&lt;?php echo $service['Id']?&gt;&quot;&gt;&lt;i class=&quot;fas fa-trash-alt&quot;&gt;&lt;/i&gt;&lt;/button&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-12&quot;&gt;&lt;hr&gt;&lt;/div&gt;
                      &lt;div class=&quot;col-lg-4 col-md-6 col-12 justify-content-md-start justify-content-center pb-md-2 d-flex align-items-center pb-lg-3&quot;&gt;
                          &lt;div class=&quot;d-flex flex-column pt-3&quot;&gt;
                              &lt;h6 class=&quot;font-normal text-brand-primary pb-1&quot;&gt;Popis :&lt;/h6&gt;
                              &lt;textarea type=&quot;text&quot; name=&quot;services-description-&lt;?php echo $service['Id']?&gt;&quot; class=&quot;border-radius&quot; cols=&quot;18&quot; rows=&quot;4&quot;&gt;&lt;?php echo $service['Description']?&gt;&lt;/textarea&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-lg-4 col-md-6 col-12 justify-content-md-start justify-content-center pb-md-2 d-flex pb-lg-3&quot;&gt;
                          &lt;div class=&quot;d-flex flex-column pt-3&quot;&gt;
                              &lt;h6 class=&quot;font-normal text-brand-primary pb-1&quot;&gt;Místo :&lt;/h6&gt;
                              &lt;input type=&quot;text&quot; name=&quot;services-place-&lt;?php echo $service['Id']?&gt;&quot; class=&quot;border-radius&quot; value=&quot;&lt;?php echo $service['Place']?&gt;&quot;&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-lg-4 col-md-6 col-12 justify-content-md-start justify-content-center pb-md-2 d-flex pb-lg-3&quot;&gt;
                          &lt;div class=&quot;d-flex flex-column pt-3&quot;&gt;
                              &lt;h6 class=&quot;font-normal text-brand-primary pb-1&quot;&gt;Termíny :&lt;/h6&gt;
                              &lt;input type=&quot;text&quot; name=&quot;services-terms-&lt;?php echo $service['Id']?&gt;&quot; class=&quot;border-radius&quot; value=&quot;&lt;?php echo implode($service['Terms'])?&gt;&quot;&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;col-lg-4 col-md-6 col-12 justify-content-md-start justify-content-center pb-md-2 d-flex align-items-center pb-lg-3&quot;&gt;
                          &lt;div class=&quot;d-flex flex-column py-3&quot;&gt;
                              &lt;h6 class=&quot;font-normal text-brand-primary pb-1&quot;&gt;Název :&lt;/h6&gt;
                              &lt;input type=&quot;text&quot; name=&quot;services-name-&lt;?php echo $service['Id']?&gt;&quot; class=&quot;border-radius&quot; value=&quot;&lt;?php echo $service['Name']?&gt;&quot;&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;?php endforeach;?&gt;
  &lt;/div&gt;
  </code></pre>
  <p>As you may've noticed I use the <code>Id</code> of the data row from the MySQLi database which it originates from to provide a unique <code>name</code> attribute which I can then use to filter through the <code>$_POST</code> of each of the containers so that I can provide functions like deleting from the database on a press of the button which is contained within each container. Here's an example of the delete function for a specific container :</p>
  <pre><code>if($_SERVER['REQUEST_METHOD'] == 'POST'){
      $id = $trainer['Id'];
      foreach ($trainers_meta as $trainer){
          if(isset($_POST[&quot;trainers-delete-$id&quot;])){
              $stmt = $db -&gt; prepare(&quot;DELETE FROM trainers WHERE id = ?;&quot;);
              $stmt -&gt; bind_param(&quot;i&quot;, $id);
              $stmt -&gt; execute();
          }
      }
  }
  </code></pre>
  <p>I simply loop through each of the IDs from the database with <code>$_POST</code> and check which one has been set. This is important because the user also has the ability to add a new container and fill it with new data after they do so I have to check dynamically for the results from the database since they change overtime and I cannot simply hardcore a specific <code>name</code> attribute for use with <code>$_POST</code>.</p>
  <p>My actual problem is that I am looking for a way to implement AJAX like functionality to the webpage so that immediatelly upon clicking delete for instance that specific containers disappears without the requirement for the user to refresh the page. I am trying to use Vue.js as my JavaScript library for the entire project.</p>
  <p>Vue.js provides an option for conditional rendering with the <code>v-if</code> attribute that's provided in the page's HTML but my problem is that I seem to have no way of determining the variable that sets the visibility of the element to false because if I would simply add a <code>v-if</code> and then set the <code>trainersVisibility</code> variable in Vue.js JavaScript to false like :</p>
  <pre><code>let app = Vue.createApp({
      data: function(){
          return{
              vue_init: 'Vue initialization success',
              trainersVisibility: true //This would be set to false on the button click
          }
      }
  })
  app.mount('#app')
  </code></pre>
  <p>all the containers disappear instead of a specific one. I got an idea of simply echoing the <code>Id</code> from the results array with <code>v-if=&quot;trainersVisibility-&lt;?php echo $trainer['Id']?&gt;&quot;</code> directly into the <code>v-if</code> attribute but that should throw an error because the base variable was never defined in the Vue.js JavaScript file at all and it's only occured in the PHP file within it's HTML code. I cannot simply define multiple <code>v-if</code> boolean variables for each container because I have no way of knowing how much containers with adequate data exist on the page since the number of them changes overtime. What can I do to use conditional rendering for each container sepparately for it to allow me to properly setup AJAX like functionality? I am very new to Vue.js so I'm very sorry if this is obvious to You but it's a little messy for me still. Huge thanks for any help with this issue from anybody!</p>
  <p>Edit :</p>
  <p>As per the comments of this question I was lead to the conclusion of converting the database array with the results into a JavaScript array and then do the rest with JavaScript only. That requires the use of the json_encode() function inside of PHP for later use in JavaScript. However <code>json_encode</code> was not completed properly. This was indded like I mentioned caused by the BLOB data that was contained within the array. The problem was that the BLOB data was converted to base64 only once it's been rendered in the HTML code where I used the PHP <code>base64_encode()</code>; function. And so now I convert to base64 directly in the PHP loop which stores the data into the array intially. Once converted to base64 BLOBs appear to be JavaScript compatible. The conversion now runs without errors and I can echo out the JSON array using PHP and it is indeed of a JSON format.</p>
  <p>I've tried defining the variable inside of a  and set it to the JSON converted value as a JavaScript variable both in the included PHP file which scrapes the data from the MySQLi database as well as the index file itself. No matter the file I put the tag in or the position I put the tag in altough the variable exists Vue.js never sees it at all and so I'm unable to do anything with the saved data. I was trying to look for a solution but haven't found any way to parse a pure JavaScript variable to Vue.js. If there is any way this could be done I'm not aware of it. Guess I might have to switch to a completely different JavaScript framework just because of this...</p>