Page 1 of 1

Help needed with simple php

Posted: Fri Oct 10, 2008 3:54 am
by MikeBlade
Hey everyone,

Basically I am a novice PHP user but I am trying to get into it. I have created a simple script which creates a list of words after running the entered word through a PHP class (the class generates words related to the word typed in). So here is what I have:

Code: Select all

<form action="index.php" method="post">
Enter Your Word: <input type="text" name="word" />
<input type="submit" value="Submit" name="submit"/>
</form>
 
<?php
 
include("relatedCLASS.php");
 
if(isset($_POST['submit'])) {
 
$word =  trim($_POST["word"]);
$related = array();
$related = crelated::getAllRelated( $word );
 
foreach($related as $word) {
echo $word . "<br />";
 
}
}
?>
Now what I want to do is to add the list of words that the above script outputs into another variable ($list). How would I go about getting this done? I am thinking this would be something extremely simple but I just can't get it :banghead:. Help would be much appreciated!

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 4:12 am
by mattcooper
You already have the list of words in the '$related' array that you are iterating over to produce your html output. What exactly are you trying to do? Your question seems to suggest that you simply need to do this:

Code: Select all

 
    $list = array();
    $list = crelated::getAllRelated( $word );
 
That what you're getting at?

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 4:48 am
by MikeBlade
Thanks for the reply. Ok what I actually want to do is as follows. I don't want to output the list of words to html I want to use them within another script . This other script is using a variable which contains ($query= 'item') I want to replace the 'item' with the generated list that my script outputs, this list changes depending on the word input to my form. If that makes sense lol

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 5:00 am
by papa

Code: Select all

 
foreach($related as $word) {
$list[] = $word;
}
 
This is not what you mean? :)

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 5:00 am
by mattcooper
MikeBlade wrote:Thanks for the reply. Ok what I actually want to do is as follows. I don't want to output the list of words to html I want to use them within another script . This other script is using a variable which contains ($query= 'item') I want to replace the 'item' with the generated list that my script outputs, this list changes depending on the word input to my form. If that makes sense lol
OK, so I am assuming you want perhaps a comma-separated string comprising the output of your class.

Code: Select all

 
    $list = implode (', ', $related);
 
Will give you "word1, word2, word3". I would recommend, however, that if you're storing words that relate directly to another word that you use a properly relational approach and store each word in $related as a separate row in the database with an identifier, but that's up to you ;)

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 5:12 am
by mattcooper
papa wrote:

Code: Select all

 
foreach($related as $word) {
$list[] = $word;
}
 
This is not what you mean? :)
cf: My first post on this subject, you've suggested the long way of achieving the same thing ;)

Re: Help needed with simple php

Posted: Fri Oct 10, 2008 5:16 am
by papa
mattcooper wrote:
papa wrote:

Code: Select all

 
foreach($related as $word) {
$list[] = $word;
}
 
This is not what you mean? :)
cf: My first post on this subject, you've suggested the long way of achieving the same thing ;)
Hehe sorry. Just woke up so didn't really thought it through i guess. ;)