Is it possible to take the name of a variable and use it as a string?

Asked

Viewed 378 times

1

Is it possible to take the name of a variable and use it as a string ? Situation: I have a form full of inputs when sending them step by step with their name and value, but I also need to update them in the BD using their correct id to insert, in the input value is used to pass urls. then I was thinking of giving the id on the receiving name then Array( "id_1"=>"http://exemplo.com"). But I need the ID to insert in the correct line of the comic.

          <?php
        for ($i = 0; $i < $nomeDasTabelas['value']; $i ++) {
            $aux = $i + 1;
            echo '<div class="object">
                <form method="GET" action="index.php">
                <div class="grade">
                    <input type="hidden" name="serie" value="'.$aboutSerie[0].'">';

            for ($is = 0; $is < $valorDeLinks[$i]; $is ++) {
                $auxs = $is + 1;
                $tabelaDeIds = $idDosLinks[$i];
                $idDoLink = $tabelaDeIds[$is];
                $btndel = '&btn=btndel&tab=' . $nomeDasTabelas[$i] . '&id=' . $idDoLink;

                echo ($is % 4 == 0) ? '<br>' : "";
                echo '<label for="txtboxtp' . $aux . 'ep' . $auxs . '">Ep' . $auxs . '|</label>
                    <input id="txtboxtp' . $aux . 'ep' . $auxs . '" type="text" class="textbox" name="' . $is . '" value="http://">
                    <a href="index.php?serie=' . $aboutSerie[0] . $btndel . '"><button class="btndel" type="button" name="btndel" title="Excluir"><img src="../imgsys/icon_del.png" alt="Del" title="Excluir"></button></a>
                    <input type="hidden" name="ep'.$is.'" value="'.$idDoLink.'">';    
            }
  • Explain your problem better, show a code of what’s doing it. If I understood, I’d practically have to do nothing but for safety.

  • Good thinking here and I decided to create inputs of type Hidden and pass in its values the input id that contains the URL.

  • This doesn’t show what you want to do in the database, but more and more I think you’re wanting to do something unsafe.

  • I posted the code. So I loaded the BD Urls for input so they can be edited. Well in the BD they each have an id. Dai want to update the values in their corresponding id.

  • Do you have an exact amount of records returned from the BD or do you take everyone you have and create as many inputs as necessary? A solution using jQuery (also) would serve?

  • create the required amount of inputs.

Show 1 more comment

1 answer

1

I didn’t quite understand the question, so I’m going by the title:

You can take the name of a variable and use it as a string?

Yes, it is possible, the easiest method to do this is by exploring the syntax of PHP, where when using a string with a quote it does not recognize the variable as a variable but as a string normal, so I kind of use a 'gambiarra'. An example:

<?php
    $minhaVariavel = "Stack Overflow";
    $nomeVariavel = '$minhaVariavel';
    $nomeVariavel = mb_substr($nomeVariavel, 1); // tira o $ do nome da variavel

    echo "Nome: ".$nomeVariavel."<br/>";
    echo "Valor: ".$minhaVariavel;

The exit will be:

Nome: minhaVariavel
Valor: Stack Overflow

I repeat again that I did not understand the question very well, I voted to close because it is not clear, so I went only by the title. If that is not the purpose of the question then I withdraw my answer and ask you to edit the question to understand what the doubt.

  • Or he can access the value like this: echo "Valor: ${minhaVariavel}";

  • Yes, when using two quotes (") the variable recognition is automatic, the {} keys are mostly used for variables that are arrays, in which case I prefer to use concatenation. It may be so echo "Valor: ${minhaVariavel}";, thus echo "Valor: {$minhaVariavel}";, or so echo "Valor: $minhaVariavel"; (the latter when the variable is not an array)

Browser other questions tagged

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