Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help to display 10 each column 1

Status
Not open for further replies.

indiantown

Technical User
Apr 29, 2005
25
US
<?

//Now grab a section of that
$db->open(

"SELECT songlist.title, songlist.id, songlist.count_played, songlist.filename, songlist.artist, songlist.album, songlist.duration FROM songlist, category, categorylist WHERE category.name = 'Top 10' AND songlist.id = categorylist.songID AND category.id = categorylist.categoryID ORDER BY songlist.count_played DESC, songlist.date_played ASC LIMIT 20");

$first = $start+1;
$last = min($cnt,$start+$limit);
$rc = $start;



function PutSongRow($song)
{
global $rc, $start, $darkrow, $lightrow;

$rc++;
$bgcolor = $darkrow;
if(($rc % 2)==0) $bgcolor = $lightrow;


PrepareSong($song);
?>

<font size="2" color="#003366"><small><? echo "$rc"; ?>.
<a href="javascript:songinfo(<? echo $song["songid"]; ?>)"><? echo $song["artist"]; ?></a></small></font> <br>
<font size="2" color="#003366"><small>&nbsp;&nbsp;&nbsp;&nbsp;<? echo $song["title"]; ?></small></font><br>



<?
}//PutSongRow

/* ## ===================================================================== ## */
?>


<table width="100%" bgcolor="<? echo $lightrow; ?>" border="0" cellspacing="0" cellpadding="5">
<tr bgcolor="<? echo $darkrow; ?>"><td height="14" colspan="2" class="rightbanner" nowrap>&nbsp;&nbsp;Top 10 Countdown</td></tr>
<tr><td nowrap>

<?
while($song = $db->row())
PutSongRow($song);
?>

</table>


Hi All,

The above script is what I am using to display songs. At present all 20 show alphabetically in the format

1
2
3
4
/
/
/
/
/
/
20

But I want to display in the format


1 11
2 12
/
/
/
10 20

Please help me I am not able to make it happen.

TIA.
 
in pseudo code:

Code:
start db read row by row
read each row into an element of an array
so you have $row[0]...$row[19]
echo out like this:

$i=0;
while ($i<10):
<tr><td>1</td><td>$row[$i]<\td><td>11</td><td>$row[$i+10]</td></tr>
$i++;
endwhile;
 
jpadie....thanks for your reply. Working still (its 2:00 in the morning.. uuh)

this is what i tried

//show stores first
$q = "select store_id, store_title from store_name order by store_title";

$r = mysql_query($q);
$n=mysql_num_rows($r);

#table tag
echo "<table align=center border=2 bgcolor=\"#77ffff\">";
for ($i=0;$i<10;$i++)

{
#$row is next row--in an array;
$row=mysql_fetch_row($r);
#dump id and store names
echo "<tr>";
echo "<td>";
print $row[0];
echo "</td>";
echo "<td>";
print $row[1]."<br>";
echo "</td>";
echo "<td>\n";
}
echo "</tr>\n";
echo "</table>";

It shows ten rows, but i am still not able to loop to show next 10 in another colum.

Appreciate if someone can take time to guide me exactly what and where i should make changes to get the result.

TIA
 
If I understand correctly, you need something like this:

Code:
// queries...
$resultset = array();
    while ($row = mysql_fetch_array($r)) {
		$resultset[] = $row;    
	}
    echo '<table border="1">';
	for ($i=0;$i<10;$i++){
		//second column index
		$c2 = $i+10;
   	 	echo '<tr>';
		echo "<td>{$resultset[$i][0]}:{$resultset[$i][1]}</td>";
		echo "<td>{$resultset[$c2][0]}:{$resultset[$c2][1]}</td>";
    	echo'<tr>';
	}
	echo '</table>';
 
Hi LZ,

A step forward.... here is the working script

//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);
echo "<TABLE BORDER=\"0\">\n";

//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result);

$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";
}
echo "<TD><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";

Now I got the columns to display....

How can I now group by Alphabets.... for ex. A in one group , B in another...

Please help.
 
what column are you trying to sort on? can't you include the sort order in your sql epression?
 
Hi jpadie,

I have around 400 entries and what I intend to do is list/group it according to letters.

So, all my entries with Letter A will be grouped together and B....

A
Apple,Ant.....

B
Boy,Ball....

If you help me with exactly how by changing SQL I can achieve will really help me out...

TIA.
 
aha...

add the following to the where clause " [field] like 'A%'
 
but then you'd have to run the query once for each letter of the alphabet. which isn't too bad but isn't necessary either.

instead let's try reading the whole recordset into an array and then retrieve from the array in the order that we want.

this code differs in style to the one you used previously -but follows that which i posted. it also displays the order in row then column rather than contrariwise:

ie.
1 2
3 4

rather than
1 3
2 4


Code:
<?
//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();

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>
		&nbsp;
	</td>
</tr>";
			endif;
		endforeach
		$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;
?>
 
LOTS of parse errors in that code. sorry - didn't check my typing. use code below
Code:
<?
//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>
		&nbsp;
	</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;
?>
 
Hi jpadie,

Thanks for your time in writing the code. Its still not working... I am trying to debug.... don't know till when I would be awake trying to get this work.

If you can also look into it please that why it still not working, I would be really tahnkful.

TIA.

 
its working at my end. can you supply some data for me to try with.
 
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>
&nbsp;
</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
 
thanks. this makes it easier to debug.

i've re-written the code to make it more accessible (and perhaps more rapidly executing).

please cut and paste directly and then add in the database connect parameters.
Code:
<?
//show stores first
error_reporting(E_ALL);
$host = ""; db host
$uname = "" ;//db username
$pwd = ""; //db password
$db = ""; //database 
mysql_connect($host, $uname, $pwd) or die(mysql_error());;
mysql_select_db($db) or die(mysql_error());

$query = "
		select  
			store_title, 
			store_link  
		from 
			store_name  
		order by 
			store_title ASC";

$result = mysql_query($query) or die(mysql_error());


//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
$start_letter = "";
$cnt = 0;
$table = "<table border=\"1\">";
while ($row  = mysql_fetch_assoc($result)):
	$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
	$link = "<a href=\"".$row['store_link']."\">".$row['store_title']."</a>";
	if ($start_letter != strtoupper(substr($row['store_title'],0,1))):
		//newline
		if (!$even && $cnt> 0):
			$table .= "<td>&nbsp;</td></tr>"; //add another column if the col count is not even
		endif;
		
		$start_letter = strtoupper(substr($row['store_title'],0,1));
		
		$table .= "<tr><td colspan=\"2\">&nbsp;</td></tr>"; //blank line
		$table .= "<tr><td colspan=\"2\">Letter: $start_letter</td></tr>"; //letter heading
		$table .= "<tr><td>$link</td>";  //nb we know this is even
		$cnt = 1;	//reset the counter
	else:
		//need to know whether this is even
		if ($even):
			$table .= "<tr><td>$link</td>";  
		else:
			$table .= "<td>$link</td></tr>";  
		endif;
		$cnt ++;
	endif;
	
endwhile;
//check for a trailing column
$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
if (!$even):
	$table .= "<td>&nbsp;</td></tr>";
endif;
$table .= "</table>";
echo $table;
?>
 
Hi jpadie,

The code shows a blank page..... looks some bug in the code somewhere....

going to look more into it.

Still doesn't work.

Any ideas.... will keep you posted.

regards

 
Hi jpadie,

Debugged.....

made it to work....

It was

mysql_connect($host, $uname, $pwd) or die(mysql_error());;

;; was the culprit.


How can I club


Letter: .
.Mac

Letter: 1
1-800-Contacts 1-800-Flowers.com
1-800-Patches 1-800-PetMeds
1-800-WheelChair 123Inkjets.com

Letter: 3
3balls.com

Letter: 6
6PM.com

under a catergory 0-9. Please suggest.
 
Hi jpadie,

one more thing...... how I can make it to display 3 columns rather than in only 2 columns

TIA
 
Hi jpadie,

I have got things working (limping).... trying to dress it up;

Cannot get few things done

1. Put stores with name 0-9 in one category
2. Trying to <table> </table> in letter category
3. Show the stores in 3 columns

Here is the modified script;

//this section stores all the rows into an array
$start_letter = "";
$cnt = 0;

$table = "<table border=\"0\">";

while ($row = mysql_fetch_assoc($result)):
$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
$link = "<a href=\"".$row['store_link']."\">".$row['store_title']."</a>";
if ($start_letter != strtoupper(substr($row['store_title'],0,1))):
//newline
if (!$even && $cnt> 0):
$table .= "<td width=\"35%\">&nbsp;</td></tr></table><a name=\"$start_letter\"><br> <br></a>"; //add another column if the col count is not even
endif;

$start_letter = strtoupper(substr($row['store_title'],0,1));

$table .= "<a name=\"$start_letter\"></a><table class=\"list_all_table\" cellpadding=\"3\" style=\"width: 80%; background-color: rgb(102, 204, 255);\" border=\"0\"><tr><td width=\"35%\">&nbsp;</td></tr>"; //blank line
$table .= "<tr style=\"font-weight: bold;\"><td class=\"deal_detail_title\" colspan=\"2\">Letter: $start_letter</td></tr>"; //letter heading
$table .= "<tr><td width=\"35%\">$link</td>"; //nb we know this is even
$cnt = 1; //reset the counter
else:
//need to know whether this is even
if ($even):
$table .= "<tr><td width=\"35%\">$link</td>";
else:
$table .= "<td width=\"35%\">$link</td></tr>";
endif;
$cnt ++;
endif;

endwhile;
//check for a trailing column
$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
if (!$even):
$table .= "<td width=\"35%\">&nbsp;</td></tr>";
endif;
$table .= "</table>";
echo $table;


Problem:

1. Not able to close </table> for the stores with even count (the loop)

Appreciate if any suggestion how to get this done.

TIA.
 
a few things
1. post your code between
Code:
 tags to make it easier to read and less susceptible to stripping.  
2. the <table> tag is closed whether or not there is a trailing column as the relevant line is outside the if statement.
3. the included code handles the separation of numbers from letters.  i have not had time to show you the three column method.  essentially you have to abandon the even-odd test and instead create a counter from 1-3 and test the value each loop.

[code]
<?
//show stores first
error_reporting(E_ALL);
mysql_connect("localhost", "root", "root") or die(mysql_error());;
mysql_select_db("test") or die(mysql_error());

$query = "
		select  
			store_title, 
			store_link  
		from 
			store_name  
		order by 
			store_title ASC";

$result = mysql_query($query) or die(mysql_error());


//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
$start_letter = "";
$cnt = 0;
$table = "<table border=\"1\">";
while ($row  = mysql_fetch_assoc($result)):
	$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
	$link = "<a href=\"".$row['store_link']."\">".$row['store_title']."</a>";
	$first_letter = strtoupper(substr($row['store_title'],0,1));
	$ord = ord($first_letter);
	if (
			($start_letter != $first_letter) //change in start letter
			&& 
			(
				($ord<91) 
				&& 
				($ord>64)
			)
		):
		//we know that there has been a change in letter and not a number 
		//newline
		
			if (!$even && $cnt> 0):
				$table .= "<td>&nbsp;</td></tr>"; //add another column if the col count is not even
			endif;
			
			$start_letter = $first_letter;
			
			$table .= "<tr><td colspan=\"2\">&nbsp;</td></tr>"; //blank line
			$table .= "<tr><td colspan=\"2\">Letter: $start_letter.</td></tr>"; //letter heading
			$table .= "<tr><td>$link</td>";  //nb we know this is even
			$cnt = 1;	//reset the counter
			
	else:
		//need to know whether this is even
		if ($even):
			$table .= "<tr><td>$link</td>";  
		else:
			$table .= "<td>$link</td></tr>";  
		endif;
		$cnt ++;
	endif;
	
endwhile;
//check for a trailing column
$even = (($cnt - (2 * floor($cnt/2))) == 0) ? true : false;
if (!$even):
	$table .= "<td>&nbsp;</td></tr>";
endif;
$table .= "</table>";
echo $table;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top