Variable variable in php

Asked

Viewed 611 times

4

I need to generate a variable with arbitrary name. I have the following code, which does not work:

$vv = mysql_query("SELECT * FROM $tabela ORDER BY rand() LIMIT 3");
$i=1;
while($v = mysql_fetch_array($vv)){
  $vertical.$i=$v['arquivo'];
  $i++;
}

echo'
<img src="img/fotos/'.$vertical1.'"> <br>
<img src="img/fotos/'.$vertical2.'"> <br>
<img src="img/fotos/'.$vertical3.'">
';

I would like the query to return me 3 variables: $vertical1, $vertical2 and $vertical3

Note: In the database the field 'file' has the 'format' included. Ex.: photo.jpg

Note 2: I believe the problem is in the concatenation $vertical.$i...

  • It would be better to move echo inside the WHILE and use the variable that is already passed, what do you think?

  • The code I posted is a short version of the page. In the real case it would not be ideal due to the position of the <img>, which are in various places. Then I would have to do several "query", besides running the risk of listing 2 times the same img.

3 answers

8


With variable variables would be so:

$varname = 'vertical' . $i;
$$varname = $v['arquivo'];

I find it kind of ugly and confusing, I prefer to use an array:

<?php
$vv = mysql_query("SELECT * FROM $tabela ORDER BY rand() LIMIT 3");
$vertical = array();

while($v = mysql_fetch_array($vv)) {
    $vertical[] = $v['arquivo'];
}

echo '
    <img src="img/fotos/'.$vertical[0].'"> <br>
    <img src="img/fotos/'.$vertical[1].'"> <br>
    <img src="img/fotos/'.$vertical[2].'">
';

PS: You need to update all your calls mysql_* for mysqli_*, because your code is incompatible with newer versions of PHP.

  • 2

    I found that I find it "ugly and confused" already does more than 2 years hehe

  • I used array of the form presented and worked perfectly for me. Thank you very much. As for the suggestion to upgrade to mysqli, I am doing this throughout the system gradually, I have not yet reached this page.

3

I agree with @bfavaretto ... the code with $variavel$i would be a bit confusing. To increment using his answer, I put a loop on the images, because if you change the LIMIT SQL, so you don’t need to add the tag again <img>, so he already does it alone:

<?php
$vv = mysql_query("SELECT * FROM $tabela ORDER BY rand() LIMIT 3");
$vertical = array();

while($v = mysql_fetch_array($vv)) {
    $vertical[] = $v['arquivo'];
}

for($i = 0; $i < count($vertical); $i++){
   echo '<img src="img/fotos/'.$vertical[$i].'"><br />';
}

2

Answering more of the same, could only delimit with keys.

Example:

$str1 = 'a';
$str2 = 'b';
${$str1.$str2} = 'c';

echo $ab;

Something more specific to the question code:

$i = '1';
${'vertical'.$i} = 'foo';

echo $vertical1;

I agree with the comments about the code because what you’re doing makes it difficult to read. There are certain cases where it can be applied well, but it does not seem to be the case you put in the question. However, I cannot judge whether what you do is right or wrong because I am oblivious to your business logic.

  • Thank you for answering. I have already solved the question with array but I will also test with keys for study purposes.

Browser other questions tagged

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