Hello,

I wrote some code to make a table using HTML with <th> tags for displaying header in the first row followed by contents of mysql database. Due to while loop the header is displayed everytime the mysql_fetch_assoc fetches stuff from the database. However I want the header to be displayed only once and I want everything in 1 single table. Here is my php code:

<?php
	require 'connect.inc.php';
	require 'core.inc.php';
	
	$query = "SELECT `id`, `name`,`title` FROM `onwebed_pages` WHERE 1";
	if ($mysql_query = mysql_query($query)){
		while ($mysql_row = mysql_fetch_assoc($mysql_query)){
			echo "
			<table frame='border'  cellpadding='10' cellspacing='5'>
				<col align='left'</col>
				<col align='left'</col>
				<col align='left'</col>
				<tr>
					<th>id</th>
					<th>Name</th>
					<th>Title</th>
				</tr>
				<tr>
					<td>".$mysql_row['id']."</td>
					<td>".$mysql_row['name']."</td>
					<td>".$mysql_row['title']."</td>
				</tr>
			</table>";
		}
	}else{
		die(mysql_error());
	}
?>
Member Avatar for diafol
$query = "SELECT `id`, `name`,`title` FROM `onwebed_pages`";
	if ($mysql_query = mysql_query($query)){
?>
		<table frame='border'  cellpadding='10' cellspacing='5'>
				<col align='left'</col>
				<col align='left'</col>
				<col align='left'</col>
				<tr>
					<th>id</th>
					<th>Name</th>
					<th>Title</th>
				</tr>
<?php  
            while ($mysql_row = mysql_fetch_assoc($mysql_query)){
		echo "<tr><td>{$mysql_row['id']}</td><td>{$mysql_row['name']}</td><td>{$mysql_row['title']}</td></tr>";
		}
            echo "</table>";
	}else{
		die(mysql_error());
	}
?>

You can echo the first bit of html too if you want.

I already tried that method but I get a table which has a row showing headers along with another row which shows the stuff which is fetched by mysql_fetch_assoc();

The rest of the rows are not in the table which is totally unexpected. How to make all the rows get inside the table?

Thanks everyone but I already figured out how to achieve this by using the following code:

<?php
	require 'connect.inc.php';
	require 'core.inc.php';
	
	$query = "SELECT `id`, `name`,`title` FROM `onwebed_pages` WHERE 1";
	if ($mysql_query = mysql_query($query)){
	?>
		<?php
		while ($mysql_row = mysql_fetch_assoc($mysql_query)){
			$id = $mysql_row['id'];
			if ($id ==1){
				echo "
				<table border='1'>
					<col align='left'</col>
					<col align='left'</col>
					<col align='left'</col>
					<tr>
						<th>id</th>
						<th>Name</th>
						<th>Title</th>
					</tr>
					<tr>
						<td>".$mysql_row['id']."</td>
						<td>".$mysql_row['name']."</td>
						<td>".$mysql_row['title']."</td>
						<td>"."<a href='/$directory/edit.php?p=$id'>Edit</a>"."</td>
					</tr>
				";
			}else{
				echo "
				<tr>
						<td>".$mysql_row['id']."</td>
						<td>".$mysql_row['name']."</td>
						<td>".$mysql_row['title']."</td>
						<td>"."<a href='/$directory/edit.php?p=$id'>Edit</a>"."</td>
				</tr>
				</table>";
			}
		}
	}else{
		die(mysql_error());
	}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.