Solved

PHP foreach Loop and Return

Posted on 2015-02-21
4
88 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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

758 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

17 Experts available now in Live!

Get 1:1 Help Now