MasterACE14 Posted September 16, 2007 Share Posted September 16, 2007 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 More sharing options...
rarebit Posted September 16, 2007 Share Posted September 16, 2007 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'><br></td></tr>"; } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349332 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 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 Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349335 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 Well, is it? print_r($array); Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349338 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 sorry? I dont understand Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349339 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 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? Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349341 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 no such luck. Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349343 Share on other sites More sharing options...
jscix Posted September 16, 2007 Share Posted September 16, 2007 <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> Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349344 Share on other sites More sharing options...
MasterACE14 Posted September 16, 2007 Author Share Posted September 16, 2007 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 Link to comment https://forums.phpfreaks.com/topic/69522-displaying-array-values-in-a-table/#findComment-349348 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.