Why is my foreach duplicating my database records?

Asked

Viewed 155 times

-2

function DBRead($table, $params = null, $fields = '*'){
    $table  = DB_PREFIX.'_'.$table;
    $params = ($params) ? " {$params}" : null;

    $query  = "SELECT {$fields} FROM {$table}{$params}";
    $result = DBExecute($query);

    if(!mysqli_num_rows($result))
        return false;
    else{
        while ($res = mysqli_fetch_assoc($result)){
            $data[] = $res;
        }
        return $data;
    }
}

$publicacao = DBRead('publicacao');
    foreach ($publicacao as $pl):
    endforeach;

    <?php foreach ($publicacao as $pl): ?>
        <li>
            <h4><a href="#"><?php echo $pl['title']?></a></h4>

            <h5><?php echo $pl['text']?><a href="%">Continue lendo &raquo;</a></h5>
        </li>
    <?php endforeach; ?>

    <?php foreach ($publicacao as $pl): ?>
        <li>
            <h4><a href="#"><?php echo $pl['title']?></a></h4>

            <h5><?php echo $pl['text']?><a href="%">Continue lendo &raquo;</a></h5>
        </li>
    <?php endforeach; ?>

I put a print_r($publishing) between the line $publishing = Dbread('publishing'); and foreach ($publication as $pl): Result is below:

Array ( [0] => Array ( [id] => 33 [title] => First Title [text] => First Text ) [1] => Array ( [id] => 34 [title] => Second Title [text] => Second Text ) )

Already the print_r($publishing) that I put between the foreach ($publication as $pl): and endforeach; the result is this:

Array ( [0] => Array ( [id] => 33 [title] => First Title [text] => First Text ) [1] => Array ( [id] => 34 [title] => Second Title [text] => Second Text ) ) Array ( [0] => Array ( [id] => 33 [title] => First Title [text] => First Text ) [1] => Array ( [id] => 34 [title] => Second Title [text] => Second Text ) )

In my database I only have 2 records. Because it is duplicated?

  • Welcome! , because it will print how many times the foreach run... It’s the same as a for if you do($i=0; $i <2; $i++){ print_r($publication); } this is going to run twice, and obviously print_r will be printed 2x but it will depend on how many records your $publication has

  • @Andersonhenrique Hello, I have two records. As I solve this, could help me?

  • Take a look at the answer below @Susi

1 answer

2


Do so, the foreach will run as many times as necessary until you reach the end of the returned array (amount of your records) so these 3 foreach are not necessary. Just remove the other 2

function DBRead($table, $params = null, $fields = '*'){
    $table  = DB_PREFIX.'_'.$table;
    $params = ($params) ? " {$params}" : null;

    $query  = "SELECT {$fields} FROM {$table}{$params}";
    $result = DBExecute($query);

    if(!mysqli_num_rows($result))
        return false;
    else{
        while ($res = mysqli_fetch_assoc($result)){
            $data[] = $res;
        }
        return $data;
    }
}

$publicacao = DBRead('publicacao');

    <?php foreach ($publicacao as $pl): ?>
        <li>
            <h4><a href="#"><?php echo $pl['title']?></a></h4>

            <h5><?php echo $pl['text']?><a href="%">Continue lendo &raquo;</a></h5>
        </li>
    <?php endforeach; ?>
  • Solved. Thank you very much <3

  • How nice of you to help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.