Jump to content


Photo

echo inside of another echo?


  • Please log in to reply
8 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 01:35 AM

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

<? 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>';
  }?>

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?

#2 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 09 July 2006 - 01:38 AM

Remove the second echo and remove the semi-colon after $row['content'].

#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 01:43 AM

I already tried that and it didn't work, that's why i tried the 2nd echo ;)

#4 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 09 July 2006 - 01:45 AM

I already tried that and it didn't work, that's why i tried the 2nd echo ;)

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.

#5 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 01:47 AM

I just re-did that and got nothing :P

*edit*

here's the full 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>


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 July 2006 - 02:15 AM

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:
<td><textarea rows=10 cols=40 name="news_content' . $i . '">'. $row['content'].'></textarea></td>

Also, you will find that using arrays for the names will make the processing of the information coming back from the form much easier:
<?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>';
  }?>

Ken


#7 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 02:50 AM

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

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());
                        }

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
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                                                         
}

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

#8 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 09 July 2006 - 05:49 AM

Why do you define everything as a session variable first?

#9 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 06:02 AM

because most of everything is stored in sessions and if for some reason I decide to rename the session variable,  Iwon't have to rename it everywhere




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users