Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Script Partially Working

Status
Not open for further replies.

WalksWithSky

Instructor
Dec 11, 2002
49
CA
Hi:

Relatively new to PHP, but am developing an application to access an MySQL database. It's a training app, so I first have it displaying the courses, then when a use clicks on a course, it displays the modules listed in that course under the course name. Then what I would like it to do is when a user clicks on a module under the course name, it lists all the exercises in that module. This is the part that is not working! I thought I could use the same logic for calling the courses and listing the modules for the modules and exercises, but it's not working. Any help would be greatly appreciated!

Here's the code:

Code:
<?php

//connect to the database
$conn=mysql_connect("localhost", "root", "")
	or die(mysql_error());
mysql_select_db("safetytrain",$conn) or die(mysql_error());

$display_block = "<h1>Courses:</h1>
<p>Select a course to view its lessons.</p>";

//show courses first
$get_courses="select CourseID, CourseName, CourseDescription from tbl_Courses order by CourseName";
$get_courses_res = mysql_query($get_courses) or die(mysql_error());

if(mysql_num_rows($get_courses_res)<1) {
	$display_block.="<p><em>Sorry, there are currently no courses available.</em></p>";
} else {

while ($courses = mysql_fetch_array($get_courses_res)) {
	$CourseID = $courses[CourseID];
	$CourseName = strtoupper(stripslashes($courses[CourseName]));
	$Coursedescription = stripslashes($courses[CourseDescription]);
	
	
	$display_block.="<p><strong><a href=\"$_SERVER[PHP_SELF]?CourseID=$CourseID\">$CourseName</a></strong>
	<br>$Coursedescription</p>";
	
	if($_GET[CourseID] == $CourseID){
		//get lessons
		$get_lessons = "SELECT ExerciseID, CourseID, ExerciseTitle FROM tbl_Exercises WHERE CourseID = $CourseID";
		$get_lessons_res=mysql_query($get_lessons) or die(mysql_error());
		
		if(mysql_num_rows($get_lessons_res)<1) {
			$display_block .="<p><em>There are no lessons in this course at this time.</em></p>";
			} else {
			
				$display_block .="<ul>";
				
				while($lessons=mysql_fetch_array($get_lessons_res)){
					$ExerciseID=$lessons[ExerciseID];
					$ExerciseTitle=stripslashes($lessons[ExerciseTitle]);
					
					$display_block.="<li><a href=\"$_SERVER[PHP_SELF]?CourseID=$CourseID?ExerciseID=$ExerciseID\">$ExerciseTitle</a></li>";
				}
				$display_block .="</ul>";
				if($_GET[ExerciseID] == $ExerciseID){
		//get exercises
		$get_exercises = "SELECT ExerciseStepID, ExerciseID, ExerciseStepNumber, ExerciseStepText FROM tbl_ExerciseSteps WHERE ExerciseID = $ExerciseID";
		$get_exercises_res=mysql_query($get_exercises) or die(mysql_error());
		
		if(mysql_num_rows($get_exercises_res)<1) {
			$display_block .="<p><em>There are no exercises in this lesson.</em></p>";
			} else {
			
				$display_block .="<ul>";
				
				while($exercises=mysql_fetch_array($get_exercises_res)){
					$ExerciseStepID=$exercises[ExerciseStepID];
					$ExerciseID=$Exercises[ExerciseID];
					$ExerciseStepNumber=stripslashes($exercises[ExerciseStepNumber]);
					$ExerciseStepText=stripslashes($exercises[ExerciseStepText]);
					
					$display_block .="<li><a href=\"showexercise.php?ExerciseStepID=$ExerciseStepID\">$ExerciseStepText</a></li>";
				}
				$display_block .="</ul>";
			}
		}

			}
		}
			


		}
		}
		?>
		<html>
		<head>
		</head>
		<body>
		<?php echo $display_block;?>

		</body>
		</html>
 
COuld you maybe elaborate a bit what "does not work" means?
Are there any error messages? What is the result - no display?
Please help us with the details so we can help you.
 
Do you get any errors? What make you think it's not working?

I have a few suggestions on your coding style:

Why are you doing
Code:
if(mysql_num_rows($get_courses_res)<1)
when
Code:
if(mysql_num_rows($get_courses_res) == 0)
is much clearer.

When you are retrieving your information from the database use [red]mysql_fetch_assoc()[/red], since you are only using the column names for retieving the data. When you use mysql_fetch_array(), you actually get an array with two entries per column, since it generates an array with both numerical and text indices.

When you reference an array with a string (not the contents of a variable) always eclose the string in quotes. For example, you have:
Code:
$ExerciseID=$lessons[ExerciseID];
This should be written as:
Code:
$ExerciseID=$lessons[[b][red]'[/red][/b]ExerciseID[b][red]'[/red][/b]];
If you had all warnings turned on, you would have seen warnings with your original code.

Ken
 
Hey:

Thanks for the responses. Basically, what happens is when the page is opened in the browser, the course names and course descriptions are listed. When you click on a course name, the lessons in that course display underneath. When you click on a lesson name, the lesson names under the course disappear and nothing displays under the course. What I was hoping the code would do is display the exercises for that lesson under the lesson name. It works for displaying the lessons when you click on a code name, so I thought the same script would work if I drilled it down further.

Ken, thanks for the helpful tips on the coding style! Will change them, as your suggestions seem to read easier.
 
Try to echo out the query which is generated by PHP and paste it in your SQL, check if it retrieves any rows.

It's hard to guess what's happening, but I guess it might be some variables beeing over-written.

Olav Alexander Mjelde
Admin & Webmaster
 
Hey, DaButcher:

Thanks for the suggestion. Tried what you suggested and the query results display in MySQL. I also tried msking the query static in the PHP page (i.e., setting the criteria to 1 or 2) and nothing happened in the PHP page. It just doesn't display anything when you click on the lesson in the PHP page -- nothing happens. It doesn't write anything further to $display_block past the exercise titles -- the script seems to just stop working and nothing more is showing up in the HTML code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top