Jump to content

php explode & texarea


tobimichigan

Recommended Posts

 

With a database of synonyms, how do I compare a fragment of a texarea sentence split by the php explode function to return only five synonyms out of say 100 synonyms of the same word for each fragment.

 

The Sql statement is queried to return 5 results

<?php
if (isset($_GET['submit'])) {
$sentence = $_GET['sentence'];

// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);

//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';

// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";

//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

//$words=$_GET['words'];
$sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);


if (!$data) {
die('<p>Error Retrieving<br/>'.
'Error: ' .mysql_error() . '</p>');}

$datalist=mysqli_fetch_assoc($data);
$synonym=$datalist['synonym'];

?>

I intend to use a 2 texareas: 

1.

 <textarea name="split_sentence"><?php echo "sentence" ?></textarea> 

  the 1st <texarea> has the original sentence to be split by the explode function.

2.

 <textarea name="split_sentence2"><?php echo ("$words[$i] {{$synonym}}?></textarea> 

the second <texarea> should take the explode fragments compare it with the query above and return five synonyms per word on a SINGLE <textarea> tag.

 

3. Besides, I keep getting this offset error on the 2nd <texarea> refering to the $words[si] array element in the 2nd <textarea>

Below is the full code:

`<?php 
//session_start();
  // Define database connection constants
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASSWORD', '');
  define('DB_NAME', 'moby_thesaurus');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>compare_words</title>

</head>

<body>
<table><p> PLEASE ENTER INITIAL SENTENCE </p>
<form name="sentences" method="get" action="" >
    	<tr>
        	<td>            
            	<textarea name="sentence" id="elm1"></textarea>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Create_Spinable Content" />
            </td>
        </tr>
    </form>	
</table>
<?php
if (isset($_GET['submit'])) {
$sentence = $_GET['sentence'];

// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);

//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';

// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";


//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

//$words=$_GET['words'];
$sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);


if (!$data) {
die('<p>Error Retrieving<br/>'.
'Error: ' .mysql_error() . '</p>');}

$datalist=mysqli_fetch_assoc($data);
$synonym=$datalist['synonym'];

?>
<table><p> SPINABLE CONTENT </p>
<form name="sentences" method="get" action="" >

	<tr>
        	<td>            
             <?php
  if ($datalist=mysqli_fetch_array($data)) {
do{
?>
            	<textarea cols="80" id="editor1" name="sentence" rows="10"><?php echo ("$words[$i] {{$synonym}}||{{$synonym}}||{{$synonym}}"); ?></textarea>
                <?php

}
while ($datalist=mysqli_fetch_array($data));
echo("</table> </p>");
}
?>
<?php
}
}
?>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Spin This Now" />
            </td>
        </tr>
    </form>	
</table>

</body>
</html>

Please any suggestions would be highly appreciated.

Link to comment
Share on other sites

 

With a database of synonyms, how do I compare a fragment of a texarea sentence split by the php explode function to return only five synonyms out of say 100 synonyms of the same word for each fragment.

 

The Sql statement is queried to return 5 results

<?php
if (isset($_GET['submit'])) {
$sentence = $_GET['sentence'];

// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);

//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';

// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";

//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

//$words=$_GET['words'];
$sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);


if (!$data) {
die('<p>Error Retrieving<br/>'.
'Error: ' .mysql_error() . '</p>');}

$datalist=mysqli_fetch_assoc($data);
$synonym=$datalist['synonym'];

?>

I intend to use a 2 texareas: 

1.

 <textarea name="split_sentence"><?php echo "sentence" ?></textarea> 

  the 1st <texarea> has the original sentence to be split by the explode function.

2.

 <textarea name="split_sentence2"><?php echo ("$words[$i] {{$synonym}}?></textarea> 

the second <texarea> should take the explode fragments compare it with the query above and return five synonyms per word on a SINGLE <textarea> tag.

 

3. Besides, I keep getting this offset error on the 2nd <texarea> refering to the $words[si] array element in the 2nd <textarea>

Below is the full code:

`<?php 
//session_start();
  // Define database connection constants
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASSWORD', '');
  define('DB_NAME', 'moby_thesaurus');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>compare_words</title>

</head>

<body>
<table><p> PLEASE ENTER INITIAL SENTENCE </p>
<form name="sentences" method="get" action="" >
    	<tr>
        	<td>            
            	<textarea name="sentence" id="elm1"></textarea>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Create_Spinable Content" />
            </td>
        </tr>
    </form>	
</table>
<?php
if (isset($_GET['submit'])) {
$sentence = $_GET['sentence'];

// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);

//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';

// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";


//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

//$words=$_GET['words'];
$sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);


if (!$data) {
die('<p>Error Retrieving<br/>'.
'Error: ' .mysql_error() . '</p>');}

$datalist=mysqli_fetch_assoc($data);
$synonym=$datalist['synonym'];

?>
<table><p> SPINABLE CONTENT </p>
<form name="sentences" method="get" action="" >

	<tr>
        	<td>            
             <?php
  if ($datalist=mysqli_fetch_array($data)) {
do{
?>
            	<textarea cols="80" id="editor1" name="sentence" rows="10"><?php echo ("$words[$i] {{$synonym}}||{{$synonym}}||{{$synonym}}"); ?></textarea>
                <?php

}
while ($datalist=mysqli_fetch_array($data));
echo("</table> </p>");
}
?>
<?php
}
}
?>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Spin This Now" />
            </td>
        </tr>
    </form>	
</table>

</body>
</html>

Please any suggestions would be highly appreciated.

 

 

 

Link to comment
Share on other sites

I'm re-writing the above script below:

`<?php 
//session_start();
  // Define database connection constants
  define('DB_HOST', 'localhost');
  define('DB_USER', 'root');
  define('DB_PASSWORD', '');
  define('DB_NAME', 'moby_thesaurus');

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>compare_words</title>
<!-- TinyMCE -->
<script type="text/javascript" src="jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	mode : "textareas",
	theme : "advanced",
	plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

	// Theme options
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,

	// Example content CSS (should be your site CSS)
	content_css : "css/content.css",

	// Drop lists for link/image/media/template dialogs
	template_external_list_url : "lists/template_list.js",
	external_link_list_url : "lists/link_list.js",
	external_image_list_url : "lists/image_list.js",
	media_external_list_url : "lists/media_list.js",

	// Style formats
	style_formats : [
		{title : 'Bold text', inline : 'b'},
		{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
		{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
		{title : 'Example 1', inline : 'span', classes : 'example1'},
		{title : 'Example 2', inline : 'span', classes : 'example2'},
		{title : 'Table styles'},
		{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
	],

	// Replace values for the template plugin
	template_replace_values : {
		username : "Some User",
		staffid : "991234"
	}
});
</script>
<!-- /TinyMCE -->
<!-- ckeditor -->
<script type="text/javascript" src="../ckeditor.js"></script>
<script src="sample.js" type="text/javascript"></script>
<link href="sample.css" rel="stylesheet" type="text/css" />
<!-- /ckeditor -->
</head>

<body>
<table><p> PLEASE ENTER INITIAL SENTENCE </p>
<form name="sentences" method="get" action="" >
    	<tr>
        	<td>            
            	<textarea name="sentence" id="elm1"></textarea>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Create_Spinable Content" />
            </td>
        </tr>
    </form>	
</table>
<?php
if (isset($_GET['submit'])) {
$sentence = $_GET['sentence'];

// break $sentence using the space character as the delimiter
$words = explode(' ', $sentence);

//tell the amount of words using the size of the array
echo 'The sentence has ' . count($words) . ' words.<br />';

// loop through and print all the words
for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";

//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';

//$words=$_GET['words'];
$sel="select * from synonyms where synonym LIKE '%$words[$i]%' limit 3";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);


if (!$data) {
die('<p>Error Retrieving<br/>'.
'Error: ' .mysql_error() . '</p>');}

$datalist=mysqli_fetch_assoc($data);
$synonym=$datalist['synonym'];

?>
<table><p> SPINABLE CONTENT </p>
<form name="sentences" method="get" action="" >

	<tr>
        	<td>            
             <?php
  if ($datalist=mysqli_fetch_array($data)) {
do{
?>
            	<textarea cols="80" id="editor1" name="sentence" rows="10"><?php echo ("$words[$i] {{$synonym}}||{{$synonym}}||{{$synonym}}"); ?></textarea>
                <?php

}
while ($datalist=mysqli_fetch_array($data));
echo("</table> </p>");
}
?>
<?php
}
}
?>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Spin This Now" />
            </td>
        </tr>
    </form>	
</table>

</body>
</html>

 

Its not giving out any error perse. The problem is its not doing what I want which is:

1. Displaying each piece of the words with on 3 results from the database of synonyms like:

The boy is jumping

 

The boy($words[$i]) {synonymn1|synonym2|synonym3} is($words[$i])  {synonymn1|synonym2|synonym3} jumping($words[$i])  {synonymn1|synonym2|synonym3}

 

2. This should be outputed in the 2nd texarea. But the code is replicating the 2nd textarea which is not what I really want.

 

2. The above

 

 

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.