Forum Moderators: coopster
I'm trying to create some variables in PHP, randomly pick six different pairs, check to see if a specific value in any of the pairs is equal to another variable, and if no: output first five, and if yes: output all of the six values except for the matching one (still five pairs).
I don't really know how to approach this project, and was hoping for some advice in a fast, clean way I can tackle this.
This is what I have so far.
PSUEDOCODE
$var[0]="CNet,www.cnet.com";
$var[1]="Google,www.google.com";
$var[2]="Ebay,www.ebay.com";
$var[3]="Microsoft,www.microsoft.com";
$var[4]="Yahoo,www.yahoo.com";
$var[5]="WebmasterWorld,www.webmasterworld.com";
$var[6]="NPR,www.npr.org";
Randomly picks six pairs (5,2,3,6,4,0)
Checks to see if any of selected values equals "Yahoo" in the first part of the set.
Since 4 does, outputs all but "4":
<a href="Part 2">Part 1</a>...
ie
<a href="http://www.webmasterworld.com">WebmasterWorld</a>
<a href="http://www.ebay.com">Ebay</a>
<a href="http://www.microsoft.com">Microsoft</a>
<a href="http://www.npr.org">NPR</a>
<a href="http://www.cnet.com">CNet</a>
Thanks for any help
The best way to approach this is to use the rand() variable within PHP. The syntax is as follows...
int rand ( [int min, int max] );
I would use this like this....
$var[0]="CNet,www.cnet.com";
$var[1]="Google,www.google.com";
$var[2]="Ebay,www.ebay.com";
$var[3]="Microsoft,www.microsoft.com";
$var[4]="Yahoo,www.yahoo.com";
$var[5]="WebmasterWorld,www.webmasterworld.com";
$var[6]="NPR,www.npr.org";
for($index = 0; index < 7; index++)
{
$rand = rand(0,6);
$check = ($rand - $index);
if($check == 0) {
print $var[$index]; }
}
This should work...get back to me if you need any more help.
for($index = 0; $index < 7; $index++)
{
$rand = rand(0,6);
$check = ($rand - $index);
if($check == 0) {
print $var[$index]; }
}
Sorry about that...i wasn't paying attention
$var[0]="CNet,www.cnet.com";
$var[1]="Google,www.google.com";
$var[2]="Ebay,www.ebay.com";
$var[3]="Microsoft,www.microsoft.com";
$var[4]="Yahoo,www.yahoo.com";
$var[5]="WebmasterWorld,www.webmasterworld.com";
$var[6]="NPR,www.npr.org";
$first_match = -1;
for($index = 0; index < 7; index++)
{
$rand = rand(0,6);
$check = ($rand - $index);
if($check == 0) {
$index = 8;
$first_match = $index; }
}
if($first_match == -1)
{
for($index =0; $index < 7; $index++)
{
print $var[$index];
}
}
else if($first_match > -1)
{
for($index = 0; $index < 7; $index++)
{
if($index!= $first_match)
print $var[$index];
}
}
Yea....something like that....you might have to mess around with the numbers in the for statements to get what you want...
Sorry about all of that.........yea.....stupid me....
Funny thing is i didn't check again...oh well
Try this method that uses shuffle... You can copy and paste this entire code block to see it in effect.
<?php$var[] = array("CNet","www.cnet.com");
$var[] = array("Google","www.google.com");
$var[] = array("Ebay","www.ebay.com");
$var[] = array("Microsoft","www.microsoft.com");
$var[] = array("Yahoo","www.yahoo.com");
$var[] = array("WebmasterWorld","www.webmasterworld.com");
$var[] = array("NPR","www.npr.org");$currentVar = 'Yahoo';
$maximum = 4;echo '<p>Original Order:<br>';
print_r($var);shuffle($var);
echo '</p><p>Shuffled Order:<br>';
print_r($var);echo '</p><ol>';
$counter = 0;
$count_var = count($var);
for($i=0; $i<$count_var; $i++){
if($var[$i][0]!=$currentVar){
echo '<li><a href="'.$var[$i][0].'">'.$var[$i][1].'</a></li>';
$counter++;
}
if($counter>=$maximum){ break; }
}
echo '</ol>';?>
Creating/modifying with square-bracket syntaxYou can also modify an existing array by explicitly setting values in it.
This is done by assigning values to the array while specifying the key in brackets. You can also omit the key, add an empty pair of brackets ("[]") to the variable name in that case.
...
Basically, it adds a new item to the end of the array. But the linked page has some examples of how it isn't that simple.
So now that I have a chance, I'm finessing the code, especially after taking a look at the link provided in the post above, and learning a bit about the unset command.
I'm wondering if it might be better to unset one of the array values before the shuffle, if one matches a specified variable, instead of having incorporating it into the loop.
Something like this:
<ul>
<?php
$var[]=array("A","/1/");
$var[]=array("B","/2/");
$var[]=array("C","/3/");
$var[]=array("D","/4/");
$var[]=array("E","/5/");
$var[]=array("F","/6/");
$var[]=array("G","/7/");
unset($var[][1]=substr($PHP_SELF,0,-9));
shuffle($var);
$counter=0;
$count_var=count($var);
for($i=0; $i<$count_var; $i++){
echo '<li><a href="'.$var[$i][1].'">'.$var[$i][0].'</a></li>';
$counter++;
if($counter>=4){break;}
}
?>
</ul>
Would somebody know if this would be a smart move, or even if something like "unset($var[][1]=substr($PHP_SELF,0,-9));" could even be written?
Thanks so much!