How to show data in rows instead of cols?

Hello,

I want to show data by rows, for eg. row1, 2, 3, 4, 5, 6, 7, 8, 9 then next column then continue again with row1, 2, 3, 4 5 etc in a table. I am not able to do it.

	$q1 = "SELECT * FROM subcategories WHERE CatID = " . sql($a0["CatID"]);
	$r1 = mysql_query($q1) or die("Query Failed");
	
	echo "<table border='1' cellpadding='0' cellspacing='0'>\
";
	
	while ($a1 = mysql_fetch_array($r1))
	{
		if ($subrow == 0) { echo "<tr>\
"; }
			echo "<td>" . $a1["SubcatName"] . "</td>\
";
		if ($subrow == 8) { echo "</tr>\
"; $subrow = 0; } else { $subrow++; }
	}
	
	echo "</table>\
";

How to do it row wise instead of col. wise ?

Thanks.

	
while ($a1 = mysql_fetch_array($r1))
{
  foreach($a1 AS $key => $value) {
    $data[$key][] = $value;
  }
}
foreach($data AS $key => $set) {
  echo "<tr><th>".$key."</th><td>".implode("</td><td>",$set)."</td></tr>";
}

Hi,

This does’nt seems to be doing anything like I wanted.

Thanks.

That code should do exactly what you asked for. Display the row as a column.

Hi,

I guess its not doing it cause you have used foreach and used all the data that is coming and its causing a lot of wrong data showing up. And i think its not working anyways even if i just put a single column in my select query still i think its showing the result incorrectly.

Its showing 2 rows with identical data.

Thanks.

Well thats because your query is selecting * instead of just the catname…

what exactly do you want the output to look like?

I have put * because i need other data too but its not required here. Anyways as I said earlier

I want to be able to show data row wise not col wise.

Suppose the query returns 100 records.

I want to show them like this:


1    11    21    
2    12    22    
3    13    23
4    14    24
5    15    25
6    16    26
7    17    27
8    18    28
9    19    29
10   20    30...

and so on.

I hope i have made myself clear.

Thanks.

Ahh. Okay. I get you now.


        $catnames = array(); //Make sure we're empty...
        $rows = 10; //How many rows you want in your table.
	while ($a1 = mysql_fetch_array($r1)) {
             $data[] = $a1; //For later use.
             $catnames[] = $a1['SubcatName'];
        }
        for($i = 0; $i < $rows; $i++) { 
            $start = $i;
            echo "<tr>";
            while(isset($catnames[$start])) {
              echo "<td>".$catnames[$start]."</td>";
              $start += $rows;
            }
            echo "</tr>";
         }

Hello,

Still not working. Will it help if all the data is in a single array ? Then just split it and display it in rows ?

Thanks.

Hi,

Please goto Barrington Sports | Cricket Bats | Hockey Sticks | Rugby Boots | Netballs | The Specialist Sports Equipment Shop and you will see the menu bar. You can see how they have created that menu. Its showing cats / subcats vertically. I am trying to achieve same thing.

Thanks.

The behaviour you’re trying to achieve will not result in a menu like the one listed, they use simple nested lists and CSS to create that layout.

Hi,

How can i achieve it ? Please help / guide me.

Thanks.