-1

This is my first time trying to create an array but it's not going so well.

My code scans every $_POST in a form when pressing a submit button below an input field. What I wanted it to do was to only fetch the content of a "filled" input. Well life's full of disappointments so then I tried to remove all the empty array elements. But instead of doing that, it echos the word "Array".

I've always been bad at arrays, ever since my C# days and I've never known why because they always look so simple. If anybody can tell me what I'm doing wrong, I'd appreciate it.

PHP code that goes above the <html>

<?php
    //This is what I've tried before
    //$klant = array();

    foreach($_POST as $k => $v) {
        if (substr($k,0,10) == 'newRecord') {
          $id = substr($k,10);
          $klant[] = $v;

          $klant = array_filter($klant);
          echo $klant;
        }
    }
?>

Markup

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <table>
        <?php
            $query = ("select * from table1");
            $result = mysqli_query($con, $query) or die (mysqli_error());
            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $id = $row['rowid'];
        ?>
        <tr><!--There are multiple rows, that's why I'm using foreach($_POST as $k => $v)-->
            <td>
                <input class="newRecord<?php echo $id; ?>" type="text" name="newRecord<?php echo $id; ?>" />
                <a href="?record=<?php echo $id; ?>">
                    <button class="saveRecord<?php echo $id; ?>" name="saveRecord<?php echo $id; ?>">Save</button>
                </a>
            </td>
        </tr>
        <?php } ?>
    </table>
</form>
deceze
  • 510,633
  • 85
  • 743
  • 889
  • @Rizier123 My bad, updated the code. Right now this code is for testing. – user3095385 May 27 '15 at 09:16
  • There are no errors, only wrong prints. – user3095385 May 27 '15 at 09:22
  • I edited my answer to fit you edit. – FrancoisBaveye May 27 '15 at 09:26
  • So is your error fixed? Your question is now an incomprehensible hybrid of your old problem and your new solution and some unstated new issue. – deceze May 27 '15 at 09:28
  • @deceze I'm affraid not. I've tried Heru-Luin's edit. But no success with that either. – user3095385 May 27 '15 at 09:35
  • Please read your question again from start to finish including the title. Again: it's incomprehensible what your current question is. – deceze May 27 '15 at 09:36
  • Slowly turning one question into another question doesn't work on SO. Your original problem was solved. Great. Mark one of the answers as accepted. Post a new question which focuses on your next problem if necessary. Don't edit your question into oblivion, then the existing answers have nothing to do with the question anymore and everyone gets confused. – deceze May 27 '15 at 09:44
  • @deceze the problem still isn't solved. But I've gotten close with someone's answer and I'll mark it as accepted when I've got that working. – user3095385 May 27 '15 at 09:47

4 Answers4

3

You cannot echo arrays use print_r or var_dump

for Pretty-printing arrays try this

echo "<pre>";
print_r($your_array);
echo "</pre>";
Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
0

Change this :

echo $klant;

To this :

print_r($klant);

Or this :

var_dump($klant);

echo is just for strings, print_r and var_dump will echo anything (strings, arrays, objects etc).

EDIT for OP EDIT :

This will remove anything that is empty inside the array :

foreach ($klant as $key => $value)
    if (empty($value))
        unset($klant[$key]);
print_r($klant);
FrancoisBaveye
  • 1,902
  • 1
  • 18
  • 25
0

Try this

    foreach($_POST as $k => $v) {
        if (substr($k,0,10) == 'newRecord') {
            $id = substr($k,10);
            $klant[] = $v;
        }
    }
var_dump($klant);
Mahadeva Prasad
  • 709
  • 8
  • 19
0

I believe this is what you want!

<?php
    $klant = array();

    foreach($_POST as $k => $v) {
        if (substr($k,0,10) == 'newRecord' && trim($v)) {
          $id = substr($k,10);
          $klant[$id] = $v;
        }
    }
    echo "<pre>";var_dump($klant);echo "</pre>";
?>

But I am not that confident that I understand the question clearly. Can you clarify a bit more than this(if this is not what you desire)?

Arcanyx
  • 852
  • 10
  • 25