PHP - problems with loop over array

Hi,

I’m just starting out with PHP and have come across a problem with a for loop I’m not able to fix myself.
I’m building a simple website for a research project where users are asked to rate videos on a scale from 1-5. The videos are stored in an array. Once the user has submitted their rating for a video, the page is reloaded with the next video from the array and a refreshed set of radio buttons. This should continue until all videos in the array have been processed. When there are no more videos to process, I’d like to continue to the next .php page with a different experimental setup (this is not implemented in the following code yet because I haven’t figured out how to do it yet).

This is what I have so far:

<?PHP
$one = 'unchecked';
$two = 'unchecked';
$three = 'unchecked';
$four = 'unchecked';
$five = 'unchecked';

$videos=array('data/test1.mov','data/test2.mov', 'data/test3.mov');
$pagenumber=0; //for when post is not set
$i=1;

for ($i=1;$i<=3;$i++) {

	if (isset($_POST['submit1'])) {

		$pagenumber=intval($_POST['pageid']); 
		$selected_radio = $_POST['part1'];

			if ($selected_radio == 'one') {
				$one = 'checked';
			}

			else if ($selected_radio == 'two') {
				$two = 'checked';
			}
			else if ($selected_radio == 'three') {
				$three = 'checked';
			}
			else if ($selected_radio == 'four') {
				$four = 'checked';
			}
			else if ($selected_radio == 'five') {
				$five = 'checked';
			}
	}
}

$nextpagenumber=intval($pagenumber)+1;//increment pageid for next page

echo "<embed src='{$videos[$pagenumber]}' width='400' height='480' controller='true'>";
?>

<FORM NAME ="evalpart1" METHOD ="POST" ACTION ="setup1.php">
<INPUT TYPE = 'Radio' Name ='part1'  value= 'one' <?PHP print $one; ?>>1
<INPUT TYPE = 'Radio' Name ='part1'  value= 'two' <?PHP print $two; ?>>2
<INPUT TYPE = 'Radio' Name ='part1'  value= 'three' <?PHP print $three; ?>>3
<INPUT TYPE = 'Radio' Name ='part1'  value= 'four' <?PHP print $four; ?>>4
<INPUT TYPE = 'Radio' Name ='part1'  value= 'five' <?PHP print $five; ?>>5
<P>
<INPUT TYPE = "hidden" Name = "pageid"  VALUE = "<?php echo $nextpagenumber; ?>"/>
<INPUT TYPE = "submit" Name = "submit1"  VALUE = "Submit">
</FORM>

I was hoping that the for loop would stop after 3 iterations but the page reloads with another set of radio buttons (albeit without a video). It would be fantastic if you could help me figure out what I have to do in order to make the loop stop and move on to another page after clicking the “submit” button once the whole video array has been processed.

I also tried to implement a foreach loop so that I can loop directly over the array instead of incrementing integers, but I didn’t understand the tutorials I found.

If I write

foreach($videos as $value){}

nothing changes, and I have the feeling I should put $value somewhere inside the loop as well, but I don’t know what to do with it.

I’d appreciate if you could point me into the right direction. Thanks in advance!

You don’t need a loop for that, because you want to loop whole page (i.e. visit that page three times) and not the fragment of code inside the page.

Now, how you can achieve that.
You should add some kind of GET parameter to your URL which will show what video must be shown now (1st,2nd or 3rd). That means you will have 3 different URLs:

/videos.php
/videos.php?video=2
/videos.php?video=3

Code inside your script should do these things:

  1. Get video number from $_GET['video'] variable;
  2. If there is $_POST['submit'] and video number > 1 then save ratings for previous video;
  3. If number is larger than videos count then redirect to “next” page, else:
  4. Show video with current number;
  5. Display rating form for current video and set form action URL to /videos.php?video={current+1}

Sample implementation:

<?php

	$videos = array('data/test1.mov','data/test2.mov', 'data/test3.mov');
	
	//
	// Get current video number from URL
	// or use 1 (default)
	//
	if (isset($_GET['video'])){	
		$current = $_GET['video'];  
	} else {
		$current = 1;
	}
	
	//
	// Save rating for previous video
	//
	if (isset($_POST['submit'])){
		
		$previous = $current - 1;
		
		$rating = $_POST['score'];
		
		//save to database or do whatever 
		//you want with rating here
		
	}
	
	//
	// If current number is too high
	// then we've done with videos
	//
	if ($current > sizeof($videos)){
		header('Location: next_script.php');
		exit();
	}
	
	// Display current video
	$index = $current - 1; // because array indexes start from zero
	echo "<embed src='{$videos[$index]}' width='400' height='480' controller='true'>";
	
	$next = $current+1;
	
?>

<form action="videos.php?video=<?php echo $next; ?>" method="post">
	<?php for($score=1; $score<=5; $score++) { ?>
		<input type="radio" name="score" value="<?php echo $score; ?>"> <?php echo $score; ?>
	<?php } ?>
	<input type="submit" name="submit">
</form>
1 Like

Thanks a lot for your detailed reply and explanations.
I have a problem understanding the form action

<form action="videos.php?video=<?php ...

When I implement it like this, the script wants to redirect me to a page called “…/videos.php?video=2” after clicking the submit button the first time, which makes totally sense, but this page does not seem to exist (page is blank). I’m wondering what I’m missing here. I’m sorry if it’s obvious but I don’t seem to see it.

Did you give your script name videos.php?

1 Like

Thanks, I didn’t realize I had to point the .php page back to itself and now it works. But now I get transferred to …/videos.php?video=4 after the array has been processed with only 3 videos. Do you have an idea why this might be happening?

It’s okay, you should be redirected from that page to the next one.
This fragment of code does that:

//
// If current number is too high
// then we've done with videos
//
if ($current > sizeof($videos)){
	header('Location: next_script.php');
	exit();
}
1 Like

Thanks again, I introduced a typo I didn’t see for hours…all working smoothly now and will try to make the next page on my own :-).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.