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

Asked

Viewed 174 times

4

It is possible to use PHP within a field data-title?

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 field data-... does not work with php inside. None of the attributes works.

This html code was working perfectly, after putting with php stopped working.

  • 3

    Do not work in what sense? Do not show the values? Because the situation you proposed I see no problem in being executed. Be sure to use php > 5.4, because <?= (simplifying echo) I believe is only from this version up, in case I’m not mistaken.

  • Complementing the information on the short tag: http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php

  • just add a echo before the variable that "holds" the value you want to print

  • I tested locally here and in Ideone with an array of any https://ideone.com/nINakE ... worked correctly. Make sure the dice aren’t empty?

  • Yes I am sure they are not empty, I am using the same variables elsewhere and it works, only those attributes are not working

  • 1

    @Márcioandré You are with the configuration short_open_tag enabled for use <?= ?

  • yes I always use like this

Show 2 more comments

4 answers

2

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


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

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

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

data-title="<?= htmlentities($row['portfolio_titulo']) ?>"
  • The problem is in all attributes.

  • Perform a var_dump in the variable $row somewhere on the page where you can see the result and check if the variable is correct and even contains the values you are trying to use. var_dump($row);

  • Yes variables have content, I’m using them elsewhere and they work.

  • Even if it works elsewhere, it is interesting to look for the error anywhere possible in the script current. When you least expect it, something small that you don’t value can represent the problem you’re looking for.

  • Fully agree @Oesli, but it is not empty variables, I inspected element and they are full

1

1 - Check if your document is .php

2 - You don’t need to square 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 - Do a test before selecting the 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">

...
  • What difference does that code make to the one I have?

  • Remove the clasps.

  • And do with this organization.

  • 1

    I tried it, it didn’t work

  • Now do it like this: $portfolio_tipo = "valore teste"; E TESTE! SE VIER ALGO, SEU SELECT PRESTA!

  • does not work the same, only stopped working after I put php, when it was only in html worked 100%

  • My dear, it was meant to be working. Are you sure your document is .php ??

  • Yes I’m sure it’s a file .php. Obviously it was supposed to be working and because I’m not solving the problem I came here to try to get help.

  • "Since PHP 5.4.0, <?= is Always available." Your php is higher than 5.4?

  • Yes, I have no problem using it. @Lollipop just like you said it works, but when I use it with query it stops working

  • The question is.... Does your SELECT bring any results?

Show 6 more comments

-3

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

Try to do this: I did 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 doesn’t work, check if your query at the bank brings any results....

  • 4

    <?= is already a echo. Using both together will generate an error.

  • @Andréribeiro did a test here and worked normally, I edited the answer to get more dynamic...

-4

In PHP, when you don’t give a ECHO in the variables, it does not print the value of the same on the page transforming it into HTML. your error is in this. try to put ECHO that solves.

  • 1

    <?= is already a echo.

  • 1

    don’t know <?=as a ECHO, both because this is not even identified as a PHP tag opening. the correct is for you to do the <?php echo $row['portfolio_tipo']; ?>.

  • 1

    @LeandroLUK http://php.net/manual/en/function.echo.php

  • 1

    on the same link you sent me @Rafaelwithoeft has the information Note: Because this is a language construct and not a function, it cannot be called using variable functions I think that’s why it’s not working, so it’s better to do it with echo.

  • @Leandroluk just wanted to demonstrate the use of the short tag, in the case <?php echo $variavel ?> would work the same way <?= $variavel ?>, I hope you didn’t misinterpret my comment, didn’t want to judge right or wrong, just demonstrate both sides... the concept of variable functions, can be seen here: http://php.net/manual/en/functions.variable-functions.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.