I'm working on some HTML wireframes.
There is some HTML code I'd like to be able to repeat 'X' number of times.
eg
<li>my list item</li>
So I put that code into a separate PHP file called "myListItem.php". And then in my main HTML template. I have some code that looks like this:
<ul>
<?php include('myListItem.php');?>
<?php include('myListItem.php');?>
<?php include('myListItem.php');?>
<?php include('myListItem.php');?>
</ul>
This feels redundant.
Is there a simple way to repeat the inclusion of the same file 'X' number of times?
Ideally, I'd be able to have something like the following in my HTML template:
<ul>
<?php include('myListItem.php'); myItem(4);?>
</ul>
Just figuring out this stuff, so I apologize for what must be an obvious question to many. Keep in mind of course that the code I'm repeating in real-life is actually much longer than a simple list item, which in my example is humorously shorter that the php include itself.
for ($i = 0; $i < XXX; $i++) { include('myListItem.php'); }
You are right - this is redundant . From your description, including the same file 4 times is the wrong way to solve this problem.
Suitable alternatives would be:
Extract all logic to the included so you can simply do the following:
<ul> <?php include('myListItem.php');?> </ul>
Create a function within your included file
<?php include('myListItem.php');?> <!-- other code... --> <ul> <?php displayListItems(4); ?> </ul>
Either way, you are only including the file once.
<?php for ($i = 0; $i < 4; $i++) include('myListItem.php'); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.