Jump to content

a to z with 4 boxes per row...


brown2005

Recommended Posts

i have a table

 

topics

 

with

 

topics_topic

 

i want to create an a to z like (with the first letter of topics_topic) and excluding letters without results...

 

A

 

box 1  box 2  box 3  box 4

box 5  box 6

 

B

 

box 1  box 2  box 3

 

C

 

box 1  box 2  box 3  box 4

box 5  box 6

 

D

 

box 1  box 2  box 3  box 4

box 5 

 

where box is the results for each letter (topics_topic)

 

can anybody please help with how i can set this out please.

 

Link to comment
Share on other sites

This should get you started...

 

<?php

$query = "SELECT topics_topic FROM table ORDER BY topics_topic";
$result = mysql_query($query) or die(mysql_error());

// loop the result
foreach ($result as $row) {
// get the first letter
$first = strtolower(substr($row['topics_topic'], 0, 1));

// make sure the first letter is a letter
if (str_pos('abcdefghijklmnopqrstuvwxyz', $first) !== false) {
	// place it in the correct position in the array
	$data[$first][] = $row['topics_topic'];
}
}

// sort by keys
ksort($data);

// the number of columns you want
$num_columns = 4;

// start the table
echo '
<table>';

// loop through the results
foreach ($data as $letter => $topics) {
// display the row with the letter
echo '
	<tr>
		<th colspan="4">' . $letter . '</th>
	</tr>
	<tr>';

$i = 0;

// display the topic title in a td
foreach ($topics as $topic) {
	// if $num_columns tds have been echo'd start a new row
	if ($i == $num_columns) {
		echo '
	</tr>
	<tr>';

		$i = 0;

	}

	echo '
		<td>' . $topic . '</td>';

	$i++;

}

// finish off the current row 
while ($i < $num_columns) {
	echo '
		<td>nbsp;</td>';
	$i++;
}

echo '
	</tr>';
}

// finish off the table
echo '
</table>';

Link to comment
Share on other sites

In this example I assume that you read your topics from a database. You can change that to fit your setup

 

<php

....
$results = mysql_query("SELECT topic FROM mytable ORDER BY topic ASC")

$this_topic = '';
echo '<table>';
while ($one_row = mysql_fetch_array($results))
{
$topic        = $one_row['topic'];
$topic_header = ucwords(strmid($topic,0,1));
// check if we have a new topic
if ($topic_header != $this_topic)
{
	if ($this_topic != '') echo '</tr>';
	$this_topic = $topic_header;
	echo '<tr colspan=4><td>$this_topic</td></tr><tr>';
	$i = 0;
}
$i++;
// if 4 sub-topics already printed, close row and start a new row
if ($i == 5)
{
	echo '</tr><tr>';
	$i = 1;
}
echo "<td>$topic</td>";
}
echo '</tr></table>';
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.