简体   繁体   English

如何在php echo中使用变量将id传递给div?

[英]How do use a variable in a php echo to pass an id to a div?

I am creating a random musical note generator using PHP and HTML. 我正在使用PHP和HTML创建一个随机音符生成器。 I have placed all the positions for the musical notes in a css file and would like to do something like this: 我已将音符的所有位置放在css文件中,并希望执行以下操作:

echo 'div id=note_array[rand_numbs[1]]  class="note_1st"> &#x2669; </div>';    

The note array provides something like id="E", or id="A". 音符数组提供类似id =“E”或id =“A”的内容。 Of course, it doesn't work. 当然,它不起作用。 Is there anyway to pass a variable in echo as an id for a tag? 反正是否将echo中的变量作为标记的id传递?

You have error in the concatenation and your open div, also I guess note_array[rand_numbs[1]] is an array and should be $note_array['rand_numbs'][1] 你在连接和你的开放div中有错误,我猜也是note_array[rand_numbs[1]]是一个数组,应该是$note_array['rand_numbs'][1]

echo '<div id="'.$note_array['rand_numbs'][1].'" class="note_1st"> &#x2669; </div>';   

I prefer write it in this way 我更喜欢用这种方式写它

echo "<div id='{$note_array['rand_numbs'][1]}' class='note_1st'> &#x2669; </div>";   

Try echo 'div id='.note_array[rand_numbs[1]].' class="note_1st"> ♩ '; 尝试echo 'div id='.note_array[rand_numbs[1]].' class="note_1st"> ♩ '; echo 'div id='.note_array[rand_numbs[1]].' class="note_1st"> ♩ ';

I think you should be able to echo it like so: 我认为你应该像这样回应它:

echo 'div="' . note_array[rand_numbs[1]] . '" class="note_1st"';

Using dot notation you can "glue" together a string that contains a variable or a piece of an array like you're doing here. 使用点表示法,您可以将包含变量或数组的字符串“粘合”在一起,就像您在此处所做的那样。

Instead of this: 而不是这个:

echo 'div id=note_array[rand_numbs[1]]  class="note_1st"> &#x2669; </div>';

use this: 用这个:

echo 'div id="' . note_array[rand_numbs[1]] . '"  class="note_1st"> &#x2669; </div>';

You need to enclose the value of id into quotes, also you can use dot notation to solve your problem. 您需要将id的值括在引号中,也可以使用点表示法来解决您的问题。

Also, you can use dot notation at any string in php. 此外,您可以在php中的任何字符串使用点表示法。

With echo , you can use the dot (.) and the comma (,). 使用echo ,您可以使用点(。)和逗号(,)。 This last is sometimes more pratical than the dot. 这最后有时比点更实际。 It's more easier to write the code with it. 使用它编写代码更容易。 Also, you didn't always need concatenation and you can get better performance (a few). 此外,您并不总是需要连接,您可以获得更好的性能(一些)。

Of course, your code will not work also if you forget the $ . 当然,如果你忘记$ ,你的代码也行不通。 I guess too such like Emilio Gort that the note_array is in fact a var (an array). note_array Emilio Gort一样, note_array实际上是一个var(一个数组)。 So, to solve your issue, I think you can do something like this : 所以,为了解决你的问题,我认为你可以这样做:

echo '<div id="',$note_array['rand_numbs'],'" class="note_1st"> &#x2669; </div>';

You didn't mention the content of your array. 您没有提到阵列的内容。 It isn't clear for me. 我不清楚。 So, you will maybe need to add some changes such like this : 所以,您可能需要添加一些如下所示的更改:

echo '<div id="',$note_array['rand_numbs']['1'],'" class="note_1st"> &#x2669; </div>';
echo '<div id="',$note_array['rand_numbs']['SomethingeElse'],'" class="note_1st"> &#x2669; </div>';

You can use ['1'] or [1] . 您可以使用['1'][1] The both syntax will work. 这两种语法都有效。 The first is supposed to allow some better performance. 第一个应该是允许更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM