Jump to content

[SOLVED] looping and ading


makka

Recommended Posts

well hello. i would like to know if you can help me with something i have a variable that ill call variableA and i want a loop or something like that to try and reconise it so it will loop through trying to match it eg

the following code is totally wrong just explain what i want

$variableA = "hello"
if (variableA == a) { echo "hello"; }
elseif (variableA == b) { echo "matched"; }
elseif (variableA == c) { echo "matched"; }
elseif (variableA == d) { echo "matched"; }
elseif (variableA == e) { echo "matched"; }
elseif (variableA == f) { echo "matched"; }
elseif (variableA == g) { echo "matched"; }
elseif (variableA == h) { echo "matched"; }
elseif (variableA == i) { echo "matched"; }
// so on untill z then it woulc be like
elseif (variableA == aa) { echo "matched"; }
elseif (variableA == bb) { echo "matched"; }
//so on an on an on

 

get what i want? how can i do it? its for a guessing game

Link to comment
Share on other sites

<?php
$answerArray = array("a" => "A!", "b" => "B!", "c" => "C!", "d" => "D!");
$variableA = "c";

foreach ($answerArray as $selection => $answer) {
      if ($selection == $variableA) {
            print $answer;
            break;
      }
}
?>

 

Something like  that would work.

Link to comment
Share on other sites

<?php
$answerArray = array("a" => "A!", "b" => "B!", "c" => "C!", "d" => "D!");
$variableA = "c";

foreach ($answerArray as $selection => $answer) {
      if ($selection == $variableA) {
            print $answer;
            break;
      }
}
?>

 

Something like  that would work.

and with this it just gets the first letter what is the ! for

"a" => "A!"

Link to comment
Share on other sites

The ! is nothing, that is the value you want to print out.

 

It could of been this:

 

<?php
$answerArray = array("a" => "You selected A!", "b" => "You selected B!", "c" => "You selected C!", "d" => "You selected D!");
$variableA = "c";

foreach ($answerArray as $selection => $answer) {
      if ($selection == $variableA) {
            print $answer; // prints You selected C!
            break; // remove this if you want the loop to go through every element of the array.
      }
}
?>

Link to comment
Share on other sites

I think you are missing the point of the array.

 

www.php.net/array

 

The array seen above is considered an associative array. The first part IE: "a" =>  is the KEY of the array, meaning you can acesses the second part or the value "You selected A!" by calling $array["a"]  and that will print out "You selected A!"

 

So if $variableA = "a"; it will print out "You selected A!" using the loop I created above.

 

If $variableA = "c"; it will print out "You selected C!";

 

It works the same as the following:

<?php
$answerArray = array("a" => "You selected A!", "b" => "You selected B!", "c" => "You selected C!", "d" => "You selected D!");
$variableA = "c";
print $answerArray[$variableA]; // Prints "You selected C!" since the index of "c" value is "You Selected C!";
?>

 

I would highly suggest you read up on arrays and how they are used.

Link to comment
Share on other sites

Maybe you are looking for something similiar to this ???

 

<?php
$mainArticle = 'Computer programming is one of the best for certain php languages that are not of this world! that';

$articles[0] = 'This is a topic that is totally not related to the first article at all!';
$articles[1] = 'Programming in PHP has done miraculous wonders to this time and many other exciting events!';
$articles[2] = 'Programming This is a topic that is totally not related to the first article at all';
$articles[3] = 'Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all';
$articles[4] = 'Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages';
$articles[5] = 'Jack and jill went up the hill to fetch a pail of water, jack fell down and broke his crown and jill came tumbling after!';

$related = relatedTest($mainArticle, $articles);
print "The following articles are related to : " . $mainArticle . " (ordered by most revlevant)<br /><br />";
foreach ($related as $key => $matches) {
print "Article: " . $articles[$key] . "<br />";
}

print "<br /><br /><br />These were all the articles used.<br /><br />";

foreach ($articles as $article) {
print $article . "<br />";
}

function relatedTest($mainArticle, $articles) {
$mainArticle = stripCommons($mainArticle);
$words = explode(" ", $mainArticle);

foreach ($articles as $key => $article) {
	$artWords[$key] = explode(" ", stripCommons($article));

	$matches = compareWords($words, $artWords[$key]);

	if ($matches > 0) {
		$match[$key] = $matches;
	}else {
		unset($artWords[$key]);
	}
}
arsort($match);
return $match;
}

function compareWords($words, $compwords) {
$match = 0;
if (is_array($words)) {
	foreach ($words as $word) {
		foreach ($compwords as $compword) {
			if (strtolower($compword) == strtolower($word)) {
				$match++;
			}
		}
	}
}

return $match;
}

function stripCommons($article) {
$article = ereg_replace("'|\.|\?|!|,|\"|&|:|-|\[|\]|\(|\)|\+|=|~|\||\*|\^|%|\$|@|#|<|>|`|;|_|\{|\}", "", $article);
$article = " " . $article . " ";
$commonWords = array("if", "u", "so", "it", "its", "is", "of", 
					"or", "by", "on", "but", "a", "was", "for", "it", 
						"this", "was", "to", "are", "can", "you", "your", 
						"any", "or", "the", "with", "this", "not", "at", "and", "that");
$commonWords = strlenSort($commonWords);	

foreach ($commonWords as $word) {
	if (eregi(" ".$word." ", $article)) {
		$article = str_replace(" ".$word." ", " ", $article);
	}
}

return trim($article);
}

function strlenSort($array) {
// sort array by string length
foreach ($array as $key => $size) {
	$newArray[$key] = strlen($size);
}
arsort($newArray, SORT_NUMERIC);

$i=0;
foreach ($newArray as $key => $size) {
	$returnArr[$i++] = $array[$key];
}

return $returnArr;
}
?>

 

 

Which when ran would produce:

 

he following articles are related to : Computer programming is one of the best for certain php languages that are not of this world! that (ordered by most revlevant)

Article: Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages
Article: Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all
Article: Programming in PHP has done miracuouls wonders to this time and many other exciting events!
Article: Programming This is a topic that is totally not related to the first article at all



These were all the articles used.

This is a topic that is totally not related to the first article at all!
Programming in PHP has done miracuouls wonders to this time and many other exciting events!
Programming This is a topic that is totally not related to the first article at all
Programming in Computer language PHP has done This is a topic that is totally not related to the first article at all
Certainly this is not a computer topic This is a topic that is totally not related to the first article at all or is it world of languages
Jack and jill went up the hill to fetch a pail of water, jack fell down and broke his crown and jill came tumbling after!

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.