Jump to content

Display multiple db results from query


wchamber22

Recommended Posts

Hello again freaks,

 

When testing I noticed that the code as provided below only displays the first db result from my query.  I need it to display all the db results.  I know I am missing a single key ingredient, but can't put my finger on it after trying and trying. Code below:

 

function coursesCode(){
if( !isset($_SESSION['user_id']) ){
	echo '<table width="716" cellpadding="3" cellspacing="0" border="0">
		  	<tr>
				<td width="250" bgcolor="#CCCCCC"><h4>COURSE NAME</h4></td>
				<td width="75" bgcolor="#CCCCCC"><h4>TEES</h4></td>
				<td width="100" bgcolor="#CCCCCC"><h4>YARDAGE</h4></td>
				<td width="75" bgcolor="#CCCCCC"><h4>RATING</h4></td>
				<td width="75" bgcolor="#CCCCCC"><h4>SLOPE</h4></td>
				<td bgcolor="#CCCCCC"><h4>CONDITION</h4></td>
		  	</tr>';

	$sql = mysql_query("SELECT courseID, courseName, courseTYRS FROM courses") or die(mysql_error());
	while($row = mysql_fetch_array($sql)){
	$course_id = $row["courseID"];
	$courseName = $row["courseName"];
	$courseTYRS = $row["courseTYRS"];

	echo '<tr>
			<td valign="top"><a href="coursedetails.php?courseID=' . $course_id . '"><strong>' . $courseName . '</strong></td>
			<td colspan="4">
				<table width="325" cellpadding="3" cellspacing="0" border="0">';

	$courseTYRSarray = explode(",", $courseTYRS);
	foreach ($courseTYRSarray as $key => $value){
		$courseTee_Yardage_Rating_Slope = explode("-", $value);
		$courseTee = $courseTee_Yardage_Rating_Slope[0];
		$courseYardage = $courseTee_Yardage_Rating_Slope[1];
		$courseRating = $courseTee_Yardage_Rating_Slope[2];
		$courseSlope = $courseTee_Yardage_Rating_Slope[3];

		echo '<tr>
				<td width="75">' . $courseTee . '</td>
				<td width="100">' . $courseYardage . '</td>
				<td width="75">' . $courseRating . '</td>
				<td width="75">' . $courseSlope . '</td>
			  </tr>';
	}

	$sql2 = mysql_query("SELECT date FROM course_conditions WHERE courseID='$course_id' ORDER BY date DESC LIMIT 1") or die(mysql_error());
	while($row2 = mysql_fetch_array($sql2)){
		$date = $row2["date"];
	}

	if(!$date){
		$dateOutput = '<img src="images/minus.png"> N/A';
	}else{
		$dateOutput = '<img src="images/checkmark.png"> ' . $date . '';	
	}

	echo '</table>
		  </td>
		  <td valign="top">' . $dateOutput . '</td>
		  </tr>
		  <tr>
			<td colspan="6"> </td>
		  </tr>';
	}
	echo '</table>';
}
}

 

As always thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/267208-display-multiple-db-results-from-query/
Share on other sites

How many rows should there be?

 

You have a lot of code going on there. Try reducing it a bit until you're sure everything works. What does this give you?

 

$sql = mysql_query("SELECT courseID, courseName, courseTYRS FROM courses") or die(mysql_error());

echo mysql_num_rows($sql) . ' rows returned';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.