Jump to content

php explode & mysql query


tobimichigan

Recommended Posts

Good day pals,

Heres the description...

I've got a base of some common phrases..Everything is working fine but for the output.

I am using the php code function to explode the sentences of a <textarea>

The explode works and splits the sentence into fragments.

But where I'm now hooked is getting the mysqli_fetch_array to display the associated fragment words and display them. Please kindly help check this code and suggest  where I might be missing something.

 

<?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);

if (isset($_GET['submit'])) {
$sentence=$_GET['sentence'];
}
?>
<!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>Next Stage</title>
</head>

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

<table>
<form name="sentences" method="get" action="next.php?sentence=<?php echo 'sentence'; ?>" >
    	<tr>
        	<td>            
            	<textarea name="sentence" id="elm1"><?php echo $sentence; ?></textarea>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Word_Count" />
            </td>
        </tr>
    </form>	
</table>

SYNONYMN WORDS
<table width='200' border='1'>
  <tr>
    <td>S/N</td>
     <td>SENTENCE_WORD</td>
    <td>SYNONYM</td>
     <td>SYNONYM_ID</td>
  </tr></br>
<?php
$sel="select * from synonyms where synonym ='$i'";
$data=mysqli_query($dbc,$sel);
$num= mysqli_num_rows($data);

$n=1;

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

  if ($datalist=mysqli_fetch_assoc($data)) {
do{
?>
<tr>
    <td><?php echo $n; ?></td>
    <td><?php echo $words[$i]?></td>
    <td><?php echo $datalist["synonym"]?></td>
<td><?php echo $datalist["synonym_id"]?></td>

  </tr>
<?php
$n++;
}
while ($datalist=mysqli_fetch_array($data));
echo("</table> There are $num instances of the word '$synonym_word' in the base.</p>");
}
?> 
</body>
</html>

 

If you observe well I am using mysql query:

[code

<?php $sel="select * from synonyms where synonym ='$i'"; ?> [/code]

 

With the explode array variable $i.

 

But nothing outputs to the browser. Please I need suggestions...

Link to comment
Share on other sites

from what i can see, $i is only defined in your for loop

for ($i = 0; $i < count($words); $i++)
{
    echo "Piece $i = $words[$i] <br />";
//echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';
}

 

but then you use it later outside

$sel="select * from synonyms where synonym ='$i'";

 

but the problem is you are using the same variable $i for both your loop counter and the "word" so every time through the loop it will be overwritten. i am not entirely sure what you are trying to accomplish here.  If you want to run the query on every word in the sentence, then the query should be inside the for loop. actually i think all of the table generation below should also go into the for loop because you want to loop through each of the words, run the lookup for synonyms and display it in the table.

 

This is a quick reorganization of the code but i think it is going in the right direction for you

 

<?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);

if (isset($_GET['submit'])) {
$sentence=$_GET['sentence'];
}
?>
<!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>Next Stage</title>
</head>

<body>

<table>
<form name="sentences" method="get" action="next.php?sentence=<?php echo 'sentence'; ?>" >
    	<tr>
        	<td>            
            	<textarea name="sentence" id="elm1"><?php if(isset($_GET) && $_GET['sentence']!="") echo $_GET['sentence']; ?></textarea>
            </td>
        </tr>
        <tr>
        	<td>            
            	<input name="submit" type="submit" value="Word_Count" />
            </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 />';
?>
SYNONYMN WORDS
<table width='200' border='1'>
  <tr>
	<td>S/N</td>
	 <td>SENTENCE_WORD</td>
	<td>SYNONYM</td>
	 <td>SYNONYM_ID</td>
  </tr>

  <?php
// 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 />';
	$sel="select * from synonyms where synonym ='".$words[$i]."' ";
	$data=mysqli_query($dbc,$sel);
	while ($datalist=mysqli_fetch_array($data)) {
		 echo "<tr>";
		echo "<td>$n</td>";
		echo "<td>".$words[$i]."</td>";
		echo "<td>".$datalist["synonym"]."</td>";
		echo "<td>".$datalist["synonym_id"]."</td>";
	  echo "</tr>	";

	}
}
echo("</table> There are $num instances of the word '$synonym_word' in the base.</p>");
}
?>


</body>
</html>

Link to comment
Share on other sites

By I think I owe this forum an apology for being away for a while..I've actually been digging the length & breaths of various php books from oreilly.com, apress.com, wrox.com, packtpub.com, sitepoint.com, manning.com, pearsonvue just to mention a few..but not all the codes in these books are 100% bulletproof one still needs a workaround them.

This 2012 I hope to make a better contribution to this wonderful open source community.

Thanks 1 thanks all...

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.