Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:40+00:00 2026-05-23T09:30:40+00:00

I have a loop of data that will only echo the loop inside the

  • 0

I have a loop of data that will only echo the loop inside the while function, but if i call/echo the looped data outside the while function, it only runs the 1st loop.

SAMPLE:

$num = mysql_num_rows($queryFromDB);
$i=0;
while($i < $num)
{
    $field1= mysql_result($queryFromDB,$i,"field1");
    $field2= mysql_result($queryFromDB,$i,"field2");

    $bothFields = $field1 . " " . $field2 "\n";

    // This will show 2 rows of data
    echo $bothFields;

    $i++;

    // This will only show 1 row of data. How can I pass the looped data to another variable?
    echo $bothFields;

}

The output that I wanted to show is:

TITLE/HEADER GOES HERE in the 1st Line

-1st Row of Data from DB

-2nd Row of Data from DB

Here’s the actual code:
$num = mysql_num_rows($qWoundAssessment);

$i=0;
while ($i < $num)
{
$wndType = mysql_result($qWoundAssessment,$i,”wndType”);
$wndNum = mysql_result($qWoundAssessment,$i,”wndNum”);
$wndLocation = mysql_result($qWoundAssessment,$i,”wndLocation”);
$wndStage = mysql_result($qWoundAssessment,$i,”wndStage”);
$wndL = mysql_result($qWoundAssessment,$i,”wndL”);
$wndD = mysql_result($qWoundAssessment,$i,”wndD”);
$wndW = mysql_result($qWoundAssessment,$i,”wndW”);
$wndAseptic = mysql_result($qWoundAssessment,$i,”wndAseptic”);
$wndIrrigate = mysql_result($qWoundAssessment,$i,”wndIrrigate”);
$wndIrrigateBox = mysql_result($qWoundAssessment,$i,”wndIrrigateBox”);
$wndPat = mysql_result($qWoundAssessment,$i,”wndPat”);
$wndCover = mysql_result($qWoundAssessment,$i,”wndCover”);
$wndCoverBox = mysql_result($qWoundAssessment,$i,”wndCoverBox”);
$wndSecure = mysql_result($qWoundAssessment,$i,”wndSecure”);
$wndSecureBox = mysql_result($qWoundAssessment,$i,”wndSecureBox”);
$wndQvisit = mysql_result($qWoundAssessment,$i,”wndQvisit”);

$wnd = “-” . $wndType . ” ” . “#” . $wndNum . “, ” . “LOCATION ” . $wndLocation . “, ” . “STAGE ” . $wndStage;
$wndSize = “SIZE ” . $wndL . “CM” . ” X ” . $wndW . “CM” . ” X ” . $wndD;
if($wndAseptic==”1″){$wndAsepticTech = “USING ASEPTIC TECHNIQUE”;}
if($wndIrrigate==”1″){$wndIrrigateWith = “IRRIGATE WITH ” . $wndIrrigateBox;}
if($wndPat==”1″){$wndPatDry = “PAT DRY”;}
if($wndCover==”1″){$wndCoverWith = “COVER WITH ” . $wndCoverBox;}
if($wndSecure==”1″){$wndSecureWith = “COVER WITH ” . $wndSecureBox;}
if($wndQvisit==”1″){$wndQv = “Q VISIT”;}

if(isset($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)){
$woundCare = implode(“, “,array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)) . “\n\n “;}

$wndCare .= $woundCare;

$i++;
}

$snWoundCare = “SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:” . “\n” . $wndCare;

if I echo $wndCare, it shows the “Undefined variable” error with the actual looped data. But if I pass this variable to PDF, it works.

SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:

-PRESSURE ULCER #1, LOCATION COCCYX, 3, SIZE 2.0CM X 1.5CM X 0.07, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT

-SURGICAL WOUND #2, LOCATION (R) KNEE, , SIZE 29CM X 0CM X 0, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT

================ CODE NOW WORKS!!! HERE’s MY FINAL SOLUTION ======================
$num = mysql_num_rows($qWoundAssessment);
$i=0;
$storeMyData = array();
while($i < $num)
{
$wnd= “-” . mysql_result($qWoundAssessment,$i,”wndType”) . ” #” . mysql_result($qWoundAssessment,$i,”wndNum”). “, LOCATION ” . mysql_result($qWoundAssessment,$i,”wndLocation”) . “, STAGE ” . mysql_result($qWoundAssessment,$i,”wndStage”);
$wndSize = “SIZE ” . mysql_result($qWoundAssessment,$i,”wndL”) . “CM” . ” X ” . mysql_result($qWoundAssessment,$i,”wndW”) . “CM” . ” X ” . mysql_result($qWoundAssessment,$i,”wndD”) . “CM”;
if(isset($rowWoundAssessment[‘wndAseptic’])){$wndAsepticTech = “USING ASEPTIC TECHNIQUE”;}
if(isset($rowWoundAssessment[‘wndIrrigate’])){$wndIrrigateWith = “IRRIGATE WITH ” . mysql_result($qWoundAssessment,$i,”wndIrrigateBox”);}
if(isset($rowWoundAssessment[‘wndPat’])){$wndPatDry = “PAT DRY”;}
if(isset($rowWoundAssessment[‘wndCover’])){$wndCoverWith = “COVER WITH ” . mysql_result($qWoundAssessment,$i,”wndCoverBox”);}
if(isset($rowWoundAssessment[‘wndSecure’])){$wndSecureWith = “SECURE WITH ” . mysql_result($qWoundAssessment,$i,”wndSecureBox”);}
if(isset($rowWoundAssessment[‘wndQvisit’])){$wndQvisit = “Q VISIT”;}

$wndCare = implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQvisit)). "\n\n";

    // This will show 2 rows of data

    $storeMyData[] = $wndCare ;  // store current data in array
    $i++;
}

/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)

/* or join the data using string concatenation /
$allFinalData2 = “”;
/
this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo “SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:” . “\n” . $allFinalData2;

thanks to DhruvPathak and Antonio Laguna! You guys are the best! Just made my day! jumps around the room

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:30:40+00:00Added an answer on May 23, 2026 at 9:30 am

    I am not sure what you want to do with your data. It seems you want to store
    all the data to use it outside the loop, then this is the way to go :

    <?php
    $num = mysql_num_rows($queryFromDB);
    $i=0;
    $storeMyData = array();
    while($i < $num)
    {
        $field1= mysql_result($queryFromDB,$i,"field1");
        $field2= mysql_result($queryFromDB,$i,"field2");
    
        $bothFields = $field1 . " " . $field2 "\n";
    
        // This will show 2 rows of data
        echo $bothFields;
        $storeMyData[] = $bothFields ;  // store current data in array
        $i++;
    
    
    }
    
    
    /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)
      {
         echo $prevData."\n";
      }
    
    ?>
    
     $allFinalData = implode("",$prevData);  // implode will join all the data as string
     echo $allFinalData."\n" ; 
    
    /* or join the data using string concatenation */
     $allFinalData2 = "";
       /* this will echo your storedData of loop */
    foreach($storeMyData as $prevData)
      {
        $allFinalData2 = $allFinalData2.$prevData ;  // keep on concatenating
      }
    
     echo $allFinalData2,"\n";
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a while() function that loops a row of data within my table.
I am writing a PHP function that will need to loop over an array
I have a big loop in actionscript that sends lots of data to an
I have a loop that reads each line in a file using getline() :
I have a loop on page to update an access database that takes 15-20
In my WPF client, I have a loop that calls a WCF service to
I'm trying to solve the 3n+1 problem and I have a for loop that
I have a variable that is built in loop. Something like: $str = ;
I have the following command which will loop over all the subdirectories in a
I have written my own stop_watch module. That will create a thread and go

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.