Multi columns with category headings strategy?

Discussion in 'PHP' started by 123GoToAndPlay, Jan 26, 2010.

  1. #1
    Hi,

    I am trying to create a links list with links categories in a sort vertical display.
    For this i have
    
    $columns = 4;
    $num_rows = count($links);
    
    //we are going to set a new variables called $rows
    $rows = ceil($num_rows / $columns);
    //to do this display, we will need to run another loop
    //this loop will populate an array with all our values
    while($row = mysql_fetch_array($result)) {
        $data[] = $row['stuff'];
    }
    
    
    for($i = 0; $i < $rows; $i++) {
    
        echo "<tr>\n";
        
        //here will run another loop for the amount of columns
        for($j = 0; $j < $columns; $j++) {
            if(isset($data[$i + ($j * $rows)])) {
                echo "<TD>" . $data[$i + ($j * $rows)] . "</TD>\n";
            }
        }
        echo "</TR>\n";
    }
    
    
    Code (markup):
    And this works but now i want to have category headings

    In a one column list this works
    
    $category = '';
    foreach ($links as $link){
    $catName = $link['linkCat'];
    $linkName = $link['linkTitle'];
    $linkURL = $link['linkHREF'];
    	if ($catName != $category) {
    	//show heading
    	$category = $catName;
    	$msg .= "<tr>";
    	$msg .= "<td class=\"category\">";
    	$msg .= "<strong>".ucfirst($category['name'])."</strong>";
    	$msg .= "<br />";
    	$msg .= "</td>";
    	$msg .= "</tr>";
    	}//end heading
    $msg .= '<tr><td>';
    $msg .= $linkName;
    $msg .= '<br />';
    }
    
    Code (markup):
    But when i try to incorporate this in the vertical display it keeps repeating the category heading??

    with this i get the repeated category headings and not just once. I have attached a screenshot.
    
    echo "<TABLE BORDER=\"0\">\n";
    
    //here we changed the condition to $i < $rows
    for($i = 0; $i < $rows; $i++) {
    //heading
       echo "<TR>\n"; 
        //here will run another loop for the amount of columns
        for($j = 0; $j < $columns; $j++) {
    		$category = '';	
    			
            if(isset($data[$i + ($j * $rows)])) {
    			$catName = $data[$i + ($j * $rows)]['linkCat'];
    			if ($catName != $category) {
    				//show heading
    				$category = $catName;
    				//--echo "<tr>";
    				echo "<td class=\"category\">";
    				echo $category;
    				echo "<br />";
    				echo "</td>";
    				//echo "</tr>";
    			} 
                echo "<TD>" . $data[$i + ($j * $rows)]['linkTitle']. "</TD>\n";
            }
    		
        }
        echo "</TR>\n";
    }
    echo "</TABLE>\n";
    
    Code (markup):
    Any tips??
     

    Attached Files:

    Last edited: Jan 26, 2010
    123GoToAndPlay, Jan 26, 2010 IP
  2. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    
    <?php
    	$column = 4;
    	
    	$data = array (
    		0=>array("col1 data1", "col2 data1", "col3 data1", "col4 data1"),
    		1=>array("col1 data2", "col2 data2", "col3 data2", "col4 data2"),
    		2=>array("col1 data3", "col2 data3", "col3 data3", "col4 data3"),
    		3=>array("col1 data4", "col2 data4", "col3 data4", "col4 data4")
    	); 
    	
    	$table = "
    		<table border='1'>
    			<tr>
    				<td>Column 1</td>
    				<td>Column 2</td>
    				<td>Column 3</td>
    				<td>Column 4</td>
    			</tr>
    	";
    	
    	for($i=0; $i<$column; $i++)
    	{
    		$table .= "<tr>
    				<td>".$data[$i][0]."</td>
    				<td>".$data[$i][1]."</td>
    				<td>".$data[$i][2]."</td>
    				<td>".$data[$i][3]."</td>
    			</tr>";
    	}
    	$table .= "</table>";
    	
    	echo $table;
    ?>
    
    
    PHP:
     
    blacksheep666, Jan 26, 2010 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @blacksheep666, tx but that is not what i am after

    this is what i had in mind
    
      <table border='1'>
        <tr>
                    <td><strong>col1 </strong></td>
                    <td> data5</td>
    
                    <td><strong>col3</strong> </td>
                    <td>data5</td>
                </tr>
        <tr>
          <td>data1</td>
          <td>data6</td>
          <td>data1</td>
          <td>data6</td>
        </tr>
        <tr>
            <td>data2</td>
            <td><strong>col2</strong> </td>
            <td> data2</td>
    
            <td> <strong>col4</strong> </td>
                </tr><tr>
                    <td> data3</td>
                    <td>data1</td>
                    <td>data3</td>
                    <td>data1</td>
    
                </tr><tr>
                    <td>data4</td>
                    <td> data2</td>
                    <td> data4</td>
                    <td>data2</td>
                </tr></table>
    
    
    HTML:
    My theory is
    //check the linkCat of each link
    //if this linkCat is != earlier cat
    //print it else no print

    Something like this http://www.tugbucket.net/wp-content/uploads/mctagmap-2col.gif
     
    123GoToAndPlay, Jan 26, 2010 IP
  4. blacksheep666

    blacksheep666 Active Member

    Messages:
    68
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    
    <?php
        $column = 4;
       
        $data = array (
            "A"=>array("col1 data1", "col2 data1", "col3 data1", "col4 data1"),
            "B"=>array("col1 data2", "col2 data2", ),
            "C"=>array("col1 data3", "col2 data3", "col3 data3", ),
            "D"=>array("col1 data4", "col2 data4", "col3 data4", "col4 data4","col4 data4","col4 data4")
        );
       
        $table = "<table border='0'>";
    	foreach ($data as $key => $val)
    	{
    		if ($val)
    		{
    			$table .= "
    				<tr>
    					<td><b>{$key}</b></td>
    				</tr>
    				<tr>
    					<td><hr /></td>
    				</tr>";
    			
    			foreach ($val as $data_val)
    			{
    				$table .= "
    					<tr>
    						<td>{$data_val}</td>
    					</tr>
    				";				
    			}
    		}
    	}
    	
    	$table .= "</table>";
    	
    	echo $table;
    ?>
    
    
    PHP:
     
    blacksheep666, Jan 26, 2010 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    hmm,

    couple of things
    1/ how to make this multi columns?
    2/ how to dynamicly populate the arrays?
     
    123GoToAndPlay, Jan 26, 2010 IP
  6. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm, ok now i know how to color each link per category(see attach).
    But still i want to have the category name once and the appropiate links underneath it and over category list can be on two colums.
     

    Attached Files:

    123GoToAndPlay, Jan 26, 2010 IP