I'll try to explain this as best as I can. I have a table with 100 values, two fields: ID and Name. For the purposes of this example, ID isn't important.
What I would like to do is select all records and display them in five columns but I need them to be in alphabetical order from top to down:
Example: Table Data has the following:
AAAA
AAAB
AAAC
AAAD
AAAE
AAAF
I need to display the data like the following:
AAAA AAAD
AAAB AAAE
AAAC AAAF
Does that make sense?
If that isn't possible then I'll take it: AAAA AAAB AAAC AAAD AAAE AAAF
Either way, whichever is easiest.
Ok, Here is the error I'm getting now:
[Mon Dec 02 22:55:19 2013] [error] [client xx.xx.xx.xx] PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /usr/home/user/www/data/test/testing.php on line 35
[Mon Dec 02 22:55:19 2013] [error] [client xx.xx.xx.xx] PHP Notice: Undefined variable: result in /usr/home/user/www/data/test/testing.php on line 42
Here is the relevant code:
<?php
$username = "user";
$password = "password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("equine",$dbhandle)
or die("Could not select Database");
$query = 'select * from breeds';
while ($row = mysql_fetch_assoc($query)) {
$result[] = $row;
}
?>
<table><tr><td>
<?php
for ($i = 0; $i < count($result); $i++) {
if ($i % (count($result) / 5) == 0) {
echo "</td><td>";
}
echo "$result <br />";
}
?>
</td></tr></table>
Ok here is the error now, and I made sure I cut and pasted what you had directly....
[Tue Dec 03 11:43:12 2013] [error] [client xx.xx.xx.xx] PHP Parse error: syntax error, unexpected '[',
expecting ']' in /usr/home/user/www/data/test/testing.php on line 45
Well I got a little further, this is the output:
Array[name]Array[name]Array[name]Array[name]Array[name]Array[name]Array[name]
Ok I got it figured out, I'll post my code in a few minutes, I need to clean it up some.
Well I thought I had it figured out. Its printing but its not limiting itself to 5 data elements per row.
Here is the code:
<?php
$username = "username";
$password = "password";
$hostname = "localhost";
//connection to the database
$link = mysqli_connect($hostname, $username, $password,"equine")
or die("Error " . mysqli_error($link));
// connect to database, etc ...
$query = "SELECT name FROM breeds" or die("Error in the consult..." . mysqli_error($link));
$result = $link->query($query);
while($row = mysqli_fetch_array($result)) {
// echo $row["name"] . "<br>";
$myResult[] = $row;
}
?>
<table border="1"><tr><td>
<?php
for ($i = 0; $i < count($myResult); $i++) {
if ($i % (count($myResult) / 5) == 0) {
echo "</td><td>";
}
echo $myResult[$i]['name']. "<br />";
}
?>
</td></tr></table>
However the output looks like this:
<table border="1"><tr><td>
</td><td>Brittinee<br /></td><td>Pit Bull<br /></td><td>Black Lab<br /></td><td>Mutt<br /></td><td>Great Dane<br /></td><td>Doberman<br /></td><td>Shepard<br /></td></tr></table>
Except the data elements are in a straight row as it should be, I know it doesn't display right here.