I know how to print data from database on html table but i have one task that i can not understand, and i can not do it. I have a table in database and when i select those data i want them to display on html table but something like this:
+2
A:
To achieve a table layout like that you just need to condition the placement of the end of your table rows.
<table>
<tr>
<?php
$count = 0;
while($row = mysql_fetch_assoc($result)) :
$count++;
?>
<td><?php echo $row['value'] ?></td>
<?php if($count % 2 == 0 || $count == mysql_num_rows($res)) : ?>
</tr>
<?php if($count != mysql_num_rows($result)) : ?>
<tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>
What the above does is use the modulus operator (%
, calculates the remainder from division) to print a closing and opening row tag whenever we're at an evenly numbered result.
Also, if you wanted to change your table layout to be 3 or 4 columns wide all you need to do is change the number applied to the modulus:
$count % 3 == 0 //3 columns
$count % 4 == 0 //4 columns, etc
Cryo
2010-02-13 10:12:38
A:
<table>
<?php while (TRUE) { ?>
<?php
// Make an array of n rows. Trailing items may be FALSE if there
// are not enough rows to fill the table row.
//
$rows= array();
for ($i= 0; $i<n; $i++)
$rows[$i]= mysql_fetch_assoc($result);
if (!$row[0])
break;
?>
<tr>
<?php foreach ($rows as $row) { ?>
<td>
<?php if ($row) { ?>
Value: <?php echo(htmlspecialchars($row['value']); ?>
<?php } ?>
</td>
<?php } ?>
</tr>
<?php } ?>
</table>
bobince
2010-02-13 10:22:52
Nice example of spaghetti-coding
Jacco
2010-02-16 12:11:19
What are you talking about? This is a single consistent markup tree, which is the precise opposite of “spaghetti code”. You can see from the indentation exactly where each element and PHP structure begins and ends, which makes it much more maintainable than random `echo`s hidden inside a large block of PHP.
bobince
2010-02-16 13:28:15
I think it would be far more readable if it used less switching between parse and output mode. I read your 'proper PHP tutorial' (http://stackoverflow.com/questions/2119083/php-tutorial-that-is-security-accuracy-and-maintainability-conscious) question and I agree with your views except for one thing: teach people new to the language to use a templating system from the very beginning.
Jacco
2010-02-17 13:14:08
A:
Cryo:
I use your code this way:
<table>
<tr>
<?php
$count = 0;
while($rowfirstfour = mysql_fetch_assoc($firstfour)) :
$count++;
?>
<td>
<?
$html1 = '<?xml version="1.0" encoding="UTF-8"?>'.$rowfirstfour['artikullithumb'];
$elements1 = new SimpleXmlElement($html1);
$url1 = $elements1->img->attributes()->src;
?>
<img src="<?php echo $url1;?>"/></td>
<?php if($count % 2 == 0 || $count == mysql_num_rows($firstfour)) : ?>
</tr>
<?php if($count != mysql_num_rows($firstfour)) : ?>
<tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>
And i get this error:
Warning: main() [function.main]: Node no longer exists in /home6/feksinte/public_html/portali/index.php on line 260
What i am doing wrong here ??
AXheladini
2010-02-13 12:01:13