Simple for loop problem

If I have an array like the following, how do I get my page to simply echo the first 3 entries and stop?

<html>
<body>

<?php
$x=array(“one”,“two”,“three”,“four”,“five”,“six”);
foreach ($x as $value)
{
echo $value . “<br />”;
}
?>

</body>
</html>

use just a plain FOR loop with its counter and not a foreach loop.

That was my first thought, but how exactly do I do that? Don’t you have to define your variable as a number? How do you get the funtion to echo anything if the array isn’t assigned to a variable? Could you provide the code for the first example to echo the last the items in the array?

For loop syntax<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . “<br />”;
}
?>

You have an indexed array in your code.

To display the first 3 elements of the array:


for($i=0; $i < 3; $i++){
     echo $x[$i].'<br />';
}

Thanks, you answered my question Webdev1958… problem is, it didn’t solve my problem, because i didn’t exactly explain it correctly. So here’s another attempt at explaining the problem I’m trying to solve. So with a little more detail, any help would be much appreciated.

I start with a table. Then I query the table as follows:

$sql = “SELECT ID, Name, Address FROM shops ORDER BY ID desc”;
$result = mysql_query($sql, $connection);

Then I put each row from the table into a new array.

$results = array();
while($row = mysql_fetch_assoc($result))
{
$results[] = array(‘name’ => $row[‘Name’], ‘address’ => $row[‘Address’],‘id’ => $row[‘ID’]);
}

So here’s where the real problem is: I need to echo the first 3 rows(ID, Name, and Address) from the table. I’m just not sure how to put all that into a loop statement…

With the use case that you have explained, I think it is unnecessary for you to loop through all of your results. Can’t you just select the first three records from the database, loop them all, echo them, and call it a day?

Like this:

<?php

$sql = "SELECT ID, Name, Address FROM shops ORDER BY ID desc LIMIT 3";
$result = mysql_query($sql, $connection);

while($row = mysql_fetch_assoc($result))
{
    echo $row['Name'] . ' | ' . $row['Address'] . ' | ' . $row['ID'];
}

?>

Or do you need all of the records from the database, but only want to show the first three?

In that case, do this:

<?php

$sql = "SELECT ID, Name, Address FROM shops ORDER BY ID desc";
$result = mysql_query($sql, $connection);

$count = 0;

while($row = mysql_fetch_assoc($result))
{
    $count++;
    if($count > 3) break;
    echo $row['Name'] . ' | ' . $row['Address'] . ' | ' . $row['ID'];
}

?>

Thanks! That worked pretty well. Still learning SQL as well as PHP, so sometimes I think the answer has to be in the PHP, but I wasn’t aware you could simply limit the number of entries returned in the SQL query.

Glad to help. Cheers!

While using break to break out of loops is legal, it was frowned upon by my teacher and we (my class) were “threatened” with expulsion :eek: if we used break anywhere except within a switch block.

Another way of doing it is something like this:


$count = 0; 

while($row = mysql_fetch_assoc($result) && $count++ < 3) { 
    echo $row['Name'] . ' | ' . $row['Address'] . ' | ' . $row['ID']; 
}

Your statement is an improvement. What did your teacher say was his reasoning behind raging over a break statement within a loop? Was it a PHP class?

As you noted, it is in fact “legal” : http://php.net/manual/en/control-structures.break.php

I bet the reason he got upset about it is that technically if you got a lot of things going on in your while loop and you mess up some logic, you might not increment your counter and accidentally end up with an infinite loop. With that in mind, the while condition that you posted is indeed superior. Thanks for sharing :slight_smile:

He was actually a she and she taught us both PHP and Javascript. She came from a C programming background and she tried to instill in us to use “logic” instead of a break statement to break out of loops. Similarly with FOR loops. To break out of a FOR loop we were encouraged to set the value of the counter variable to its upper limit instead of using break if we needed to break out of the loop.

Her view was that using break to break out of loops was a form of spaghetti code.

I agree with the decrementing loops instead of incrementing in a sense, but it can become a hassle.

I don’t see why it should be.

Can you post an example of where it would be a hassle?