Avatar of thedeal56
thedeal56

asked on 

Creating Dynamic Table Display From PHP/MySQL/HTML

Hello.  I hope this is an easy one.  I want to display a list of names from a MySQL database.  The way I want to format the names is displayed in the attached pictures.  The first picture is showing the output of the attached code.  The second picture is showing my goal for the table display.

I know that my while loop is creating a table for each of the second row of names. How do I get around adding <table> tags inside my loop? How do I produce the output in the second picture? Thanks.
<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
?>
<table align=center width=900 height=50>
<?
while ($row = mysql_fetch_array($res)){
$i+=1;
if ($i <= 10) {
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
              } 
if ($i > 10 and $i <= 20) {
?>
</table>
<table align=center width=200 height=50>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<?
                          }
    
                                      }
?>
</table>

Open in new window

TableWrong.JPG
Correct.JPG
PHPMySQL ServerHTML

Avatar of undefined
Last Comment
thedeal56
Avatar of thedeal56
thedeal56

ASKER

I just wanted to add something.  Having two columns of the vertical table would be fine too.  It doesn't necessarily have to be two horizontal rows of output.    
Avatar of FrivolousSam
FrivolousSam

This code has the advantage of working with any number of rows, not just 10 or 20.
You might find that eventually you want to add blank cells so everything lines up if you get, say 19 results.  To do that, add the lines
for($j = 0; $j != 10 - ($i % 10); $j++) {
?>
 &nbsp;
<?
}
after the last "}" but before the "?>"

<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
?>
<table align=center width=900>
<tr height=50>
<?
$i=0;
while ($row = mysql_fetch_array($res)){
    $i+=1;
    if ($i <= 10) {
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
    } 
    if ($i % 10 == 0) { // i is 10 or 20 or 30 or ...
?>
</tr>
<tr height=50>
<?
    }
}
?>
</tr>
</table>

Open in new window

Avatar of MMDeveloper
MMDeveloper
Flag of United States of America image

try this

assuming nobody else has already posted an answer whilst I wrote this
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
 
$perRow = 10;
 
$tableStrings = array (
			'<table align="center" width="900" height="50">
				<tr>',
			"</tr></table>"
		);
$tdStrings = array (
		'<td width=900 align=middle bgcolor=blue> <font color=white><b>',
		'</b></font></td>'
	);
 
 
if(mysql_num_rows($res) > 0) {
	$index = 0;
	echo $tableStrings[0];
	while ($row = mysql_fetch_array($res)){
		
		
		echo $tdStrings[0];
		
		echo $row['First'];
		
		echo $tdStrings[1];
		
		
		if (($perRow % $index) == 0) {
			$index = 0;
			echo $tableStrings[1];
			echo $tableStrings[0];
		} else {}
		
		++$index;
	}
} else {}

Open in new window

Avatar of FrivolousSam
FrivolousSam

If you're looking to allow the rows to have different column widths, you will need separate tables, like this:
<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
?>
<table align=center width=900 height=50>
<?
$i=0;
while ($row = mysql_fetch_array($res)){
    $i+=1;
    if ($i <= 10) {
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
    } 
    if ($i % 10 == 0) { // i is 10 or 20 or 30 or ...
?>
</table>
<table align=center width=900 height=50>
<?
    }
}
?>
</table>

Open in new window

Avatar of thedeal56
thedeal56

ASKER

Wow! Thank you both very much for the fast response.  When I put in the code posted by FrivolousSam, I get one row of 10 results.  When I put in the code posted by MMDeveloper, I get one column of all the results.  Let me know if you want me to attach pictures of the results.  Is there anything else I can try?
Avatar of FrivolousSam
FrivolousSam

Sorry, I thought I'd taken out your IF statement.

Corrected below:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
 
	
 
<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
?>
<table align=center width=900 height=50>
<?
$i=0;
while ($row = mysql_fetch_array($res)){
    $i+=1;
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
    if ($i % 10 == 0) { // i is 10 or 20 or 30 or ...
?>
</table>
<table align=center width=900 height=50>
<?
    }
}
?>
</table>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of FrivolousSam
FrivolousSam

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of thedeal56
thedeal56

ASKER

That is awesome!  Would it be too much trouble for you to show me how it works with repeating columns (10 to a column) instead of rows?  If you don't have time, that's ok.  
Avatar of thedeal56
thedeal56

ASKER

Thank you very much.  If you ever get time to post how it works with columns instead of rows, that would be awesome.
Avatar of FrivolousSam
FrivolousSam

It's a bit more difficult with columns because HTML requires you to go across then down.

You'd have to read in all your data first, then display it after you're done:

<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
 
for ($i=0; $row = mysql_fetch_array($res); $i++){
    $htmlRows[$i % 10][]=$row['First'];
}
 
for($i=0; $i != sizeof($htmlRows); $i++)
{
?>
<table align=center width=900 height=50>
<?
    foreach($name in $htmlRows[$i])
    {
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
    }
?>
</table>
<?
}
?>

Open in new window

Avatar of FrivolousSam
FrivolousSam

Bear in mind that your solutions are not using good html.

All attributes should be enclosed in double-quotes and each table should have a  element enclosing all the  elements.
Avatar of thedeal56
thedeal56

ASKER

I am getting a parse error on line 17.  Yes, you're right.  I wrote that out pretty sloppy.
Avatar of FrivolousSam
FrivolousSam

sorry, it should be
foreach
(


$htmlRows
[
$i
]

 as $name

)

Open in new window

Avatar of thedeal56
thedeal56

ASKER

I'm sorry to keep bugging you with this, but here is a picture of what it looks like.
Result.JPG
Avatar of FrivolousSam
FrivolousSam

I see.

Two issues:
  1. On line 20 it should have been htmlRows[$i][$j] instead of $row['First']
  2. Needed to generate blanks again
<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
 
for ($i=0; $row = mysql_fetch_array($res); $i++){
    $htmlRows[$i % 10][]=$row['First'];
}
 
for($i=0; $i != sizeof($htmlRows); $i++)
{
?>
<table align=center width=900 height=50>
<?
    $j=0;
    for(; $j != sizeof($htmlRows[$i]); $j++)
    {
?>
<td width=900 align=middle bgcolor=blue> <font color=white><b><?echo $htmlRows[$i][$j];?></b></font></td>
<? 
    }
    for($k=0; $k != $j % 10; k++)
    {
?>
<td width=900 align=middle bgcolor=white> &nbsp; </td>
<? 
    }
?>
</table>
<?
}
?>

Open in new window

Avatar of thedeal56
thedeal56

ASKER

Cool. Thank you for rewriting it with the blanks built in.  It's giving this error on the blank-generating for loop, though:
Parse error: parse error, expecting `')''  on line 24

IF you get a chance today, would you mind looking over it?  Thanks.  
Avatar of thedeal56
thedeal56

ASKER

Ah, I changed it to:

for($k=0; $k != 10- ($j % 10); $k++)

it produces results, but is there an easy way to make the columns align evenly?
Avatar of thedeal56
thedeal56

ASKER

I didn't intend for the posts after the solution to carry on this long.  Should I make a new thread for the column alignment question?  
Avatar of thedeal56
thedeal56

ASKER

I messed around with it and figured it out.  I went back to the original solution's code.   Thanks a lot for all your help; I feel like I've learned something about for loops, which I haven't messed with up until now.  
<?
$database="Office";
mysql_connect ("localhost", "root", "");
@mysql_select_db($database) or die( "Unable to select database");
$result = "select First, Last from user order by Last";
$res=mysql_query($result) or die(mysql_error());
?>
 
<table width=50% align=center height=50>
<td>
<?
$i=0;
while ($row = mysql_fetch_array($res)){
    $i+=1;
?>
<td  height=50 align=middle bgcolor=blue> <font color=white><b><?echo $row['First'];?></b></font></td>
<? 
    if ($i % 5 == 0) { // i is 10 or 20 or 30 or ...
?>
</td>
<tr>
<td>
<?
    }
}
for($j = 0; $j != 10 - ($i % 10); $j++) {
?>
<td   bgcolor=white> &nbsp; </td>
<?
}
?>
</td>
</table>

Open in new window

Correct.JPG
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo