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
https://forums.phpfreaks.com/topic/103619-a-to-z-with-4-boxes-per-row/
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>';

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>';
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.