Reputation: 416
Basically I have a list of particular members that I am calling with their member ID, and displaying the td's afterwards on each one..
So I have the table like this
<table id="sortable" class="tablesorter" width="100%">
<thead>
<tr>
<th class="header"><strong>Game Username</strong></th>
<th class="header"><strong>Rank</strong></th>
<th class="header"><strong>Game Played</strong></th>
<th class="header"><strong>Playing On</strong></th>
<th class="header"><strong>Age</strong></th>
<th class="header"><strong>Time On</strong></th>
<th class="header"><strong>Location</strong></th>
</tr>
</thead>
<tbody>
<?php include ("users.php"); ?>
</tbody>
And the users.php file looks like this, but right now at over 50 lines of the same code as below, with only the $id= number changed..
<tr><td class="username"><a href="<?php $id='1'; include ("fields.php"); ?>
<tr><td class="username"><a href="<?php $id='2'; include ("fields.php"); ?>
<tr><td class="username"><a href="<?php $id='3'; include ("fields.php"); ?>
<tr><td class="username"><a href="<?php $id='4'; include ("fields.php"); ?>
<tr><td class="username"><a href="<?php $id='5'; include ("fields.php"); ?>
The fields.php file is the remainder of the table rows etc., starting with the below code to close off this td.
<?php echo $domain; $member_id = $id; $user_info = get_userdata($id);echo $user_info-> user_login.""; ?>"><?php echo xprofile_get_field_data( "$field_gamename" , $member_id); ?></a>
</td>
I'm trying to avoid including the fields.php file over 50 times in the users.php file. Anyone got an idea of the most efficient way?
Thanks!
Upvotes: 4
Views: 3793
Reputation: 193
This should work:
<?php for ($i=0; $i<50; $i++): ?>
<tr><td class="username"><a href="<?php $id=$i; include ("fields.php"); ?>
<?php endfor; ?>
On the other hand, you should probably re-factor your code so that you do not need to include the file on each loop, probably by either putting the code inline or (much better) by creating a function that does whatever you need to be done, something like this:
<?php for ($i=0; $i<50; $i++): ?>
<tr><td class="username"><a href="<?php do_some_stuff($i); ?>
<?php endfor; ?>
EDIT:
Since you want this to work for a defined list of IDs, you should create an array with that list and loop through it using the foreach() statement, like this:
<?php
$IdArray = array(1, 2, 25, 38);
foreach ($IdArray as $id): ?>
<tr><td class="username"><a href="<?php include ("fields.php"); ?>
<?php endforeach; ?>
Notice that I have removed the "$id = $i", since I am feeding the $id variable directly inside the foreach statement! ;)
BTW, you should close the tr, td and a tags before the "endforeach", ok?
Good luck!
Upvotes: 1
Reputation: 4625
<?php
for($i = 0; $i <= 50; $i++) {
?>
<tr>
<td class="username">
<?php $href = $domain . $user_info->get_userdata($i) . $user_info->user_login;?>
<a href="<?php echo $href;?>"><?php echo xprofile_get_field_data( "$field_gamename" , $i); ?></a>
</td>
</tr>
<?php
}
?>
Upvotes: 1
Reputation: 174967
Yes, a loop.
for ($i = 1; $i <= 50; $i++) {
//Do stuff with $i.
}
Upvotes: 1