Hi jpadie,
This is the code i am using
<?php
session_start();
//connect to database
include("config.php");
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die(mysql_error());
mysql_select_db("$dbname",$conn) or die(mysql_error());
//show stores first
$query = "select store_title, store_link from store_name order by store_title";
$result = mysql_query($query);
//we add this line because we need to know the number of rows
# $num_rows = mysql_num_rows($result); //YOU DONT NEED THIS
//this section stores all the rows into an array
while ($row = mysql_fetch_array($result)):
$sortletter = strtoupper(substr(trim($row['store_title']),1)); //this takes the sort key and stores the first letter converted into uppercase
$array[$sortletter][] = $row; //stores each line to a row
endwhile;
//now free the resultset
mysql_free_result($result);
for ($i=65; $i <=90; $i++): //need this loop for ascii values 65 = A NOTE THAT THIS DOES NOT DEAL WITH STORES THAT DON@T HAVE AN A-Z start letter
$cnt = 0;
if (isset($array[chr($i)])):
$table = "<table> <tr><td colspan=\"3\">".chr($i)."</td></tr>";//prints out a row with the letter in it
$cnt = count ($array[chr($i)]);
$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
for ($j=0; $j<$cnt; $j+2):
$table .=
"<tr>
<td>
<a href=\"".stripslashes($array[chr($i)][$j]['store_link'])."\">".stripslashes($array[chr($i)][$j]['store_title'])."</a>
</td>
<td>".
htmlspecialchars(" ") . "</td>"; //use this for a spacer row
if ($even): //use this to decide whether to add another column or just dummy it off
$table .=
"
<td>
<a href=\"".stripslashes($array[chr($i)][$j+1]['store_link'])."\">".stripslashes($array[chr($i)][$j+1]['store_title'])."</a>
</td>
</tr>";
else:
$table .=
"
<td>
</td>
</tr>";
endif;
endfor;
$table .= "</table><br/><br/>"; //appends a table open tag
endif; // no need for an else as we don't want blank letters to print
endfor;
?>
//show stores first
$query = "select subcat_id, store_title, store_link from store_name where subcat_id = ".$subcat_id." order by store_title";
$result = mysql_query($query);
//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
//echo "<TABLE class=list_all_table cellpadding=\"3\" style=width: \80\%\; background-color: rgb(102, 204, 255)\n";
echo "<TABLE BORDER=\"0\">\n";
//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['store_title'];
}
//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $rows; $i++) {
$store_link = stripslashes($row[store_link]);
//if($i % $columns == 0) {
//if there is no remainder, we want to start a new row
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 width=35%><a href=\"$store_link\">" . $data[$i + ($j * $rows)] . "</a></TD>\n";
}
// echo "<TD width=\35\%\><a href=\"$store_link\"> " .$row['store_title']. "</a></TD>\n";
// if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//if there is a remainder of 1, end the row
//or if there is nothing left in our result set, end the row
echo "</TR>\n";
}
}
echo "</TABLE>\n";
?>
This is what I am getting
//show stores first $query = "select subcat_id, store_title, store_link from store_name where subcat_id = ".$subcat_id." order by store_title"; $result = mysql_query($query); //we add this line because we need to know the number of rows $num_rows = mysql_num_rows($result); //echo "
\n"; //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['store_title']; } //changed this to a for loop so we can use the number of rows for($i = 0; $i < $rows; $i++) { $store_link = stripslashes($row[store_link]); //if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "\n"; //} //here will run another loop for the amount of columns for($j = 0; $j < $columns; $j++) { if(isset($data[$i + ($j * $rows)])) { echo "\n"; } // echo "\n"; // if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row //or if there is nothing left in our result set, end the row echo "\n"; } } echo "
" . $data[$i + ($j * $rows)] . " " .$row['store_title']. "
\n"; ?>
This is the data
-- phpMyAdmin SQL Dump
-- version 2.6.0-pl3
--
--
-- Host: localhost
-- Generation Time: Dec 25, 2005 at 09:53 AM
-- Server version: 3.23.58
-- PHP Version: 4.3.10
--
--
-- --------------------------------------------------------
--
-- Table structure for table `store_name`
--
CREATE TABLE `store_name` (
`store_title` varchar(75) default NULL,
`store_image` varchar(75) default NULL,
`store_id` int(11) NOT NULL auto_increment,
`subcat_id` int(11) default NULL,
`store_link` varchar(200) default NULL,
PRIMARY KEY (`store_id`)
) TYPE=MyISAM AUTO_INCREMENT=385 ;
--
-- Dumping data for table `store_name`
--
INSERT INTO `store_name` VALUES ('Almond Plaza', NULL, 219, 1, '/coupon/stores/0-a/almondplaza.php');
INSERT INTO `store_name` VALUES ('Ashford.com', NULL, 220, 1, '/coupon/stores/0-a/ashford.php');
INSERT INTO `store_name` VALUES ('ADT - Home Security', NULL, 221, 1, '/coupon/stores/0-a/adt.php');
INSERT INTO `store_name` VALUES ('123Inkjets.com', '/coupon/images/smalllogos/123inkjets.gif', 20, 0, '/coupon/stores/0-a/123inkjets.php');
INSERT INTO `store_name` VALUES ('1-800-Patches', NULL, 142, 0, '/coupon/stores/0-a/1800patches.php');
INSERT INTO `store_name` VALUES ('1-800-PetMeds', NULL, 140, 0, '/coupon/stores/0-a/1800petmeds.php');
INSERT INTO `store_name` VALUES ('1-800-WheelChair', NULL, 138, 0, '/coupon/stores/0-a/1800wheelchair.php');
INSERT INTO `store_name` VALUES ('3balls.com', NULL, 24, 0, '/coupon/stores/0-a/3balls.php');
INSERT INTO `store_name` VALUES ('Camping World', NULL, 271, 3, '/coupon/stores/c/campingworld.php');
INSERT INTO `store_name` VALUES ('Air France', NULL, 227, 1, '/coupon/stores/0-a/airfrance.php');
INSERT INTO `store_name` VALUES ('Avenue.com', NULL, 226, 1, '/coupon/stores/0-a/avenue.php');
INSERT INTO `store_name` VALUES ('1-800-Contacts', NULL, 137, 0, '/coupon/stores/0-a/1800contacts.php');
INSERT INTO `store_name` VALUES ('American Blooms', NULL, 228, 1, '/coupon/stores/0-a/americanblooms.php');
INSERT INTO `store_name` VALUES ('1-800-Flowers.com', NULL, 122, 0, '/coupon/stores/0-a/1800flowers.php');
INSERT INTO `store_name` VALUES ('American Blinds', NULL, 225, 1, '/stores/?goldencansiteid=769&goldencancid=935551#goldencan');
INSERT INTO `store_name` VALUES ('Aplus.net', NULL, 217, 1, '/coupon/stores/0-a/aplus.php');
INSERT INTO `store_name` VALUES ('Active K9', NULL, 218, 1, '/stores/?goldencansiteid=1695&goldencancid=1098595#goldencan');
INSERT INTO `store_name` VALUES ('Adidas', NULL, 224, 1, '/coupon/stores/0-a/adidas.php');
INSERT INTO `store_name` VALUES ('Atlas Pen & Pencil', NULL, 223, 1, '/coupon/stores/0-a/apn.php');
INSERT INTO `store_name` VALUES ('Amazon.com', NULL, 222, 1, '/coupon/stores/0-a/amazon.php');
INSERT INTO `store_name` VALUES ('Altrec.com Outdoors', NULL, 216, 1, '/stores/?goldencansiteid=113&goldencancid=1066514#goldencan');
INSERT INTO `store_name` VALUES ('ACE Hardware', NULL, 215, 1, '/coupon/stores/0-a/acehardware.php');
INSERT INTO `store_name` VALUES ('BabyStyle', NULL, 214, 2, '/coupon/stores/b/babystyle.php');
INSERT INTO `store_name` VALUES ('Boscovs', NULL, 213, 2, '/coupon/stores/b/boscovs.php');
INSERT INTO `store_name` VALUES ('Beyond Blossoms', NULL, 212, 2, '/stores/?goldencansiteid=1455&goldencancid=1095094#goldencan');
INSERT INTO `store_name` VALUES ('BabyAge', NULL, 211, 2, '/coupon/stores/b/babyage.php');
INSERT INTO `store_name` VALUES ('Appetizertogo.com', NULL, 210, 1, '/coupon/stores/0-a/appetizerstogo.php');
INSERT INTO `store_name` VALUES ('Alle` Fine Jewelry', NULL, 209, 1, '/coupon/stores/0-a/alle.php');
INSERT INTO `store_name` VALUES ('Abt Electronics', NULL, 208, 1, '/coupon/stores/0-a/abtelectronics.php');
INSERT INTO `store_name` VALUES ('Apple iTunes', NULL, 207, 1, '/coupon/stores/0-a/appleitunes.php');
INSERT INTO `store_name` VALUES ('Alibris', NULL, 206, 1, '/coupon/stores/0-a/alibris.php');
INSERT INTO `store_name` VALUES ('A1 Wireless', NULL, 205, 1, '/coupon/stores/0-a/a1wireless.php');
INSERT INTO `store_name` VALUES ('6PM.com', NULL, 203, 0, '/stores/?goldencansiteid=466&goldencancid=107008#goldencan');
INSERT INTO `store_name` VALUES ('.Mac', NULL, 204, 0, '/coupon/stores/m-n/mac.php');
INSERT INTO `store_name` VALUES ('Axiom Home Theater', NULL, 229, 1, '/coupon/stores/0-a/axiom.php');
INSERT INTO `store_name` VALUES ('AKA Gourmet', NULL, 230, 1, '/coupon/stores/0-a/akagourmet.php');
INSERT INTO `store_name` VALUES ('AOL', NULL, 231, 1, '/coupon/stores/0-a/aol.php');
INSERT INTO `store_name` VALUES ('Alazing.com', NULL, 232, 1, '/coupon/stores/0-a/alazing.php');
INSERT INTO `store_name` VALUES ('Apple Store', NULL, 233, 1, '/coupon/stores/0-a/apple.php');
INSERT INTO `store_name` VALUES ('BabyUniverse', NULL, 243, 2, '/coupon/stores/b/babyuniverse.php');
INSERT INTO `store_name` VALUES ('Blinds.com', NULL, 241, 2, '/coupon/stores/b/blinds.php');
INSERT INTO `store_name` VALUES ('Botanic Choice', NULL, 242, 2, '/coupon/stores/b/botanicchoice.php');
INSERT INTO `store_name` VALUES ('Lingo', NULL, 384, 12, '/coupon/stores/j-l/lingo.php');
Please suggest