Solved

PHP foreach Loop and Return

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

912 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

18 Experts available now in Live!

Get 1:1 Help Now