foreach index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jacosan
Forum Newbie
Posts: 3
Joined: Sat Nov 15, 2008 5:54 am

foreach index

Post by jacosan »

Hi

I`ve have a code that constructs a table with the appointments entered in a data base by a user. The code should be something like this, though i`ve try many others structures

<form action="delete_app.php" method=post enctype="multipart/form-data">
<table>
<?php foreach($app as $index =>$data_app) {
$data_app =& $app[$index];?>
<tr>
<td><input type="hidden" name="id_service" value="<?php echo $index?>"><?php echo $data_app[0]?></td>
<td><?php echo $data_app[1]?></td>
<td><?php echo $data_app[2]?></td>
<td><?php echo $data_app[3]?></td>
<td><input type="submit" value="Cancel Appoiment" /></td>
<?php unset ($data_app);
}?>
</table>
</form>

The point is that i need to get de data_app[0] value from every appoiment so i can delete the appoiment with another function.

Print_r($app) returns this for a user with three appoiments

Array ( [0] => 164 [IdApp] => 164 [1] => 45 [IdService] => 45 [2] => 2008-11-16 [Date] => 2008-11-16 [3] => 09:00:00 [Hour] => 09:00:00 ) Array ( [0] => 163 [IdApp] => 163 [1] => 3 [IdService] => 3 [2] => 2008-11-16 [Date] => 2008-11-16 [3] => 09:00:00 [Hour] => 09:00:00 ) Array ( [0] => 163 [IdApp] => 163 [1] => 3 [IdService] => 3 [2] => 2008-11-16 [Date] => 2008-11-16 [3] => 09:00:00 [Hour] => 09:00:00

I understand the structure of the $app array and i can get the IdApp that i want using two indexes (print $app [1][0] returns 163 (IdApp)) but i can´t get it dynamically, that is, by clicking the button on the <table>.
Any idea?
Last edited by jacosan on Sat Nov 15, 2008 11:04 am, edited 2 times in total.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: foreach index

Post by Syntac »

Always use <?php. Your code will not be portable if you use <? or <?=.
jacosan
Forum Newbie
Posts: 3
Joined: Sat Nov 15, 2008 5:54 am

Re: foreach index

Post by jacosan »

thanks syntac, it`s done
Any other idea?
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: foreach index

Post by Syntac »

<?php= will not work.

Code: Select all

<?php echo $some_variable; ?>
jacosan
Forum Newbie
Posts: 3
Joined: Sat Nov 15, 2008 5:54 am

Re: foreach index

Post by jacosan »

Done. Now, any ideas about getting that index dynamically?
Post Reply