Jump to content

[SOLVED] Confusing Sentence/Comment Structuring System


Jakebert

Recommended Posts

On one page, generate_comment.php, the user enters the student's first name, last name, grade, and gender, and is then presented with a list of possible sentences, each with a checkbox next to it. The sentences are stored in a database. There are three types of sentences: intro, body, and conclusion. After selecting the sentences he/she would like, the user clicks submit and is brought to a page where all the sentences he selected are put together in order.

 

Here's what generate_comment.php looks like:

 

<?php

//DB connection
mysql_connect("localhost", "###", "###") or die(mysql_error());
mysql_select_db("###") or die(mysql_error());

if (isset($_POST['submit'])) {

if (!$_POST['fname'] | !$_POST['lname'] | !$_POST['sex'] | !$_POST['level']) {
die('You did not complete all of the required fields');
}

$fname=$_POST['fname'];
$lname=$_POST['lname'];

if($_POST['sex']=="male") {
	$pos="his";
	$pro="he";
	}

if($_POST['sex']=="female") {
	$pos="her";
	$pro="she";
	}

?>

<form action="comment.php" method="post">
<h2>POSITIVE Intro Comments</h2>
<h5>Pick one.</h5>
<?php
$result = mysql_query("SELECT * FROM comments WHERE type='intro'");

while($row = mysql_fetch_array($result))
  {
?>

<input type="radio" name="intro_sentence" value="<?php $row['comment'] ?>" />

<?php 

echo $row['comment'] ;
  echo "<br />";
  }


$result = mysql_query("SELECT * FROM comments WHERE type='body'");

?>
<h2>Body Comments</h2>
<h5>Pick as many as are appropriate. Try for three.</h5>
<?php

while($row = mysql_fetch_array($result))
{
?>
<input type="checkbox" name="body_sentence" value="<?php $row['comment'] ?>" />
<?php

echo $row['comment'] ;
echo "<br />";
}

$result = mysql_query("SELECT * FROM comments WHERE type='closing'");
?>

<h2>POSITIVE Closing Comments</h2>
<h5>Pick one.</h5>
<?php

while($row = mysql_fetch_array($result))
{
?>
<input type="radio" name="closing_sentence" value="<?php $ow['comment'] ?>" />
<?php

echo $row['comment'] ;
echo "<br />";
}
?>
<br /> <br />
<input type="submit" name="submit" value="Generate Comment" />
</form>



<?php
}else{
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td>Student's First Name:</td><td>
<input type="text" name="fname" size="15">
</td></tr>
<tr><td>Student's Last Name:</td><td>
<input type="text" name="lname">
</td></tr> <br /> <br />
<tr><td>Level:</td><td>
<input type="text" name="level" size="10">
</td></tr> <br /> <br />
<tr><td>Gender:</td><td> <br /><br />
<input type="radio" name="sex" value="male" /> Male
<br />
<input type="radio" name="sex" value="female" /> Female
</td></tr> <br />

<tr><th colspan=2><input type="submit" name="submit" value="Go!"></th></tr> </table>
</form>

<?php
}
?>

 

 

So, two questions:

 

How can I get the sentences selected from generate_comment.php to go to comment.php?

 

and how can I get the name entered at the beginning to appear in the sample sentence?

 

Please, if anything is confusing, tell me.

Any help is appreciated.

Link to comment
Share on other sites

1. I'm not sure what to write on comment.php so that it reads what is being sent from generate_comment.php. Also, the intro sentence and the closing sentence are radio selection, but the body sentences are checkbox to be able to add more than one.

 

2. For example, if the sentence in the DB is:

It has been a very productive session for $fname.

 

When the script runs, instead of it echoing "...session for $fname"

I'd like it to echo "...session for Bobby" (or whatever the user inputted).

 

Thanks for your help!

 

Link to comment
Share on other sites

1. All the send data through the method post are stored in a variable called $_POST.

So in comments.php,  $_POST['intro_sentence'] will be your intro sentence.

Same goes for $_POST['closing_sentence']

 

But won't work for $_POST['body_sentence']. Cause it is a checkbox and the way it is been coded in generate_comment.php can't send multiple data to that. the solution would be :

 

<?php

while($row = mysql_fetch_array($result))
{
?>
<input type="checkbox" name="body_sentence[]" value="<?php $row['comment'] ?>" />
<?php

echo $row['comment'] ;
echo "<br />";
}

 

Notice the name="body_sentence[]"

 

Now in the comments.php your body sentence will be stored in an array:

 

$_POST['body_sentence']

 

And body sentences can be accessed like this:

 

$_POST['body_sentence'][0]

$_POST['body_sentence'][1]

$_POST['body_sentence'][2]

.

.

.

 

2.I'm not sure I have get you right, 

Have you stored in the database something like this:

It has been a very productive session for $fname.

??

 

Then if you want to change $fname to "bobby" for example:

you can go:

 

<?php
echo str_replace('$fname', 'bobby', DB_LINE);
?>

where DB_LINE is the variable which habe stored your specific line in db.

 

 

Link to comment
Share on other sites

So if comment.php reads like this:

 

<body>

<?php


echo $_POST['intro_sentence'];
echo $_POST['body_sentence'][0];
echo $_POST['body_sentence'][1];
echo $_POST['body_sentence'][2];
echo $_POST['closing_sentence'];
?>

 

Theoretically it should work? At the moment all that comes up on comment.php is a blank page, no errors.  :confused:

 

Sorry to be such a pain, and thanks for the help.

Link to comment
Share on other sites

ok I tried you code and it worked here. (I hadmodified your code a bit)

 

since we are at this point,

There are lotta ways you can improve performance too.

 

But for now:

1. for the closing part you have :

<input type="radio" name="closing_sentence" value="<?php $ow['comment'] ?>" />

 

Notice you have missed the letter r there.

 

2. did you add [] at the end of your body_sentence name? like this:

<input type="radio" name="body_sentence[]" value="<?php $row['comment'] ?>" />

 

3. Why you are coding html/php into each other? the readabilty is horrible XD Look at this code:

 

<?php

//DB connection
mysql_connect("localhost", "kharkhar", "mangul") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

if (isset($_POST['submit']))
{
if (!$_POST['fname'] | !$_POST['lname'] | !$_POST['sex'] | !$_POST['level'])
	die('You did not complete all of the required fields');


$fname=$_POST['fname'];
$lname=$_POST['lname'];

if($_POST['sex']=="male")
{
	$pos="his";
	$pro="he";
}

if($_POST['sex']=="female")
{
	$pos="her";
	$pro="she";
}

echo '
<form action="comment.php" method="post">
<h2>POSITIVE Intro Comments</h2>
<h5>Pick one.</h5>';

$result = mysql_query("SELECT * FROM comments WHERE type='intro'");
while($row = mysql_fetch_array($result))
{
	echo '<input type="radio" name="intro_sentence" value="'.$row['comment'].'" />';
	echo $row['comment'] ;
	echo '<br />';
}

$result = mysql_query("SELECT * FROM comments WHERE type='body'");

echo '
<h2>Body Comments</h2>
<h5>Pick as many as are appropriate. Try for three.</h5>';


while($row = mysql_fetch_array($result))
{
	echo '<input type="checkbox" name="body_sentence[]" value="'.$row['comment'].'" />';
	echo $row['comment'] ;
	echo "<br />";
}

$result = mysql_query("SELECT * FROM comments WHERE type='closing'");

echo '
<h2>POSITIVE Closing Comments</h2>
<h5>Pick one.</h5>';


while($row = mysql_fetch_array($result))
{
	echo '<input type="radio" name="closing_sentence" value="'.$row['comment'].'" />';
	echo $row['comment'] ;
	echo "<br />";
}

echo '
<br /> <br />
<input type="submit" name="submit" value="Generate Comment" />
</form>';
}
else
{
echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<tr><td>Student\'s First Name:</td><td>
<input type="text" name="fname" size="15">
</td></tr>
<tr><td>Student\'s Last Name:</td><td>
<input type="text" name="lname">
</td></tr> <br /> <br />
<tr><td>Level:</td><td>
<input type="text" name="level" size="10">
</td></tr> <br /> <br />
<tr><td>Gender:</td><td> <br /><br />
<input type="radio" name="sex" value="male" /> Male
<br />
<input type="radio" name="sex" value="female" /> Female
</td></tr> <br />

<tr><th colspan=2><input type="submit" name="submit" value="Go!"></th></tr> </table>
</form>';

}
?>

Link to comment
Share on other sites

I don't know how you did it, but it works! Thank you thank you thank you thank you! :thumb-up:

 

Now I'm going to try getting the names to work. The only problem is, I have no way of knowing what the $fname that the user will enter is. So is it possible to do:

 

echo str_replace('$fname', $fname, $row);

 

Will that replace all the instances of "$fname" in the DB sentences with what is stored in the local $fname variable?

 

[Also, is there a way I can get the combined sentences on comment.php to go into an editable textarea that the user can finalize?]

Link to comment
Share on other sites

You have to send the username the user typed to the comments.php also.

 

Since you are asking about his username in the first form, So you CAN have it in the second form (Which user picks the comments).

All you need to do is define the username in the second form, So just something to grab in the comments.php

 

This can be a good example for you to learn the hidden inputs.

 

Here: (Where your second form starts)

echo '

  <form action="comment.php" method="post">

  <h2>POSITIVE Intro Comments</h2>

  <h5>Pick one.</h5>';

 

add this line after it:

<input type="hidden" name="username" value="'.$fname.'" />

 

now in comments.php, the username will be :  $_POST['username']

and so on accordingly:

 

echo str_replace('$fname', $_POST['username'], $row);

But I don't know what is your $row. the way you have coded, I doubt the $row variable will be your db line.

 

 

[Also, is there a way I can get the combined sentences on comment.php to go into an editable textarea that the user can finalize?]

 

There is always a way. :)

Same as what you did in generate_comment. You can put a checkbox next to each comment in the comments or so.

don't confuse yourself with many tasks though, One by one.

Link to comment
Share on other sites

2 ways:

 

1.

<textarea name="test" cols="100" rows="30"></textarea>

 

2.

<textarea name="test" style="width:99%;height:200px;"></textarea>

 

In the second way I put 99% for width, The reason for why not 100% is that some browsers like IE for some reason mess up on 100%, So 99% it is ;)

Link to comment
Share on other sites

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.