PHP For loop help

C++, C#, Java, PHP, ect...
Post Reply
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

PHP For loop help

Post 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
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: PHP For loop help

Post by Xaos »

Put short, name and description into seperate essays and do $short[$i]. Same with the other two.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP For loop help

Post 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...
}
The indelible lord of tl;dr
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

Re: PHP For loop help

Post by Zester »

Thankz all works great :)
Post Reply

Return to “Coding”