dououde4065 2014-03-28 05:29
浏览 48
已采纳

为数组中的每个项创建唯一ID,即使在页面上多次调用也是如此

On a website I am creating, I have the store 'open hours' in three locations on one page. One in the header, one in the body and one in the footer. I am trying to pull these in with PHP for simplicity when updating. I wrote an array:

<?php
$UniqID = uniqid(day);
$day1 = 'Mon-Fri: 9:00a-5:00p';
$day2 = 'Saturday: 9:00a-1:00p';
$day3 = 'Sunday: CLOSED';
$days = array(
    "1" => "<span id=" . $UniqID . " class='editable-text'>" . $day1 . "</span>",
    "2" => "<span id=" . $UniqID . " class='editable-text'>" . $day2 . "</span>",
    "3" => "<span id=" . $UniqID . " class='editable-text'>" . $day3 . "</span>",<
);
?>

and then am calling it on the page as such:

<span>Address | Phone <br/> Our Hours: <?php echo $days[1] ?>, <?php echo $days[2] ?>, <?php echo $days[3] ?></span>

The problem I have is that I need a unique ID EVERY time these are called to the page, and obviously this gives a unique ID for 1, 2 and 3 - but if I call that same thing on the page 2 or 3 times... well, then all three of the first instances have the same ID and so on.

So to sum it all up, I want to create an array that stores the hours like above - and be able to call them to the page however many times necessary but always get a unique ID wherever they show up.

Any help is appreciated, I can't process this anymore!

  • 写回答

2条回答 默认 最新

  • dtx9763 2014-03-28 05:35
    关注

    What you can do is declare the unique ID at the very top, like you have with $uniqueID=X

    Then, every time you insert $uniqueID into the DOM, increment it as well. So:

    <?php echo "<span id='".$uniqueID++."'>STUFF IN SPAN</span>"; ?>

    If $uniqueID is an int, the "++" will increment it, thus maintaining a unique one for each time it's inserted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?