Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 97
  • Last Modified:

PHP foreach Loop and Return

I don't have any code written to show.  What I am interested in knowing is this;
If I have a query;
<?php
function myFunction() {
  $query = SELECT name FROM Table_Name;
  $data = $data->result_array();
  foreach ($data as $row) {
  $name = $row['name'];
  $html = '<div class="my-class">Hello:: '.$name.' How are you today?<br /></div>';
  }
return $html;
}
Let's say that the query returns 3 names.  I want the $html to loop 3 times.  How do I writ that properly?

Open in new window

0
rgranlund
Asked:
rgranlund
  • 2
1 Solution
 
COBOLdinosaurCommented:
Change  $html =
to  $html .=

So it concatenates instead of overwriting the variable.
Before you start the loop initialize the variable as an empty string:
$html='';

Cd&
0
 
rgranlundAuthor Commented:
Like this?
<?php
function myFunction() {
$html = '';
  $query = SELECT name FROM Table_Name;
  $data = $data->result_array();
  foreach ($data as $row) {
  $name = $row['name'];
  $html. = '<div class="my-class">Hello:: '.$name.' How are you today?<br /></div>';
  }
return $html;
}

Open in new window

0
 
COBOLdinosaurCommented:
That is about right except that there should not be a space between the dot and the equal sign.  I am assuming that  this:
$query = SELECT name FROM Table_Name;

Is just pseudo code for the actual db methods you use to ge the data.

Cd&
0
 
Ray PaseurCommented:
The answer is right from COBOLdinosaur.  I just want to make two suggestions.

1. Learn about PHP coding standards and follow them.  If you do this, you will instantly become more productive because your code will be easier to read, understand and test.  The links can be found here:
http://www.php-fig.org/

2. Start with var_dump() always.  It will save you a huge amount of time if you know what your data looks like.  Here is how I might start the code, including comments that explain what I am thinking at each step.

<?php

/**
 * Demonstrate how to read from a data base query results set
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28621838.html
 *
 * @param none
 *
 * @return none
 */
error_reporting(E_ALL);


function myFunction() 
{
    // A QUERY WITH NO ORDER, NO GROUP, NO LIMIT 
    $query = "SELECT name FROM Table_Name"; 
    
    // INSERT CODE TO RUN THE QUERY HERE
    
    // RETRIEVE A COLLECTION OF ROWS FROM THE QUERY
    $data = $results->result_array();
    var_dump($data);
    
    // ITERATE OVER THE RESULTS SET AND PRINT OUT THE ROWS
    foreach ($data as $row) 
    {
        var_dump($row);
    }
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now