Jump to content

Dynamic Table using PHP..


Colton.Wagner

Recommended Posts

I am trying to generate the table below by pulling the information out of the database. The only issue I am having is the <tr> (Row) should have two <td> (Columns) inside it.

 

<table>
<tr>
<td>
Column #1 Row #1
</td>
<td>
Column #2 Row #1
</td>
</tr>
<tr>
<td>
Column #1 Row #2
</td>
<td>
Column #2 Row #2
</td>
</tr>
</table>

 

Hopefully that makes sense here is the code I have so far:

 

<?php

		$query = mysql_query("SELECT * FROM Doctoral_Biography ORDER BY Last_Name");

			while($row = mysql_fetch_array($query)){
				echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>";
				echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>";
			}

		?>

 

Any help would be greatly appreciated. I thought about adding either a ternary operator or a for loop. Please let me know what the most efficient option would be. Thanks in advanced!

Link to comment
Share on other sites

If i understand correctly, something like this should work:

<?php
$count = 1;
echo '<tr>';
while($row = mysql_fetch_array($query)){	
echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>";
echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>";

if ($count % 2 == 0) {
	echo '</tr><tr>';
}

$count++;
}

Link to comment
Share on other sites

Is there a problem with just adding the table rows into your current loop?

 

echo '<table>';

while($row = mysql_fetch_array($query)){
echo '<tr>';
echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>";
echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>";
echo '</tr>';
}
echo '</table>';

Link to comment
Share on other sites

If i understand correctly, something like this should work:

<?php
$count = 1;
echo '<tr>';
while($row = mysql_fetch_array($query)){	
echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>";
echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>";

if ($count % 2 == 0) {
	echo '</tr><tr>';
}

$count++;
}

 

After I posted this I found a solution myself. It's your code almost exactly but a little bit more messy. Thank you very much for your assistance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.