Есть ли способ получить все извлеченные идентификаторы из цикла PHP в то время как? - PullRequest
0 голосов
/ 10 ноября 2019

Я хочу знать, смогу ли я получить все извлеченные идентификаторы из цикла while в PHP, чтобы использовать их в jQuery для эффекта отражения?

while ($fetch_locomotion = mysqli_fetch_assoc($result_locomotion))
{
    $id = $fetch_locomotion['id'];

    echo 
    '<a href="index.php?price='.$hash.'" style="text-decoration: none;" id="object_a_jquery">
        <div id="'.$id.'" style="perspective:1000px;height:100%;width:13%;display:inline-table;">
            <div class="front">
                <div id="object_list_price_bg" style="background-image:url(images/bg_price_f.png);"></div>
            </div>
            <div class="back">
                <div id="object_list_price_bg_b" style="background-image:url(images/bg_price.png);"></div>
            </div>
        </div>
    </a>';
}


<script>$(<?php echo $id;?>).flip();</script>

Ответы [ 2 ]

1 голос
/ 10 ноября 2019

Вы можете сбросить их в массив и использовать их позже.

// do this before entering the loop so it isn't overwritten
$ids = [];
// Do this part inside of the while loop
$ids[] = $fetch_locomotion ['id'];

?>

/* do this part in the html section you wish to execute the javascript code */
<script>
jQuery(function ($) {
    <?php foreach ($ids as $id): ?>
        <?php echo "$('#". $id . "').flip();\n"; ?>
    <?php endforeach; ?>
});
</script>

Отредактировано для вашего точного кода:

$ids = [];
while ($fetch_locomotion = mysqli_fetch_assoc($result_locomotion))
{

    $ids[] = $fetch_locomotion['id'];

    echo 
    '<a href="index.php?price='.$hash.'" style="text-decoration: none;" id="object_a_jquery">
        <div id="'. $fetch_locomotion ['id'] .'" style="perspective:1000px;height:100%;width:13%;display:inline-table;">
            <div class="front">
                <div id="object_list_price_bg" style="background-image:url(images/bg_price_f.png);"></div>
            </div>
            <div class="back">
                <div id="object_list_price_bg_b" style="background-image:url(images/bg_price.png);"></div>
            </div>
        </div>
    </a>';
}


<script>
jQuery(function ($) {
    <?php foreach ($ids as $id): ?>
        <?php echo "$('#". $id . "').flip();\n"; ?>
    <?php endforeach; ?>
});
</script>
0 голосов
/ 10 ноября 2019

Я бы предложил добавить атрибут класса и запросить его (вместо каждого идентификатора отдельно).

Кроме того, использование альтернативного синтаксиса PHP в этом контексте будет более понятным.

Это становится:

<?php while ($fetch_locomotion = mysqli_fetch_assoc($result_locomotion)): ?>
  <a href="index.php?price=<?= $hash ?>" style="text-decoration: none;" id="object_a_jquery_<?= $id ?>">
    <div id="<?= $fetch_locomotion['id'] ?>" class="js-flipMe" style="perspective:1000px;height:100%;width:13%;display:inline-table;">
      <div class="front">
        <div id="object_list_price_bg" style="background-image:url(images/bg_price_f.png);"></div>
      </div>
      <div class="back">
        <div id="object_list_price_bg_b" style="background-image:url(images/bg_price.png);"></div>
      </div>
    </div>
  </a>
<?php endwhile ?>

<script>
  jQuery(function ($) {
    $('.js-flipMe').flip();
  });
</script>

Обратите внимание, что я также изменил ваши <a> 's id атрибут должен быть уникальным, как и должно быть.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...