0

I'm trying to print each column row values into a text area without null? If I do it within the while then I got perfect but I need print it in HTML code outside of the while. How do I get this.

$sdata = mysql_query("select (case when `EntryType` like '%Buildup%' then `EntryDescription` end) As cia,
    (case when `EntryType` like '%prelim%' or '%grinding%' then `EntryDescription` end) As grind,
    (case when `EntryType` like '%Covers%' then `EntryDescription` end) As covers,
    (case when `EntryType` like '%prelim%' or '%grinding%' then `EntryDescription` end) As buildup,
    (case when `EntryType` like '%Comment%' then `EntryDescription` end) As comments
  from `selectitem`
  JOIN `patient` on `patient`.`RecdNo` = `selectitem`.`RecordID`
  WHERE `patient`.`PatientID`='".$_GET['edit']."'");

Query Output: enter image description here

while($data = mysql_fetch_array($sdata) ){

}

HTML Code Here:

    <tr>
        <td colspan="2"> <textarea rows="2" name="cia" style="width:90%;"><?php echo $data['cia']; ?></textarea> </label> </td>
        <td colspan="2"> <textarea rows="2" name="grind" style="width:100%;"><?php  ?> </textarea> </td>
    </tr>
    <tr>
        <td colspan="2"><i>Covers</i> </td>
        <td colspan="2"><i>Build Up</i> </td>
    </tr>

    <tr>
        <td colspan="2"> <textarea rows="2" name="covers" style="width:90%;"><?php ?> </textarea> </label> </td>
        <td colspan="2"> <textarea rows="2" name="buildup" style="width:100%;"><?php ?> </textarea> </td>
    </tr>

    <tr>
        <td colspan="4"> <i>Comments</i>  </td> 
    </tr>
    <tr>
        <td colspan="4"> <textarea rows="2" name="comments" style="width:100%;"><?php ?> </textarea></td>
    </tr>
5
  • simple, just take away the while(){} and make it $data = mysql_fetch_array($sdata). Oh yeah and stop using mysql functions Commented Mar 16, 2016 at 19:54
  • Sir, is that print all row for each column into a textarea? Commented Mar 16, 2016 at 20:00
  • it's print first row only. Commented Mar 16, 2016 at 20:01
  • My mistake I thought your query only returned one row. Commented Mar 16, 2016 at 20:02
  • Then how do I get it done? Commented Mar 16, 2016 at 20:05

1 Answer 1

1

You still need to use the while loop, but assign the values to variables like this:

$cia ="";
$grind = "";

$count = mysql_num_rows($sdata); //count rows
$i=1; // set an iteration counter

while($data = mysql_fetch_array($sdata) ){

    if($i < $count){ // check if not last row and add newline if true
        $nl = "\r\n";
    } else {
        $nl = ""; // no newline if last row
    }

    if($data["cia"]){
        $cia .= $data["cia"] . $nl; // remember to change "\r\n" to $nl in all of these places
    }
    if($data["grind"]){
        $grind.= $data["grind"] . $nl;
    }

    $i++; //increase iteration counter
}

Then you output the variables in the textareas:

<tr>
    <td colspan="2"> <textarea rows="2" name="cia" style="width:90%;"><?php echo $cia; ?></textarea> </label> </td>
    <td colspan="2"> <textarea rows="2" name="grind" style="width:100%;"><?php echo $grind; ?> </textarea> </td>
</tr>
<tr>
    <td colspan="2"><i>Covers</i> </td>
    <td colspan="2"><i>Build Up</i> </td>
</tr>

<tr>
    <td colspan="2"> <textarea rows="2" name="covers" style="width:90%;"><?php ?> </textarea> </label> </td>
    <td colspan="2"> <textarea rows="2" name="buildup" style="width:100%;"><?php ?> </textarea> </td>
</tr>

<tr>
    <td colspan="4"> <i>Comments</i>  </td> 
</tr>
<tr>
    <td colspan="4"> <textarea rows="2" name="comments" style="width:100%;"><?php ?> </textarea></td>
</tr>
Sign up to request clarification or add additional context in comments.

5 Comments

for other columns?
just assign the variables the same way as I did the other two.
each variable have a blank row at the end, how do I stop it?
You'll have to count the rows of the result and then only add a new line if it's not the last row. See my updated answer for how to do this.
I got it. Thank you Sir, for the best cooperation and kind behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.