Solved

PHP foreach Loop and Return

Posted on 2015-02-21
4
90 Views
Last Modified: 2015-03-07
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
Comment
Question by:rgranlund
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 40623534
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
 
LVL 7

Author Comment

by:rgranlund
ID: 40623542
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40623556
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40623610
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now