0

Hi, im working on some project on php with mysql, and I'm only actually a beginner in programming with php and mysql.

I created a table in mysql which contains several records. I have like 10 records in it. Now what I want to do is I only want to display 4 records from that table.
Here is the code on the query part. I stored it in an associative array.

<?php
	$query_latestJobPost = "SELECT * FROM tbljobad ORDER BY postDate DESC";
	$latestJobPost = mysql_query($query_latestJobPost, $dbConnection) or die (mysql_error());
	$assoc_latestJobPost = mysql_fetch_assoc($latestJobPost);
	$numberRows_latestJobPost = mysql_num_rows($latestJobPost);
?>

What happens if I use this code below, it displays the whole record which is stored in the table, but what I want is only to display a limited number of records.

<table>
			<?php do { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php }while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)); ?> 
</table>

What will I do to arrive with that output?

3
Contributors
7
Replies
10
Views
7 Years
Discussion Span
Last Post by rajarajan07
0

try this:

<table>
			<?php while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)) { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php } ?> 
</table>

where $latestJobPost being assigned by the mysql_query() function.

0

try this:

<table>
			<?php while($assoc_latestJobPost = mysql_fetch_assoc($latestJobPost)) { ?>
			<tr>
				<td><?php echo $assoc_latestJobPost['jobTitle']; ?></td>
				<td><?php echo $assoc_latestJobPost['jobCategoryID']; ?></td>
				<td><?php echo $assoc_latestJobPost['postDate']; ?></td>
				<td><?php echo $assoc_latestJobPost['employerID']; ?></td>
			</tr>
			<?php } ?> 
</table>

where $latestJobPost being assigned by the mysql_query() function.

cwarn23, thanks for the reply, but still with the code that you have revised it still arrives with the same output, still the whole record is being displayed. But, I only want limited records to be displayed, like 4 records only.

1

To limit it to 4 records then in addition to what I said before do the following sql query as well.

$query_latestJobPost = "SELECT * FROM tbljobad ORDER BY postDate DESC LIMIT 4";
0

Thanks sir.. I didn't know it's on the sql query.. Not yet familiar with the LIMIT keyword.. anyway.. thanks a lot.. that was a lot of help..

0

It will retrieve Top 5 records from the query result, you can give Top 4 to retrieve top 4 records from the query result.

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.