Help needed with simple php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MikeBlade
Forum Newbie
Posts: 3
Joined: Fri Oct 10, 2008 3:26 am

Help needed with simple php

Post 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!
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Help needed with simple php

Post 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?
MikeBlade
Forum Newbie
Posts: 3
Joined: Fri Oct 10, 2008 3:26 am

Re: Help needed with simple php

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Help needed with simple php

Post by papa »

Code: Select all

 
foreach($related as $word) {
$list[] = $word;
}
 
This is not what you mean? :)
Last edited by papa on Fri Oct 10, 2008 5:00 am, edited 1 time in total.
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Help needed with simple php

Post 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 ;)
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: Help needed with simple php

Post 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 ;)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Help needed with simple php

Post 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. ;)
Post Reply