/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] New to php

hi guys!, I want to create a code that will dynamically fetch a rows of data in sql and display it on a 3 x 3 html table. i already found a code, the problem is every time the counter reach its limit it always print an opening and closing tags <tr></tr>. Is there a way to get rid of those tags? thanks guys. Here is an example code:

[CODE]<table><?php
$resultpackage = mysql_query(“SELECT * FROM package”,$db);
echo ‘<tr>’;
while ($package = mysql_fetch_array($resultpackage)) {
$counter2++;
echo ‘<td valign=”top”><h1>’.$package[‘name’].'</h1></p>. nl2br($package[‘description’]).'</p><p>’. nl2br($package[‘inclusion’]).'</p></td>’;
if ($counter2%2==0) {
echo ‘</tr><tr>’;
}}
echo ‘</tr>’;
?></table>[/CODE]

and it’s html output is this:

[CODE]
<table>
<tr>
<td>
<h1>Package Name</h1>
<p>Information</p>
<p>inclusion</p>
</td>

<td>
<h1>Package Name</h1>
<p>Information</p>
<p>inclusion</p>
</td>
</tr>

<tr>
<td>
<h1>Package Name</h1>
<p>Information</p>
<p>inclusion</p>
</td>
</tr><tr></tr></table>
[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyNov 12.2011 — Well initially I want to start by saying you should consider using CSS tables rather than the traditional <table> tag itself. As for your code itself, to me it certainly seems like an odd way of achieving your desired result. I would try something more along the lines of:
[code=php]
<table>
<?php
$resultpackage = mysql_query("SELECT * FROM package", $db);
while($package = mysql_fetch_array($resultpackage))
{
echo '<tr><td valign="top"><h1>' . $package['name'] . '</h1></td><td></p>' . nl2br($package['description']) . '</p></td><td><p>' . nl2br($package['inclusion']) . '</p></td></tr>';
}
?>
</table>
[/code]


However this would display the information horizontally per package(which is actually how it is of course stored in a database naturally) and allow for you to do something such as display column headers/labels, but it appears you are attempting to display each package vertically. If that seems to be necessary then I would try something like:
[code=php]
<table>
<?php
$pkg_names = array();
$pkg_desc = array();
$pkg_inc = array();
$table_count = 0;

$resultpackage = mysql_query("SELECT * FROM package", $db);
while($package = mysql_fetch_array($resultpackage)) {
$pkg_names = $package['name'];
$pkg_desc = $package['desc'];
$pkg_inc = $package['inclusion'];
$table_count++;
}

echo '<tr>';
for($i = 0; $i < $table_count; $i++) {
echo '<td>' . $pkg_names[$i] . '</td>';
}
echo '</tr><tr>';
for($j = 0; $j < $table_count; $j++) {
echo '<td>' . $pkg_desc[$j] . '</td>';
}
echo '</tr><tr>';
for($k = 0; $k < $table_count; $k++) {
echo '<td>' . $pkg_inc[$k] . '</td>';
}
echo '</tr>';
?>
</table>
[/code]


The second example starts to seem a big lengthy or overdoing it, however logically(for me) it made the most sense because of what you are seeming to want to do, which is display all of the information from multiple rows of one column on the same row(inverting the original format of your table in mysql).

Your original example actually doesn't display a 3x3 table at all, but a 1x3 table in which you simply separate each column's data with HTML text formatting tags. So my example literally creates a 3x3 table instead, which of course involved a few more steps given it conflicts with the way the table/data is naturally returned from mysql.

[EDIT]

I also wanted to add your original code uses a closing paragraph tag(</p>) after the closing header tag(</h1>) instead of an opening paragraph tag and after this an apostrophe( ' ) is missing which would mess up the html formatting as well as the PHP code execution. Though I took it all as purely example code that was created for this post and assumed your original code did not contain those two errors. If they exist in your actual code you could always fix them and try running it again if you prefer your way.

[/EDIT]

Hopefully one of the two examples helps you and feel free to ask for a conversion to a CSS table instead as well.
Copy linkTweet thisAlerts:
@rrfuertesauthorNov 13.2011 — hi Sup3rkirby! thank you for your reply, really appreciate it. i tried your code, but i've got a different result. anyway i already have a solution. i add another counter that prints </tr> if the counter is equal to total rows.

[code=php]
<?php
$db = mysql_connect("","","");
mysql_select_db("dbase1",$db);
?>
<html>
<body>
<table border="1">
<?php
echo '<tr>';
$resultpackage = mysql_query("SELECT * FROM package", $db);
$totalrows = mysql_num_rows($resultpackage);
while($package = mysql_fetch_array($resultpackage)) {
$test1++;
$test2++;
echo '<td><h1>' . $package['name'] . '</h1><p>' . $package['desc'] . '</p><p>' . $package['inclusion'] . '</p></td>';
if ($test2 == $totalrows) {
echo '</tr>';
}
elseif ($test1%3==0) {
echo '</tr><tr>';
}
}
?>
</table>
</body>
</html>
[/code]
and here's the html output:

[code=html]
<html>
<body>
<table border="1">
<tr><td><h1>name 1</h1><p>desc 1</p><p>inclusion 1</p></td><td><h1>name 2</h1><p>desc 2</p><p>inclusion 2</p></td><td><h1>name 3</h1><p>desc 3</p><p>inclusion 3</p></td></tr><tr><td><h1>name 4</h1><p>desc 4</p><p>inclusion 4</p></td><td><h1>name 5</h1><p>desc 5</p><p>inclusion 5</p></td><td><h1>name 6</h1><p>desc 6</p><p>inclusion 6</p></td></tr><tr><td><h1>name 7</h1><p>desc 7</p><p>inclusion 7</p></td><td><h1>name 8</h1><p>desc 8</p><p>inclusion 8</p></td><td><h1>name 9</h1><p>desc 9</p><p>inclusion 9</p></td></tr>
(finally get rid of the extra <tr>)
</table>
</body>
</html>
[/code]
I also attached a picture of the result of the code when you view it on a browser:

http://www.freeimagehosting.net/57af0

by the way, about the quotation mark, it was a typo. Thanks again!
×

Success!

Help @rrfuertes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...