Forum Moderators: coopster

Message Too Old, No Replies

Problem with ordering the output of an array in a table

         

jsnoland

1:52 pm on Sep 30, 2004 (gmt 0)

10+ Year Member



I am trying to output some data in a 2-dimensional array into a simple two-column table (categories and subcategories) and all I want to show is categories on the left and subcategories on the right - however the keys keep displaying, pushing all of the values into the right column. I have tried tweaking the foreach statements about a thousand different ways to no avail. I suspect it is something very simple that I can't see due to having stared at it for the last several hours. Any help or suggestions would be greatly appreciated. Thanks!

---------------------------------------

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

// Create category/subcategory query.
$category_subcategory_query = "SELECT * FROM x_categories_subcategories WHERE x_categories_subcategories_id = '$row[1]'";

// Run query and assign returned result to variable.
$category_subcategory_result = @mysql_db_query(DB_NAME, $category_subcategory_query);
$category_subcategory_row = mysql_fetch_array( $category_subcategory_result, MYSQL_NUM);

$category = $category_subcategory_row[1];
$subcategory = $category_subcategory_row[2];

$categories_subcategories[$i] = array($category, $subcategory);
$i++;

}

// Alphabetize the category/subcategory results.
sort($categories_subcategories);

// Iterate through the categories/subcategories and insert them into the table.

foreach ($categories_subcategories as category_subcategory) {
foreach ($category_subcategory as $category => $subcategory) {

$bg = ($bg=='#eeeeee'? '#ffffff' : '#eeeeee'); // Alternate background color on each row.

echo '<TR BGCOLOR="', $bg, '">
<TD ALIGN="left">', $category, '</TD>
<TD ALIGN="left">', $subcategory, '</TD>
</TR>';
}
}

coopster

10:43 pm on Sep 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, jsnoland!

Maybe you could show us some data instead, just a few rows. Show us what it looks like coming in, and what you want it to look like.

jsnoland

1:39 am on Oct 1, 2004 (gmt 0)

10+ Year Member



Here is an example of what I want the output to resemble:

Category ¦ Subcategory
--------------------------
Category 01 ¦ Subcategory 03
Category 04 ¦ Subcategory 07
Category 08 ¦ Subcategory 09

Instead this is what is displaying (note the keys displaying in the left column and all values being pushed into the second column):

Category ¦ Subcategory
--------------------------
0 ¦ Category 01
1 ¦ Subcategory 03
0 ¦ Category 04
1 ¦ Subcategory 07
0 ¦ Category 08
1 ¦ Subcategory 09

The lines don't match up well in these examples but I tried to show the two colums as well as the board would let me. Sorry I didn't include a good example earlier - my mind had been reduced to mush by the time I threw up my arms and decided to ask for help : ) Thanks!

StupidScript

5:00 am on Oct 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Taking a guess at the basics of your table structures:

x_categories
autoinc_id,x_category_name ...

x_categories_subcategories
autoinc_id,x_categories_subcategories_id,x_categories_subcategory_name ...

where x_categories_subcategories.x_categories_subcategories_id = x_categories.x_category_name

Something like that? It would help if you would post a couple of rows of data similar to one used in your example.