PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Sat Jan 29, 2005 3:10 am
Hi,
This code should produce a table, but it doesn't produce anything:
Code: Select all
<?
$cols = 4;
$arrayofitems = array('a','b','c','d','e','f','g','h','i');
$arrayofitems2 = array('description of a','description of b','description of c','description of d','description of e','description of f','description of g','description of h','description of i');
$rows = ceil(sizeof($arrayofitems) / $cols);
$counter = 0;
$content = '<table border=1 cellpadding=5>';
for($i = 0; $i < $rows; $i++) {
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($arrayofitemsї$counter])) {
$content .= '<td align="center"><strong>'.$arrayofitemsї$counter].'</strong></td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
$counter = $counter - $cols;
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($arrayofitemsї$counter])) {
$content .= '<td>Column '.$arrayofitems2ї$counter].'</td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
}
?>
The table it should produce:
Code: Select all
<table border="1" cellpadding="5">
<tr>
<td align="center">
<strong>a</strong>
</td>
<td align="center">
<strong>b</strong>
</td>
<td align="center">
<strong>c</strong>
</td>
<td align="center">
<strong>d</strong>
</td>
</tr>
<tr>
<td>
Column description of a
</td>
<td>
Column description of b
</td>
<td>
Column description of c
</td>
<td>
Column description of d
</td>
</tr>
<tr>
<td align="center">
<strong>e</strong>
</td>
<td align="center">
<strong>f</strong>
</td>
<td align="center">
<strong>g</strong>
</td>
<td align="center">
<strong>h</strong>
</td>
</tr>
<tr>
<td>
Column description of e
</td>
<td>
Column description of f
</td>
<td>
Column description of g
</td>
<td>
Column description of h
</td>
</tr>
<tr>
<td align="center">
<strong>i</strong>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
Column description of i
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
Last edited by
mhenke on Mon Jan 31, 2005 5:25 am, edited 3 times in total.
hunterhp
Forum Commoner
Posts: 46 Joined: Sat Jan 22, 2005 5:20 pm
Contact:
Post
by hunterhp » Sat Jan 29, 2005 4:20 am
Nothing is wrong, you just haven't echoed $content
echo "$content";
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Sat Jan 29, 2005 4:57 am
Ooops... guess I didn't have my lenses in ; )
THX!
hunterhp
Forum Commoner
Posts: 46 Joined: Sat Jan 22, 2005 5:20 pm
Contact:
Post
by hunterhp » Sat Jan 29, 2005 5:09 am
I didn't see it either, I was testing the code on my server for a while, changing different little things. Then I just noticed it. Pretty cool code though, I like simple and effective codes like that.
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Mon Jan 31, 2005 3:45 am
It is very cool, managed to do lots of fun things with it. Used it with file searches (files in dir using glob), but now I'm trying to display results from db, and for some reason can't get it to work : (
It only returns 1 username in the first column, instead of 4 colums filled with 4 different usernames... Whatever I try, the remaining columns are all filled with ' ' so it cannot find more results. What am I doing wrong?
Code: Select all
<?
dbuser();
$query = "SELECT username FROM users";
$result = mysql_query($query);
$prof = mysql_num_rows($result);
$cols = 4;
$rows = ceil($prof / $cols);
$counter = 0;
$content = '<table border=1 cellpadding=5>';
for($i = 0; $i < $rows; $i++) {
$row = mysql_fetch_row($result);
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($rowї$counter])) {
$content .= '<td align="center"><strong>'.$rowї$counter].'</strong></td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
$counter = $counter - $cols;
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($rowї$counter])) {
$content .= '<td>'.$rowї$counter].'</td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
}
echo "$content";
?>
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Mon Jan 31, 2005 5:01 am
SOLVED! Searched php.net again and found that I had to use mysql_result:
Code: Select all
<?
dbuser();
$query = "SELECT username FROM users ";
$result = mysql_query($query);
$prof = mysql_num_rows($result);
$cols = 4;
$rows = ceil($prof / $cols);
$counter = 0;
$content = '<table border=1 cellpadding=5>';
for($i = 0; $i < $rows; $i++) {
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
$row = mysql_result($result,$counter);
if(!empty($rowї$counter])) {
$content .= '<td align="center"><strong>'.$row.'</strong></td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
$counter = $counter - $cols;
$content .= '<tr>';
for($j = 0; $j < $cols; $j++) {
$link = mysql_result($result,$counter);
if(!empty($linkї$counter])) {
$content .= '<td>'.$link.'</td>';
}
else {
$content .= '<td> </td>';
}
$counter++;
}
$content .= '</tr>';
}
echo "$content";
?>
I would like to know if this code could be improved, as it says that using mysql_result is very slow. Eventually this code should work its way through a huge db...
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Mon Jan 31, 2005 7:10 am
you probably want to make a table a function.
this way, you only have to pass it an array with rows.. (doesn't matter if those come from glob or a mysql query... it just needs rows :p)
Code: Select all
$query = ".....";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
$rows = array();
while ($row = mysql_fetch_row($result))
{
$rowsї] = $row;
}
echo buildTable($rows);
function buildTable($datarows, $cols = 4)
{
// build your table here
return $table;
}
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Mon Jan 31, 2005 7:44 am
THX! Gonna make it work now : )
mhenke
Forum Newbie
Posts: 21 Joined: Thu Jan 27, 2005 2:20 am
Post
by mhenke » Mon Jan 31, 2005 9:30 am
Here's the full code, works like a charm! THX timvw!!!!! : )
Code: Select all
<?
dbuser();
$query = "SELECT username, country, city_1 FROM users ";
$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
$rows = array();
while ($row = mysql_fetch_row($result))
{
$rowsї] = $row;
}
echo buildTable($rows);
function buildTable($rows)
{
$cols = 3;
$numrows = ceil(count($rows) / $cols);
$counter = 0;
$table = '<table border=0 cellspacing=5>';
for($i = 0; $i < $numrows; $i++) {
$table .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($rowsї$counter])) {
$table .= '<td align="center" class="td2"><img src="../emb/b/'.$rowsї$counter]ї0].'_tn.jpg" border="0"></td>';
}
else {
$table .= '<td> </td>';
}
$counter++;
}
$table .= '</tr>';
$counter = $counter - $cols;
$table .= '<tr>';
for($j = 0; $j < $cols; $j++) {
if(!empty($rowsї$counter])) {
$table .= '<td align="center" class="td2">'.$rowsї$counter]ї1].'<br>'.$rowsї$counter]ї2].'</td>';
}
else {
$table .= '<td> </td>';
}
$counter++;
}
$table .= '</tr><tr><td><img src="../gfx/spacer.gif" height="5"></td></tr>';
}
$table .= '</table>';
return $table;
}
?>