Page 1 of 1

PHP For loop help

Posted: Tue Jan 07, 2014 11:59 pm
by Zester
I have this code that I am trying to put in two a for loop,
but I can not figure out how to increment the variables.
This what I have so far is
The short code

Code: Select all

for ($i = 1; $i <= 6; $i++) {
    echo "
	<tr>
		<td>$Short1</td>
		<td>$Name1</td>
		<td>$Description1</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='".$i."'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
}



I need to increment the variables $Short1, $Name1, and $Description1.

The long code :

Code: Select all

echo "
	<tr>
		<td>$Short1</td>
		<td>$Name1</td>
		<td>$Description1</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='1'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
	<tr>
		<td>$Short2</td>
		<td>$Name2</td>
		<td>$Description1</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='2'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
	<tr>
		<td>$Short3</td>
		<td>$Name3</td>
		<td>$Description3</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='3'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
	<tr>
		<td>$Short4</td>
		<td>$Name4</td>
		<td>$Description3</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='4'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
	<tr>
		<td>$Short5</td>
		<td>$Name6</td>
		<td>$Description3</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='5'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
		<tr>
		<td>$Short5</td>
		<td>$Name6</td>
		<td>$Description3</td>
		<td><form action='editattrib.php' method='post'>
			<input name='token' type='hidden' value='".$_SESSION['token']."'/>
			<input name='attribnum' type='hidden' value='6'/>
			<input name='submit' type='submit' value='EDIT'/>
			</form></td>
	</tr>
	";
Any one know a way to shorten this code with a loop?
cheers

Re: PHP For loop help

Posted: Wed Jan 08, 2014 12:22 am
by Xaos
Put short, name and description into seperate essays and do $short[$i]. Same with the other two.

Re: PHP For loop help

Posted: Wed Jan 08, 2014 12:28 am
by Jackolantern
What are $Short, $Name and $Description referring to? If you can put them into arrays, you could use

Code: Select all

for ($a = 0; $a < count($Name); $a++, $b++, c++) {
     echo "<tr>
             <td>$Short[$a]</td>
             <td>$Name[$b]</td>
             <td>$Description[$c]</td>
             // etc...
}

Re: PHP For loop help

Posted: Wed Jan 08, 2014 3:43 am
by Zester
Thankz all works great :)