Re: results from same mySQL query in multiple table columns
| From: | David Robley | Date: | Mon, 28 Aug 2000 03:02:25 +0000 |
| Subject: | Re: results from same mySQL query in multiple table columns | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-13892@lists.php.net to get a copy of this message | ||
On 27 Aug, chk wrote:
> My first post to the list and newbie.
>
> I'm trying to nicely format in 6 columns the result of a mySQL query.
>
> My table will always have less than 72 names but right now it has 59 names.
>
> When I apply the script below it chops off a name on the first column if I have less
> than 60 names in the table and formats ok if I have 60.
>
> Does anyone have a nice script that will count the number of items and format them
> across multiple columns with out missing any?
>
> Thank you.
>
> Chuck
>
>
> ___________________________________________________
>
> <php
>
> $sql = "SELECT fname FROM people ORDER BY fname ASC";
>
> $sql_result = mysql_query($sql,$connection);
>
>
> echo "<table width=625 border=0 align=center>
> <tr>
> <th>column1</th>
> <th>column2</th>
> <th>column3</th>
> <th>column4</th>
> <th>column5</th>
> <th>column6</th>
> </tr>";
>
>
> $all_fname = mysql_fetch_array($sql_result);
>
> while ($column1 = array_slice($all_fname, 0, 11))["column1"]; {
>
> $column2 = array_slice($all_fname, 12, 23)["column2"],
>
> $column3 = array_slice($all_fname, 24, 35)["column3"],
>
> $column4 = array_slice($all_fname, 36, 47)["column4"],
>
> $column5 = array_slice($all_fname, 48, 59)["column5"],
>
> $column6 = array_slice($all_fname, 60, 71)["column6"];
>
> echo "<tr>
> <td><input type=radio name=$fname value=$fname>$column1</td>
> <td><input type=radio name=$fname value=$fname>$column2</td>
> <td><input type=radio name=$fname value=$fname>$column3</td>
> <td><input type=radio name=$fname value=$fname>$column4</td>
> <td><input type=radio name=$fname value=$fname>$column5</td>
> <td><input type=radio name=$fname value=$fname>$column6</td>
> </tr>
> }";
>
> echo "</TABLE>";
>
> // free resources and close connection
> mysql_free_result($sql_result);
> mysql_close($connection);
>
> ?>
While, each and modulo are probably the tools you want here.
<php
$sql = "SELECT fname FROM people ORDER BY fname ASC";
$sql_result = mysql_query($sql,$connection);
?>
<table width=625 border=0 align=center><tr>
<th>column1</th><th>column2</th><th>column3</th>
<th>column4</th><th>column5</th><th>column6</th></tr>
<?php
$num_cols = 6; /* How many columns in the table? */
$all_fname = mysql_fetch_array($sql_result);
echo '<TR>';
$tmp = 1; /* keep track of nunmber of columns */
while($row = mysql_fetch_array($sql_result)) {
$fname = $row["fname"];
echo "<td><input type=radio name=$fname value=$fname>$fname</td>";
if( ($tmp % $num_cols) == 0){ /* If multiple of 6 close row */
echo '</TR><TR>';
}
$tmp++;
}
// Close TR and table; there'll be a case where you'll get a
//spurious </TR> but you can fix that :-)
?>
<?TR></TABLE>
That should get you close to where you want to be.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA