Jump to content

Recommended Posts

morning everyone,

 

I have a Multi-Dimensional Array set up like this:

<?php
$weapons = array( array( id => 0,
					 weapon => "L85A2", 
					 price => 250,
					 damage => 15,
					 type => "Rifle",
					 rarity => "Basic",
					 description => "The L85A2 is a bullpup assault rifle, it can be fitted with a laser. 
           				                 A iron-sight comes standard.",
					 options => "You can give it a Laser."
					),
		   array( id => 1,
					 weapon => "AK-47", 
					 price => 325,
					 damage => 25,
					 type => "Rifle",
					 rarity => "Good",
					 description => "The AK-47(Kalashnikov) is a Russian made assault rifle, it is one of the most
						                 popular weapons in the world, and is also one of the most reliable.",
					 options => "You can give it a iron-sight."
					),
               array( id => 2,
					 weapon => "L96", 
					 price => 400,
					 damage => 30,
					 type => "Sniper Rifle",
					 rarity => "Good",
					 description => "The L96(super magnum) is a sniper rifle with deadly accuracy.",
					 options => "You can give it a stand."
					),
             );
?>

 

And I'm wondering how do I display the arrays in a table like what I've done here:

<?php

// loop through records writing a table for each one
while ( $row = mysql_fetch_array( $rs ) )
{

?>
<table class = "fix">
<tr>
<td><b><?php echo $row["author"]; ?></b></td>

<?php 

$datetime = $row["posttime"];

?>
<td><b><?php echo date("F j, Y", strtotime($datetime)); ?></b></td>
</tr>


<tr><td colspan="2">
<?php echo $row["text"]; ?>
</td></tr>
</table>
<br>
</center>

<?php } ?>

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/
Share on other sites

I'm getting a few errors:

Warning: main(www.crikeygames.com.au/conflictingforces/weapons.php) [function.main]: failed to open stream: No such file or directory in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 3

 

Warning: main(www.crikeygames.com.au/conflictingforces/weapons.php) [function.main]: failed to open stream: No such file or directory in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 3

 

Warning: main() [function.include]: Failed opening 'www.crikeygames.com.au/conflictingforces/weapons.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 3

 

Warning: Invalid argument supplied for foreach() in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 6

 

in encyclopedia.php :

<?php // encyclopedia

include 'www.crikeygames.com.au/conflictingforces/weapons.php';

echo "<table><tr><td colspan='2'>WEAPONS</td></tr>";
foreach($weapons as $w)
{
while (list($k, $v) = each($l))
{
	echo "<tr><td>".$k."</td><td>".$v."</td></tr>";
}
echo "<tr><td colspan='2'></td></tr>";
}
echo "</table>";

?>

 

 

EDIT:

 

I've now got it including correctly. Now the only errors Im getting are:

Warning: Variable passed to each() is not an array or object in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 8

 

Warning: Variable passed to each() is not an array or object in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 8

 

Warning: Variable passed to each() is not an array or object in /home/ace/public_html/conflictingforces/lib/encyclopedia.php on line 8

Well, the error message is telling you, that you are passing a non-array.. or non enumerable object to a foreach loop, thus it can do nothing with it..

 

I'm asking if you dump the content of your variable, (Which should be an array) does it output an array?

<html>
<body>
<?php

$weapons = array( array( id => 0,
					 weapon => "L85A2", 
					 price => 250,
					 damage => 15,
					 type => "Rifle",
					 rarity => "Basic",
					 description => "The L85A2 is a bullpup assault rifle, it can be fitted with a laser. 
           				                 A iron-sight comes standard.",
					 options => "You can give it a Laser."
					),
		   array( id => 1,
					 weapon => "AK-47", 
					 price => 325,
					 damage => 25,
					 type => "Rifle",
					 rarity => "Good",
					 description => "The AK-47(Kalashnikov) is a Russian made assault rifle, it is one of the most
						                 popular weapons in the world, and is also one of the most reliable.",
					 options => "You can give it a iron-sight."
					),
               array( id => 2,
					 weapon => "L96", 
					 price => 400,
					 damage => 30,
					 type => "Sniper Rifle",
					 rarity => "Good",
					 description => "The L96(super magnum) is a sniper rifle with deadly accuracy.",
					 options => "You can give it a stand."
					),
             );
            echo "<table border=\"1\">";
            foreach ($weapons as $topof) {
               print "<tr><td colspan='2'>WEAPONS</td></tr>";
               foreach ($topof as $lowerof => $desc) {
                if (substr($lowerof,0,2) !== "id") { 
                print "<tr><td>" . $lowerof . "</td><td>" . $desc . "</td></tr>";
                }
               } 
               echo "<tr><td colspan='2'></td></tr>";
             }
             echo "</table>";
             
             
             
             ?>
             
             </body>
             </html>

Excellant! that works perfectly, thanks rarebit and jscix.

 

I forgot to ask earlier, but how can I exclude certain fields in the array from being echo'd for example, If I dont want

say weapon 0(L85A2) to be displayed because its rarity is "Basic" how can I make it show it excludes all weapons that are "Basic"? and how can I exclude certain id's for example, exclude id 2(L6) from being echo'd but echo all the other weapons.

 

Regards ACE

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.