Forum Moderators: coopster

Message Too Old, No Replies

Question about picking and outputting variables

         

Jeremy_H

8:02 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Hello,

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

eelixduppy

8:55 pm on Apr 11, 2006 (gmt 0)



Hello...

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.

eelixduppy

8:56 pm on Apr 11, 2006 (gmt 0)



I'm sorry the for statement above should look like this...

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

eelixduppy

9:06 pm on Apr 11, 2006 (gmt 0)



Now that i officially feel like an idiot, i should really check before i submit...

$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

whoisgregg

10:41 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had done some benchmarking on the difference between using rand() and shuffle() [webmasterworld.com] to randomly order an array. I'm pretty sure it applies in this situation as well. I'd use the tool that is built for arrays (shuffle) over rand.

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>';

?>

Jeremy_H

11:16 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Both of you, thank you so much! The code work's great. You two rock!

I'm just a bit curious though, what does the backet after the variable name mean?

$var[]=...

Could one not just call it:

$var=...

Thanks again!

whoisgregg

12:59 am on Apr 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From [us2.php.net...]

Creating/modifying with square-bracket syntax

You 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.

Jeremy_H

6:48 am on Apr 12, 2006 (gmt 0)

10+ Year Member



Thanks Gregg, I learned something new about the brackets and arrays.

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!