Is it possible to use PHP in a data-title field?

4

Is it possible to use PHP within a data-title field?

I have the following code:

<div id="grid" class="m-row shuffle--container shuffle--fluid">
      <?
      $result = $connection -> query("SELECT * FROM portfolio") or die($connection -> error);
      while($row = $result -> fetch_array(MYSQLI_ASSOC)){
      ?>
          <div class="col-md-3 col-sm-4 col-xs-12 m-col-md-3 picture-item" 
               data-groups='["<?=$row['portfolio_tipo']?>"]' 
               data-date-created="<?=$row['portfolio_data']?>" 
               data-title="<?=$row['portfolio_titulo']?>">
          <div class="picture-item__inner">

The problem is that every data-... field does not work with php inside. None of the attributes works.

This code in html was working perfectly, after putting it with php crashed.

    
asked by anonymous 11.03.2015 / 14:05

4 answers

2

Php works anywhere in the file. Since you reported the problem by quoting only the data-title attribute, I believe the other attributes ( data-date-created , ...) are working. If so, I believe your $row['portfolio_titulo'] variable is empty or does not exist.

Edit: You may have some problems with quotation marks. For example, if you try to put a Meu título com "aspas" string in the data-title attribute, it would look like this:

data-title="Meu título com "aspas""

To work around this problem, you can use the htmlspecialchars or htmlentities :

data-title="<?= htmlentities($row['portfolio_titulo']) ?>"
    
11.03.2015 / 15:03
1

1 - Verify that your document is .php

2 - You do not need brackets data-groups='["<?=$row['portfolio_tipo']?>"]'

...

    <?php
          $portfolio_tipo = $row['portfolio_tipo'];
          $portfolio_data = $row['portfolio_data'];
          $portfolio_titulo = $row['portfolio_titulo'];

    ?>


          <div class="col-md-3 col-sm-4 col-xs-12 m-col-md-3 picture-item" 
                   data-groups="<?=$portfolio_tipo?>" 
                   data-date-created="<?=$portfolio_data?>" 
                   data-title="<?=$portfolio_titulo?>">
          <div class="picture-item__inner">

...

3 - Test before selecting values with the query:

...

    <?php
          $portfolio_tipo = 'teste tipo';
          $portfolio_data = 'teste data';
          $portfolio_titulo = 'teste titulo';

    ?>


          <div class="col-md-3 col-sm-4 col-xs-12 m-col-md-3 picture-item" 
                   data-groups="<?=$portfolio_tipo?>" 
                   data-date-created="<?=$portfolio_data?>" 
                   data-title="<?=$portfolio_titulo?>">
          <div class="picture-item__inner">

...
    
11.03.2015 / 15:38
-3

Yes, it is possible! I even believe that this was the initial intention to create these attributes in HTML5 . Create a more pleasant environment for the user without having to make several queries to the database ... in the first query already popular all necessary data of the server in the fields data and leave the rest in charge of the user's browser using front-end e taking the responsibility of the server to create all the dynamics of the page.

Try this: I ran a test here and it worked normally the attributes data

<div id="grid" class="m-row shuffle--container shuffle--fluid">
  <?
$result = $connection -> query("SELECT * FROM portfolio") or die($connection -> error);
while($row = $result -> fetch_array(MYSQLI_ASSOC)){

    echo  '<div class="col-md-3 col-sm-4 col-xs-12 m-col-md-3 picture-item" 
        data-groups="['.$row['portfolio_tipo'].']" 
        data-date-created='.$row['portfolio_data'].' 
        data-title='.$row['portfolio_titulo'].'>'; ?>

      <div class="picture-item__inner">

If it still does not work, check if your query in the database has any results ....

    
11.03.2015 / 14:09
-4

In PHP, when you do not give ECHO to the variables, it does not print the value of it on the page, turning it to HTML. your mistake lies in that. try putting the ECHO that resolves.

    
11.03.2015 / 14:15