Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP foreach Loop and Return

Posted on 2015-02-21
4
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 2000 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 111

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

Industry Leaders: 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!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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.

670 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