Okay, after scouring the net and doing some searching, I found some code that allowed me to display output from a table into multiple columns that I set. This resulted in a horizontal output.
Example: I have 26 rows in the table and each are assinged a letter in the alphabet. I assign 5 columns and get the following output:
A B C D E
F G H I J
.. .. .. .. etc
So, I then tried to code in to give me vertical output which would look like:
A F .. .. ..
B G .. .. ..
C H .. .. ..
D I .. .. ..
E J .. .. ..
I am having trouble with the second part.
Since others may want to do something similar, the code for horizontal output is here. My output is the "surname" that is linked by their "email" address.
<?
$columns=5;
$dbh=@mysql_connect ("$dbhost", "$dbuser", "$dbpass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbname");
$qrySurname__letter = "%";
// Output letters as links: (Ascii range from 65 to 90 are letters A to Z!)
// Trying to do this automatically without having to resort to hand coding // of every single link
for($x=65;$x<=90;$x++)
{
if ($x==ord($subsel)) echo "<b>".chr($x)."</b>";
else
echo "<a href=\"surnametest2.html?letter=".chr($x)."\">".chr($x)."</a> ";
}
$letter = $_GET["letter"];
if (isset($letter))
{
$qrySurname = "SELECT * FROM surnames WHERE nm_surname LIKE '$letter%' ORDER BY nm_surname";
} else {
$qrySurname = "SELECT * FROM surnames ORDER BY nm_surname";
}
$result = mysql_query($qrySurname);
//Need to know the number of rows, so use this line
$num_rows=mysql_num_rows($result);
echo "<TABLE BORDER=1 cellpadding=5 align=center>\n";
//Use a for loop so that the number of rwos can be used
for ($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_array($result);
if ($i % $columns == 0) {
//If there is no remainder, start a new row
echo "<tr>";
}
echo "<td> <A HREF=\"mailto:{$row['nm_email']}\">{$row['nm_surname']}</a></td>";
if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
//If there is a remainder of 1, end row or if there is nothing left in the result set, end the row
echo "</tr>";
}
}
echo "</table>";
?>
Okay, the above works great, now to try and get vertical listings. THe problem is, it looks like a $data[]= is made but only uses the data from one field. So when it outputs (echoes the <td> field) it only outputs that single field and works fine. However, my output needs to be along the lines of
echo "<td> <A HREF=\"mailto:{$row['nm_email']}\">{$row['nm_surname']}</a></td>";
instead of
echo "<td>".$data[$i + ($j * $rows)]."</td>";
I have tried numerous ways, but I think my problem may be in understanding the array function and calls in this second piece of code. Here is the code:
<?
$columns=5;
$dbh=@mysql_connect ("$dbhost", "$dbuser", "$dbpass") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("$dbname");
$qrySurname__letter = "%";
// Output letters as links: (Ascii range from 65 to 90 are letters A to Z!)
// Trying to do this automatically without having to resort to hand coding // of every single link
for($x=65;$x<=90;$x++)
{
if ($x==ord($subsel)) echo "<b>".chr($x)."</b>";
else
echo "<a href=\"surnametest3.html?letter=".chr($x)."\">".chr($x)."</a> ";
}
$letter = $_GET["letter"];
if (isset($letter))
{
$qrySurname = "SELECT * FROM surnames WHERE nm_surname LIKE '$letter%' ORDER BY nm_surname";
} else {
$qrySurname = "SELECT * FROM surnames ORDER BY nm_surname";
}
$result = mysql_query($qrySurname);
//Need to know the number of rows, so use this line
$num_rows=mysql_num_rows($result);
//Set a new variable called $rows
$rows = ceil($num_rows / $columns);
//For this vertical display, need to run another loop, which will populate an array with our values
while($row = mysql_fetch_array($result)) {
$data[] = $row['stuff']; //<NEED TO CHANGE THIS
}
echo "<TABLE BORDER=1 cellpadding=5 align=center>\n";
for ($i = 0; $i < $num_rows; $i++) {
echo "<tr>";
for ($j = 0; $j <$columns; $j++) {
if(isset($data[$i + ($j * $rows)])) {
echo "<td>".$data[$i + ($j * $rows)]."</td>";//<NEED TO CHANGE THIS
}
}
echo "</tr>";
}
echo "</table>";
?>
Any help would be greatly appreciated