英文:
While loop within another while loop is not working
问题
我无法在另一个while循环内的while循环中获取数据。
我无法获取 - =$index_subjects_data['subject_id'];?> 在 "color_palettes_data" 的循环中,而这个循环又在另一个 "index_subjects_data" 的循环中。
alert ("=$index_subjects_data['subject_id'];?>"); // 问题 - 只有最后一个项目出现
但当我点击不同的按钮 - choose_subject_color_button_.......... 时,我应该得到不同的主题ID。
如何在函数内部获取该值?
请帮忙。
谢谢。
英文:
I can not fetch data whithin a while loop which is another while loop.
<div class="row p_10" id="select_subject_div" style="display: none;">
<?php
$index_subjects_sql = "SELECT * FROM index_subjects";
$index_subjects_result = mysqli_query($conn, $index_subjects_sql);
while($index_subjects_data = mysqli_fetch_assoc($index_subjects_result)) {
?>
<div id="index_subject_item_div_<?=$index_subjects_data['subject_id']?>" style="width: 240px; position: relative; float: left;">
<div class="p_5 border_1 bg_black" id="subject_color_palette_container_<?=$index_subjects_data['subject_id']?>" style="width: 350px; position: absolute; top: 100%; float: left; display: none; z-index: 1;">
<?php
$color_palettes_sql = "
SELECT * FROM `index_color_palettes`";
$color_palettes_result = mysqli_query($conn, $color_palettes_sql);
while($color_palettes_data = mysqli_fetch_assoc($color_palettes_result)) {
?>
<input type="button" id="choose_subject_color_button_<?=$color_palettes_data['color_palette_id']?>" style="background-color: <?=$color_palettes_data['color_hex_code']?>; border: none; width: 60px; height: 20px; margin: 0px; padding: 0px;" value="<?=$color_palettes_data['color_hex_code']?>" onclick="choose_subject_color_function_<?=$color_palettes_data['color_palette_id']?>()" >
<script type="text/javascript">
function choose_subject_color_function_<?=$color_palettes_data['color_palette_id']?>()
{
alert ("<?=$color_palettes_data['color_name']?>"); // PROBLEM - only the last item is coming
alert ("<?=$index_subjects_data['subject_id']?>"); // PROBLEM - only the last item is coming
}
</script>
<?php
}
?> <!-- ---------- Close While : color_palettes_data ---------- -->
</div>
</div> <!-- ---------- Close : index_subject_item_div_ ---------- -->
<?php
}
?> <!-- ---------- Close While : index_subjects_data ---------- -->
</div> <!-- ---------- Close : row ---------- -->
I can not fetch the data of - <?=$index_subjects_data['subject_id']?> which is within a while loop of "color_palettes_data" which is within another while loop "index_subjects_data"
alert ("<?=$index_subjects_data['subject_id']?>"); // PROBLEM - only the last item is coming
But I should get different subject id when I click on different button - choose_subject_color_button_....................
How can I get the value there within the function ?
Please help.
Thanks.
答案1
得分: 2
首先,由于您在一个 while
循环中声明了 JavaScript 函数 choose_subject_color_function_()
,因此只会保留该函数定义的最后一次迭代。
这是我的意思:
while
循环将遍历数据并多次重新创建 choose_subject_color_function_
函数。
在每次循环中,您都传递一个将在 alert
函数中设置的变量。
下面是假设最后一次迭代在第 10 次的情况下,alert()
值的可视化概述:
迭代次数 | id | 结果 |
---|---|---|
0 | 01 | alert(01) |
1 | 02 | alert(02) |
... | ... | ... |
9 | 10 | alert(10) |
10 | 11 | alert(11) |
因此,您的警报将始终输出 while
循环的最后一次迭代的 subject_id
,如 alert(11)
。
解决方案
解决您的问题的方法是在 while
循环之外声明您的 JS 函数,并在函数的参数中传递要在 alert
中使用的值。
function choose_subject_color_function(subject_id) {
alert(subject_id)
}
英文:
First thing, since you are declaring the javascript function choose_subject_color_function_()
in a while
loop, only the last iteration will be kept for this function definition.
Here is what I mean:
The while
loop while iterate over the data and recreate the choose_subject_color_function_
function multiple times.
On each loop, you are passing a variable that will be set in the alert
function.
Here is an visual overview of what's happening with the alert()
value at each loop assuming that last iteration is at 10:
Iterations | id | Result |
---|---|---|
0 | 01 | alert(01) |
1 | 02 | alert(02) |
... | ... | ... |
9 | 10 | alert(10) |
10 | 11 | alert(11) |
Hence, your alert will always output the subject_id
of the last iteration of the while
loop, such as alert(11)
.
Solution
The solution to your problem resides in declaring your JS function outside of the while
loops and passing the value you want to alert
in the parameters of the function.
function choose_subject_color_function(subject_id) {
alert(subject_id)
}
答案2
得分: 0
以下是翻译好的部分:
<div class="row p_10" id="select_subject_div" style="display: none;">
<?php
$index_subjects_sql = "SELECT * FROM index_subjects";
$index_subjects_result = mysqli_query($conn, $index_subjects_sql);
while ($index_subjects_data = mysqli_fetch_assoc($index_subjects_result)) {
?>
<div id="index_subject_item_div_<?=$index_subjects_data['subject_id']?>" style="width: 240px; position: relative; float: left;">
<div class="p_5 border_1 bg_black" id="subject_color_palette_container_<?=$index_subjects_data['subject_id']?>" style="width: 350px; position: absolute; top: 100%; float: left; display: none; z-index: 1;">
<?php
$color_palettes_sql = "SELECT * FROM `index_color_palettes`";
$color_palettes_result = mysqli_query($conn, $color_palettes_sql);
while ($color_palettes_data = mysqli_fetch_assoc($color_palettes_result)) {
?>
<input type="button" id="choose_subject_color_button_<?=$color_palettes_data['color_palette_id']?>" style="background-color: <?=$color_palettes_data['color_hex_code']?>; border: none; width: 60px; height: 20px; margin: 0px; padding: 0px;" value="<?=$color_palettes_data['color_hex_code']?>" onclick="choose_subject_color_function('<?=$index_subjects_data['subject_id']?>')" >
<?php
}
?> <!-- ---------- Close While : color_palettes_data ---------- -->
</div>
</div> <!-- ---------- Close : index_subject_item_div_ ---------- -->
<?php
}
?> <!-- ---------- Close While : index_subjects_data ---------- -->
</div> <!-- ---------- Close : row ---------- -->
<script type="text/javascript">
function choose_subject_color_function(subjectId) {
alert(subjectId);
}
</script>
英文:
So what you're seeing is because of how PHP handles scoping.
You can try this code and let me know how it goes...
<div class="row p_10" id="select_subject_div" style="display: none;">
<?php
$index_subjects_sql = "SELECT * FROM index_subjects";
$index_subjects_result = mysqli_query($conn, $index_subjects_sql);
while ($index_subjects_data = mysqli_fetch_assoc($index_subjects_result)) {
?>
<div id="index_subject_item_div_<?=$index_subjects_data['subject_id']?>" style="width: 240px; position: relative; float: left;">
<div class="p_5 border_1 bg_black" id="subject_color_palette_container_<?=$index_subjects_data['subject_id']?>" style="width: 350px; position: absolute; top: 100%; float: left; display: none; z-index: 1;">
<?php
$color_palettes_sql = "SELECT * FROM `index_color_palettes`";
$color_palettes_result = mysqli_query($conn, $color_palettes_sql);
while ($color_palettes_data = mysqli_fetch_assoc($color_palettes_result)) {
?>
<input type="button" id="choose_subject_color_button_<?=$color_palettes_data['color_palette_id']?>" style="background-color: <?=$color_palettes_data['color_hex_code']?>; border: none; width: 60px; height: 20px; margin: 0px; padding: 0px;" value="<?=$color_palettes_data['color_hex_code']?>" onclick="choose_subject_color_function('<?=$index_subjects_data['subject_id']?>')" >
<?php
}
?> <!-- ---------- Close While : color_palettes_data ---------- -->
</div>
</div> <!-- ---------- Close : index_subject_item_div_ ---------- -->
<?php
}
?> <!-- ---------- Close While : index_subjects_data ---------- -->
</div> <!-- ---------- Close : row ---------- -->
<script type="text/javascript">
function choose_subject_color_function(subjectId) {
alert(subjectId);
}
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论