0

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());
	}
?>
2
Contributors
3
Replies
8
Views
6 Years
Discussion Span
Last Post by Nahiyan
0
$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.

0

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?

0

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());
	}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.