Jump to content

[SOLVED] Formatting with loops and arrays.


pocobueno1388

Recommended Posts

Hello =]

 

I am creating a forum script, and I am trying to make it as easy as possible to add/remove forums within the script. The part I am having trouble with is just displaying the different boards in a dynamic way.

 

Here is the format I am using in my array

 array("Is it a main or sub forum?", "Display Text", "URL", "Display Text")

 

Here is what I currently have it displaying...which is not even close to how I want it.

main

General Board

forum.php?id=1

This should go under the General Boad

     Sub Genderal

forum.php?id=2

This should go under the Sub general Board

main

Misc Board

forum.php?id=3

This should go under the Misc Board

     Sub Misc

forum.php?id=4

This Should go under the Sub Misc Board

 

This is what I am trying to get:

General Board <---links to forum.php?id=1
This should go under the General Board

    Sub General <-----Links to forum.php?id=2
    This should go under the Sub General Board

Misc Board <---Links to forum.php?id=3
This should go under the Misc Board

   Sub Misc <---Links to forum.php?id=4
   This should go under the Sub Misc board

 

Here is my current code in attempting to get the right output:

<?php

//Board Information
$boards = array(
           array("main", "General Board", "forum.php?id=1", "This should go under the General Boad"),
           array("sub", "Sub Genderal", "forum.php?id=2", "This should go under the Sub general Board"),
           array("main", "Misc Board", "forum.php?id=3", "This should go under the Misc Board"),
           array("sub", "Sub Misc", "forum.php?id=4", "This Should go under the Sub Misc Board")
           );


for ($row=0; $row < count($boards); $row++){
   for ($col=0; $col < 4; $col++){
   
     //If the category is a sub category, indent it
      if ($col == 0 && $boards[$row][$col] == 'sub'){
         echo '     ';
      continue;
      }
   
      echo $boards[$row][$col].'<p>';
      
   }
}

?>

 

Any help with this is greatly appreciated =D Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/54621-solved-formatting-with-loops-and-arrays/
Share on other sites

try

<?php
//Board Information
$space = '     ';
$ident_array = array('main' => 0, 'sub' => 1, 'subsub' => 2);
$boards = array(
           array("main", "General Board", "forum.php?id=1", "This should go under the General Boad"),
           array("sub", "Sub Genderal", "forum.php?id=2", "This should go under the Sub general Board"),
           array("subsub", "Sub Sub Genderal", "forum.php?id=12", "This should go under the Sub sub general Board"),
           array("main", "Misc Board", "forum.php?id=3", "This should go under the Misc Board"),
           array("sub", "Sub Misc", "forum.php?id=4", "This Should go under the Sub Misc Board")
           );
foreach ($boards as $x) {
echo str_repeat($space, $ident_array[$x[0]]),'<a href="',$x[2],'">',$x[1],'</a><br />',"\n";
echo str_repeat($space, $ident_array[$x[0]]),$x[3].'<br />';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.