0
echo "<table cellpadding=5 border=1>";
            foreach ($array as $value) {
                echo "<tr><td>".$value['title']."</td>";
                echo "<td>".$value['author']."</td>";
                echo "<td>".$value['year']."</td>";
                echo "<td><button onClick=\"location.href='delete.php?id=$value['id']'\">delete</button></td></tr>";
            }
        echo "</table>";

Just to say on start, this is test code, for my own practise.

I am getting error when i put id in ' ' like this : $value['id']. It is some string error i guess, tried like dozen ways to get out of it. On delete.php i have $id=$_GET['id']; which will take sent id, but nothing is sent.

Thanks, Michael

Michael
  • 199
  • 2
  • 16
  • You're getting the T_ENCAPSED_AND_WHITESPACE error, I presume. http://stackoverflow.com/a/13935532/541091 ...because of the quoted array key `['id']` which is already inside a double-quoted string but not `{}` enclosed. Several options for proceeding are in the linked answer. – Michael Berkowski Dec 01 '14 at 15:24

1 Answers1

1

You cannot add $value['id'] into the string like that... Try this:

echo "<table cellpadding=5 border=1>";
            foreach ($array as $value) {
                echo "<tr><td>".$value['title']."</td>";
                echo "<td>".$value['author']."</td>";
                echo "<td>".$value['year']."</td>";
                echo "<td><button onClick=\"location.href='delete.php?id=" . $value['id'] . "'\">delete</button></td></tr>";
            }
        echo "</table>";
RichardBernards
  • 3,146
  • 1
  • 22
  • 30
  • or just `echo "$value[id]"`... – Marc B Dec 01 '14 at 15:25
  • @MarcB Yes... but that's just semantics... – RichardBernards Dec 01 '14 at 15:26
  • tried `echo "";` but nothing is happening when i go on `delete.php`. i have `` but also there is nothing in url bar just `delete.php?id=` without id. – Michael Dec 01 '14 at 15:33
  • @Michael than I guess `id` is not an element in in the `$value` element of the array... Try echoing the contents of `$value` with `echo print_r($value,true);` and see if there is an element present with key `id` – RichardBernards Dec 01 '14 at 15:34
  • @RichardBernards oh yea. id is AutoIncrement in mysql. when i do `echo print_r($value,true)` i see only title and authors, but no id. shold i `fetch_array()` it or ? – Michael Dec 01 '14 at 15:37
  • 1
    @Michael Post the code which contains the query... It should be obvious from that... in your query there should be something like `SELECT id, title, author, year FROM table WHERE...` – RichardBernards Dec 01 '14 at 15:39
  • @RichardBernards oh i am so...Stup*d...i forgot to put ID in `select from ` ... Thanks!! – Michael Dec 01 '14 at 15:41
  • @Michael I already guessed that was happening, have a great day! =D – RichardBernards Dec 01 '14 at 15:42