嵌套的 while 循环不起作用。

huangapple go评论68阅读模式
英文:

While loop within another while loop is not working

问题

我无法在另一个while循环内的while循环中获取数据。

我无法获取 - 在 "color_palettes_data" 的循环中,而这个循环又在另一个 "index_subjects_data" 的循环中。

alert (""); // 问题 - 只有最后一个项目出现

但当我点击不同的按钮 - 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

以下是翻译好的部分:

&lt;div class=&quot;row p_10&quot; id=&quot;select_subject_div&quot; style=&quot;display: none;&quot;&gt;
  &lt;?php
  $index_subjects_sql = &quot;SELECT * FROM index_subjects&quot;;
  $index_subjects_result = mysqli_query($conn, $index_subjects_sql);
  while ($index_subjects_data = mysqli_fetch_assoc($index_subjects_result)) {
    ?&gt;
    &lt;div id=&quot;index_subject_item_div_&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&quot; style=&quot;width: 240px; position: relative; float: left;&quot;&gt;
      &lt;div class=&quot;p_5 border_1 bg_black&quot; id=&quot;subject_color_palette_container_&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&quot; style=&quot;width: 350px; position: absolute; top: 100%; float: left; display: none; z-index: 1;&quot;&gt;
        &lt;?php
        $color_palettes_sql = &quot;SELECT * FROM `index_color_palettes`&quot;;
        $color_palettes_result = mysqli_query($conn, $color_palettes_sql);
        while ($color_palettes_data = mysqli_fetch_assoc($color_palettes_result)) {
          ?&gt;
          &lt;input type=&quot;button&quot; id=&quot;choose_subject_color_button_&lt;?=$color_palettes_data[&#39;color_palette_id&#39;]?&gt;&quot; style=&quot;background-color: &lt;?=$color_palettes_data[&#39;color_hex_code&#39;]?&gt;; border: none; width: 60px; height: 20px; margin: 0px; padding: 0px;&quot; value=&quot;&lt;?=$color_palettes_data[&#39;color_hex_code&#39;]?&gt;&quot; onclick=&quot;choose_subject_color_function(&#39;&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&#39;)&quot; &gt;
          &lt;?php 
        }
        ?&gt; &lt;!-- ---------- Close While : color_palettes_data ---------- --&gt;
      &lt;/div&gt;
    &lt;/div&gt; &lt;!-- ---------- Close : index_subject_item_div_ ---------- --&gt;
    &lt;?php 
  } 
  ?&gt; &lt;!-- ---------- Close While : index_subjects_data ---------- --&gt;
&lt;/div&gt; &lt;!-- ---------- Close : row ---------- --&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
function choose_subject_color_function(subjectId) {
  alert(subjectId);
}
&lt;/script&gt;
英文:

So what you're seeing is because of how PHP handles scoping.

You can try this code and let me know how it goes...

&lt;div class=&quot;row p_10&quot; id=&quot;select_subject_div&quot; style=&quot;display: none;&quot;&gt;
  &lt;?php
  $index_subjects_sql = &quot;SELECT * FROM index_subjects&quot;;
  $index_subjects_result = mysqli_query($conn, $index_subjects_sql);
  while ($index_subjects_data = mysqli_fetch_assoc($index_subjects_result)) {
    ?&gt;
    &lt;div id=&quot;index_subject_item_div_&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&quot; style=&quot;width: 240px; position: relative; float: left;&quot;&gt;
      &lt;div class=&quot;p_5 border_1 bg_black&quot; id=&quot;subject_color_palette_container_&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&quot; style=&quot;width: 350px; position: absolute; top: 100%; float: left; display: none; z-index: 1;&quot;&gt;
        &lt;?php
        $color_palettes_sql = &quot;SELECT * FROM `index_color_palettes`&quot;;
        $color_palettes_result = mysqli_query($conn, $color_palettes_sql);
        while ($color_palettes_data = mysqli_fetch_assoc($color_palettes_result)) {
          ?&gt;
          &lt;input type=&quot;button&quot; id=&quot;choose_subject_color_button_&lt;?=$color_palettes_data[&#39;color_palette_id&#39;]?&gt;&quot; style=&quot;background-color: &lt;?=$color_palettes_data[&#39;color_hex_code&#39;]?&gt;; border: none; width: 60px; height: 20px; margin: 0px; padding: 0px;&quot; value=&quot;&lt;?=$color_palettes_data[&#39;color_hex_code&#39;]?&gt;&quot; onclick=&quot;choose_subject_color_function(&#39;&lt;?=$index_subjects_data[&#39;subject_id&#39;]?&gt;&#39;)&quot; &gt;
          &lt;?php 
        }
        ?&gt; &lt;!-- ---------- Close While : color_palettes_data ---------- --&gt;
      &lt;/div&gt;
    &lt;/div&gt; &lt;!-- ---------- Close : index_subject_item_div_ ---------- --&gt;
    &lt;?php 
  } 
  ?&gt; &lt;!-- ---------- Close While : index_subjects_data ---------- --&gt;
&lt;/div&gt; &lt;!-- ---------- Close : row ---------- --&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
function choose_subject_color_function(subjectId) {
  alert(subjectId);
}
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年7月14日 02:03:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定