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!
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.
Instead of declaring one unique ID at the top, declare it once for every <span>
. It is also possible to declare it in a loop with a variable variable, like this:
$day1 = 'Mon-Fri: 9:00a-5:00p';
$day2 = 'Saturday: 9:00a-1:00p';
$day3 = 'Sunday: CLOSED';
for ($i = 1; $i <= 3; $i++) {
$days[$i] = "<span id='uid" . uniqid() . "' class='editable-text'>" . ${'day'.$i} . "</span>";
}
This will produce something like:
Array
(
[1] => <span id='uid53350c2f730eb' class='editable-text'>Mon-Fri: 9:00a-5:00p</span>
[2] => <span id='uid53350c2f73156' class='editable-text'>Saturday: 9:00a-1:00p</span>
[3] => <span id='uid53350c2f731d0' class='editable-text'>Sunday: CLOSED</span>
)
Note that the id
attribute cannot start with a number (or else it's not valid HTML), so you should probably put some letters before the uniqid() like I did (with uid
). Also, you were missing quotes around the id
value.