0

Hi all,
Im learning PHP & SQL very quickly, but still have issues with counting this, and incrementing that.... so if someone could help, you'd be(proverbially) teaching a man(woman in my case..lol) to fish....

Im constructing a form which in part, allows user to input instructions.
I would like the users input to increment with numbering, each time user hits "enter" in the textarea to make a paragraph, so that the output looks something like:

step 1. blah blip blah
<user hits "enter", makes paragraph>
step 2. blah blur blah
<user hits "enter", makes paragraph>

The best example I have is on the website recipezaar.com...

so for example:
http://www.recipezaar.com/Crock-Pot-Chicken-W-Black-Beans-and-Cream-Cheeseyum-89204

which is just an awesome site btw for any of u who like to cook...

If you look to how their "Directions" on recipe pages increment, you will see what I am trying to explain, god help my explainatory abilities. The way in which i explained i would like input incremented, is how they increment it on this website.... user presses "enter"..input is incremented..

If anyone could give me an idea on how I could implement this....very thankful, i would be...

Cheers! Clarity

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by somedude3488
0

Pretty simple. Check this example.

<?php
if(isset($_POST['submit'])) {
	$string = nl2br($_POST['textarea']);
	echo $string."<br />";
	echo substr_count($string, "<br />");
}
?>
<html>
<body>
<form method='POST'>
<textarea rows="30" cols="30" name='textarea'>
</textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

I am using nl2br function to convert newline to <br />. Then I am counting how many <br />'s are present in the sting. This would give the count of the "Enter" pressed by the user.
Cheers!

0

on input of the data, you need to replace the newlines "\r\n" with a replaceable string. i have had problems with newlines being forgotten when they are put into the database.
ex.

$text = preg_replace( "/(\r\n)/","|NL|",$_POST['text'] ); //where $_POST['text'] is the name of the textarea

then on output, explode the data.
ex.

$text = explode( '|NL|',$row['text'] ); //$row['text'] is from the db
$i = 1;
foreach( $text as $txt ) {
  echo "{$i} - {$txt}<br />";
  $i++;
}

like that? how i would do it

0

OK, wonderful... thanks for both answers:)
But i dont think i have explained this correctly...
Firstly:
When the input is entered via the textarea, each step a user types is done like this:

Recipe Directions:
Take your eggs, and whisk
<user hits "enter" and makes new line>
Add eggs to flour mix
<user hits "enter" and makes new line>
Beat until lumps are gone.

So i think kkeith29, your answer would solve that, replacing the newlines "\r\n" with a replaceable string and using foreach.

secondly:

Then I need the data to output with numbering. So as per my example, i want it to output as:



Recipe Directions:

1. Take your eggs & milk, and whisk
2. Add eggs & milk mix to flour mix
3. Beat until lumps are gone.

Each time user creates new line, i want it to increment, & number each line. Hope that makes more sense.

0

then on output, explode the data.

Oops, I didnt see that before I replied... I guess that answers my problem..

Thank you....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.