php - foreach loop keeps repeating from beginning inside for loop? -
after executed query, used foreach ($result $row) data each found row database , used inside loop, had no problem.
$sql="select * post order id desc"; $result = $conn->prepare($sql); $result->execute(); $num_rows=$result->rowcount(); for($i=1;$i<=$num_rows;$i++){ foreach ($result $row) { echo "$i - $row[posttile]";break 1; } }
result looks this:
1 - post 2 - post b ...
however, applied principle print each value in array, foreach loop keep repeating start:
for($i=1;$i<=$num;$i++){ foreach($array $post){ echo "$i - $post";break 1; } }
but result kept printing post every time?
1 - post 2 - post 3 - post ...
how fix code to achieve desired outcome?
Comments
Post a Comment