Jump to content

lots of repeating code into a for loop???


Rizla

Recommended Posts

Hello,

 

I'm making this site to help my girlfriends research for her end terms.

Now she needs to display 2pictures a & b, with a total of 69 sets of 2 pictures.

This is how i'm doing it atm:

 

// Alle foto cases.
<?php
case "foto1":
	// unique key generator: global $frmKey;
		$pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		// if statement checks if $length is empty, if it is it sets its value to 10.
		if(empty($length))
		{ 
			$length = "10"; 
		}
		// for loop
		for($i=0; $i<$length; $i++)
		{ 
			$getKey .= $pattern{rand(0,61)}; 
		}
		$_SESSION[$getKey] = $getKey; // plus stores to session called after the key.			
		$age = $_POST['age_cat'];
		$gender = $_POST['gender'];
		$insert = "INSERT into outputdata (id,gender,age_cat,frmKey) VALUES ('','$gender','$age','$getKey')";
		mysql_query($insert);
	 	include('layout_top.php'); 
		?>
		<table width="650" border="0" cellpadding="0" cellspacing="0" id="pas"><tr>
		<td colspan="2">maak uw keuze </td>
		</tr><tr>
		<td><div align="center"><?php echo('<a href="?page=foto2&keuze=1&frmKey='.$getKey.'"><img src="images/img01_a.jpg" /></a>'); ?></div></td>
		<td><div align="center"><?php echo('<a href="?page=foto2&keuze=2&frmKey='.$getKey.'"><img src="images/img01_b.jpg" /></a>'); ?></div></td>
		</tr></table>
	<?php 
	include('layout_bottom.php'); 
break; // leads to foto2 case
case "foto2":
	$getKey = $_GET['frmKey']; 
	$keuze = $_GET['keuze'];
	$q = "UPDATE `outputdata` SET `img1` = '$keuze' WHERE `frmKey` = '$getKey'";
	mysql_query($q);
	$keuze ="";
	include_once('layout_top.php');
	 ?>
	<table width="650" border="0" cellpadding="0" cellspacing="0">
  		<tr>
    	<td colspan="2">maak uw keuze </td>
  		</tr>
  		<tr>
   	 	<td>
	<div align="center">
	<?php echo('<a href="?page=foto3&keuze=1&frmKey='.$getKey.'"><img src="images/img02_a.jpg" /></a>');	?>
	</div>
	</td>
    	<td>
	<div align="center">
	<?php echo('<a href="?page=foto3&keuze=2&frmKey='.$getKey.'"><img src="images/img02_b.jpg" /></a>'); ?>
	</div> 		 		
	</td>
  		</tr>
	</table>
	<?php 
	include('layout_bottom.php'); 
break; // leads to foto3 case 
case "foto3":
	$getKey = $_GET['frmKey'];
	$keuze = $_GET['keuze'];
	$q = "UPDATE `outputdata` SET `img2` = '$keuze' WHERE `frmKey` = '$getKey'";
	mysql_query($q);
	$keuze="";
	include('layout_top.php');
	?>
	<table width="650" border="0" cellpadding="0" cellspacing="0"><tr>
	<td colspan="2">maak uw keuze </td>
	</tr><tr>
	<td><div align="center"><?php echo('<a href="?page=foto4&keuze=1&frmKey='.$getKey.'"><img src="images/img03_a.jpg" /></a>');?></div></td>
    	<td><div align="center"><?php echo('<a href="?page=foto4&keuze=2&frmKey='.$getKey.'"><img src="images/img03_b.jpg" /></a>');?></div></td>
  		</tr></table>
	<?php 
	include('layout_bottom.php'); 
break; // leads to foto4 case

 

And so on till set 69...

I know this is just a way around and I should do this whole part with a for loop or something.

The problem is i don't know how(yet);)

 

These 69 foto sets are 69 cases in a swithc case statement, there are some pages before these 69

and some after them.

 

The switch case statement acts as a guide that brings them from one page to the next and then saves the data from the previous case.

I hope someone could help me out here.

 

Any tip or hint, clue or tutorial is also welcome.

In the mean time i keep looking for it myself also.

 

Greets Rizla

Link to comment
https://forums.phpfreaks.com/topic/97604-lots-of-repeating-code-into-a-for-loop/
Share on other sites

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.