4

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!

1

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Thomas! The first line of code, php for works well except it does all the users from 1-50. How can I define specific ids, since the ones I need to display are completely random. I will accept this answer shortly. Maybe something like <?php for ($i=1,3,24,85; $i++): ?>
Or could I wrap the for statement with an if statement, that will only display users if they have a specific field filled.<?php if ( $rank == 'Recruit' ); ?> <?php for ($i=0; $i<50; $i++): ?> <tr><td class="username"><a href="<?php $id=$i; include ("fields.php"); ?> <?php endfor; ?> <?php endif; ?>
Hi, sorry for the delay! If you have an array with the list of IDs you want, you should use foreach instead! I will edit the answer to reflect the solution, ok?
1

Yes, a loop.

for ($i = 1; $i <= 50; $i++) {
    //Do stuff with $i.
}

Comments

1
<?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
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.