imnaveen
imnaveen

Reputation: 19

Explode function returning single row in textarea

Hi actually when i execute the code i get an output it will be spitted in echo statement but if i put echo in textarea i get only last value of string from my database

    $sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
    while ($row = mysqli_fetch_array($sql)) {
        $myString = $row['admin'];
        $myArray  = explode(',', $myString);
        foreach ($myArray as $my_Array) {
            echo $my_Array.'<br>';
        }
    }
    ?>
    <form method="post" action="">
 <textarea name="valid" cols="60" rows="5"><?php echo $my_Array;?></textarea>
  </form>
    <?php ?>

This is my result

enter image description here

Upvotes: 0

Views: 169

Answers (4)

Charly
Charly

Reputation: 101

You should paint a <textarea> inside of first bucle (while).

echo '<form method="post" action="">';
$sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
    while ($row = mysqli_fetch_array($sql)) {
        $myString = $row['admin'];
        $myArray  = explode(',', $myString);
        echo '<textarea name="valid" cols="60" rows="5">';
        foreach ($myArray as $my_Array) {
            echo = $my_Array.'<br>';
        }
        echo '</textarea>';
    }
    ?>
  </form>

Upvotes: 1

Varun Malhotra
Varun Malhotra

Reputation: 1202

You have to do it like this:

    <form method="post" action="">
<?php $sql = mysqli_query($con, "SELECT naveen from kumar where id<5");
        while ($row = mysqli_fetch_array($sql)) {
            $myString = $row['admin'];
            $myArray[]  = explode(',', $myString);

        }
        ?>
     <textarea name="valid" cols="60" rows="5"><?php echo implode(" ",$my_Array);?></textarea>
      </form>
        <?php ?>

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

You need to concatenate your result with previous one. Delcare $my_Array = ""; before while loop For break line in textarea you have to use &#10;

foreach ($myArray as $my_Array) {
$my_Array .= '&#10;';
echo $my_Array; //IF you also want to echo here
}

Upvotes: 1

Rexan Light
Rexan Light

Reputation: 3

I hope this example will solve your issue. Please let me know if you still have any queries.

<?php
$myvalue = array('one','two','three');
$myterm =  implode('-', $myvalue);
$mynewvalue = explode('-',$myterm);

?>
<form method="post" action="">
 <textarea name="valid" cols="60" rows="5">
    <?php 
        foreach($mynewvalue as $mvn)
            {
                echo $mvn.',';
            }
    ?>
 </textarea>
 </form>

Upvotes: 0

Related Questions