Jump to content

Recommended Posts

Hey guys,

I have an echo that displays form elements, 1 of them is a textarea and I need the value to be something that's fetched from my DB

so this is what I did but it's now working, keeps giving me an error

[code]
<? for ($i=1;$i<=$num_news;$i++) {
  echo '<tr>
    <td width="151"><strong>Article #'.$i.'</strong></td>
    <td width="442"></td>
  </tr>
  <tr>
    <td>Upload Pic:</td>
    <td><input type="file" name="file' . $i . '"></td>
  </tr>
  <tr>
    <td>Content:</td>
    <td><textarea rows=10 cols=40 name="news_content' . $i . '" value="'. echo $row['content'];.'"></textarea></td>
  </tr>
  <tr>
    <td>Type:</td>
    <td><select name="type' . $i . '" id="type">
<option>Choose one</option>
<option value="house">House</option>
<option value="hip hop">Hip Hop</option>
<option value="both">Both</option></br></br></td>
  </tr>';
  }?>
[/code]

I tried it without the second echo and even though the script worked it didn't display anything. I tried using the echo $row['content'] alone and it worked perfectly so I know that's not the problem.

Any ideas how can I display the textarea value?
Link to comment
https://forums.phpfreaks.com/topic/14070-echo-inside-of-another-echo/
Share on other sites

[quote author=digitalgod link=topic=99923.msg393814#msg393814 date=1152409420]
I already tried that and it didn't work, that's why i tried the 2nd echo ;)
[/quote]
You said you tried removing the second echo, but you never said you removed the semi-colon (;) after $row['content']. Do that, and I guarantee you it will work.
I just re-did that and got nothing :P

*edit*

here's the full code

[code]
<?php
$num_news = $_SESSION['num_news'];
$date = $_SESSION['news_date'];
$result=mysql_query("SELECT * FROM news WHERE date='". $date ."'") or die(query_error());
$row=mysql_fetch_array($result);
echo $row['content'];
?>
<div id="article">
<form action="admin.php?a=newnews" enctype="multipart/form-data" method="post">
<input type="hidden" name="process" value="yes" />
<input type="hidden" name="process_b" value="yes" />
<input type="hidden" name="size_limit" value="500" />
<table width="500" border="0">
  <? for ($i=1;$i<=$num_news;$i++) {
  echo '<tr>
    <td width="151"><strong>Article #'.$i.'</strong></td>
    <td width="442"></td>
  </tr>
  <tr>
    <td>Upload Pic:</td>
    <td><input type="file" name="file' . $i . '"></td>
  </tr>
  <tr>
    <td>Content:</td>
    <td><textarea rows=10 cols=40 name="news_content' . $i . '" value="'. $row['content'].'"></textarea></td>
  </tr>
  <tr>
    <td>Type:</td>
    <td><select name="type' . $i . '" id="type">
<option>Choose one</option>
<option value="house">House</option>
<option value="hip hop">Hip Hop</option>
<option value="both">Both</option></br></br></td>
  </tr>';
  }?>
 
</table>
<br />
<input type="submit" value="Submit News" />
</form>
</div>
[/code]
You're problem maybe that you're using the <textarea> tag incorrectly. That tag doesn't have a 'value="' attribute, but the value should go between the <textarea> and the </textarea> tags. Also, are you sure that the value of $num_news is greater than or equal to 1?

You do realize that this code will output the same $row['content'] n times, where n = $num_news?

For the <textarea> tag, try:
[code]
<td><textarea rows=10 cols=40 name="news_content' . $i . '">'. $row['content'].'></textarea></td>
[/code]

Also, you will find that using arrays for the names will make the processing of the information coming back from the form much easier:
[code]<?php
for ($i=1;$i<=$num_news;$i++) {
  echo '<tr>
    <td width="151"><strong>Article #'.$i.'</strong></td>
    <td width="442"></td>
  </tr>
  <tr>
    <td>Upload Pic:</td>
    <td><input type="file" name="file[' . $i . ']"></td>
  </tr>
  <tr>
    <td>Content:</td>
    <td><textarea rows=10 cols=40 name="news_content[' . $i . ']">'. $row['content'].'</textarea></td>
  </tr>
  <tr>
    <td>Type:</td>
    <td><select name="type[' . $i . ']" id="type">
<option>Choose one</option>
<option value="house">House</option>
<option value="hip hop">Hip Hop</option>
<option value="both">Both</option></br></br></td>
  </tr>';
  }?>[/code]

Ken
hey Ken,

that worked perfectly thank you.

If I use arrays in the form how do I process them?

this is what I got in my admin.php

[code]
for ($i = 1;$i <= $num_news;$i++) {                           
$headline = $i;
                            $content = $_POST['news_content' . $i];
                            $type = $_POST['type' . $i];
mysql_query("UPDATE " . $prefix . "news SET content='$content',type='$type' WHERE headline='$headline' AND date='$date'") or die(query_error());
                        }
[/code]

but it doesn't seem to update anything, it doesn't give me an error either. I think it has to do with $num_news. In the form $num_news is greater than 1. but when I echo $num_news right before the loop it doesn't output anything.

this is what defines $num_news
[code]
if ($_POST['date'] != '') {
$_SESSION['news_date'] = $_POST['date'];  //gets it from the first form
$date = $_SESSION['news_date'];
$result = mysql_query("SELECT COUNT(*) AS count FROM " . $prefix . "news WHERE date='$date'");
$_SESSION['num_news'] = mysql_result($result,0,'count');
$num_news = $_SESSION['num_news'];
echo $num_news; //works perfectly here                                                       
}
[/code]

And yes I did notice that it will output $row['content'] n times, I'm really not sure how to get the correct info from the DB. Let's say I have 3 entries that have the same date but different content, I want to be able to display that content in different textboxes
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.