SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use just a plain FOR loop with its counter and not a foreach loop.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 />";
    }
    ?>

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have an indexed array in your code.

    To display the first 3 elements of the array:

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


  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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..

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 Code:
    <?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'];
    }

    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by josh mcq View Post
    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!

  9. #9
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 if we used break anywhere except within a switch block.

    Another way of doing it is something like this:
    PHP Code:
    $count 0

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


  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    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 if we used break anywhere except within a switch block.

    Another way of doing it is something like this:
    PHP Code:
    $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

  11. #11
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JV- View Post
    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?
    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.

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    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.

  13. #13
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JV- View Post
    .......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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •