1

Basically I'm using this library someone put together to return information from my minecraft server. Everything works, except for this page I just want to list the mods and their version numbers. You can see the output here: http://litcraft.net/view2.php

As you can see, it's just using a loop to dump the information to the page. All I'm trying to do is go into the last array "modinfo" and ONLY pint out the modid and version in a list. I can format it once I get it, but I'm having an issue printing that list. Here is the code for what there is now:

<?php foreach( $Info as $InfoKey => $InfoValue ): ?>
    <tr>
        <td><?=htmlspecialchars($InfoKey); ?></td>
        <td><?php
if($InfoKey === 'favicon'){
    echo '<img width="64" height="64" src="' . Str_Replace("\n","",$InfoValue) . '">';
}else if(Is_Array($InfoValue)){
    echo "<pre>";
    print_r($InfoValue);
    echo "</pre>";
}else{
    echo htmlspecialchars( $InfoValue );
}
?></td>
                </tr>
<?php endforeach; ?>

So I tried to do this to dig into the array and print out only the mod list:

<?php 
$len = count($Info['modinfo']['modList']) + 1;
echo "<p>There are " . $len . " mods.</p>";
for($i=0;$i<$len;$i++){
    foreach($Info['modinfo']['modlist'][$i] as $ModID => $ModVersion): ?>
                <tr>
                    <td><?=$ModID; ?></td>
                    <td><?=$ModVersion; ?></td>
                </tr>
    <?php endforeach;
}?>

And all I get is the error you see on the page 175 times.... What am I doing wrong? I know it has to be something simple, but it's all starting to blend together at this point lol.

6
  • what is the error message? Commented Jun 11, 2015 at 18:56
  • Can you supply a formatted print_r of the $Info array? It would help us understand the array structure and get an idea of what you are doing. If it is long, you can shorten it to like 2 or three entries. Also, be sure to remove any private/personal information. Commented Jun 11, 2015 at 19:01
  • undefined index 'modlist'. That means there isn't a key 'modlist' in the array, but you ask for it in your loop, therefor the hundreds of errors. And that's also why you're getting the foreach error. Commented Jun 11, 2015 at 19:01
  • You should check for isset($Info['modinfo']['modList']) && is_numeric($Info['modinfo']['modList']) before taking the count of it Commented Jun 11, 2015 at 19:02
  • do print_r or var_dump of $Info and there may be a lot of instances where 'modlist' isn't set. Using isset as suggested above, or array_key_exists before using those values will help. Commented Jun 11, 2015 at 19:04

1 Answer 1

2

You can also do instead of foreach

echo $Info['modinfo']['modlist'][$i]['modid'] ;
echo $Info['modinfo']['modlist'][$i]['version'] ;
Sign up to request clarification or add additional context in comments.

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.