php - Display same random key several times in a post

768

This code is probably not the best for click to call but it works.

I have 3 numbers that I want to random in a post. When it comes to display only once whith a unique piece of code it's OK. But when it comes to display the same random number twice lower in a post it's complicated. The displayed numbers are different.

How can I display the same random number (while refreshing the page) several times having control on this.

$numbers = array("800-433-7300", "800-633-3711", "800-756-8613");
switch ($numbers[array_rand($numbers)]) {
   case "800-433-7300":
      echo '<a href="tel:8004337300">800-433-7300</a>';
      break;
   case "800-633-3711":
      echo '<a href="tel:8006333711">800-633-3711</a>';
      break;
   default:
      echo '<a href="tel:8007568613">800-756-8613</a>';
      break;
}

I expect 800-433-7300 will be displayed in different part of the page. Next refreshing page, 800-633-3711 will be displayed as well several times and so on...

Any ideas ? Could you only suggest me a specific function, strcuture or page ressource to solve this by myself ?

Thanks

354

Answer

Solution:

Use a var to stock the value :

$numbers = array("800-433-7300", "800-633-3711", "800-756-8613");
$rand_num = $numbers[array_rand($numbers)];

Then use it where you need it in your page :

echo '<a href="tel:'.str_replace('-', '', $rand_num).'">$rand_num</a>';

Now if you always need the same string to echo, stock the string :

$numbers = array("800-433-7300", "800-633-3711", "800-756-8613");
switch ($numbers[array_rand($numbers)]) {
  case "800-433-7300":
    $rand_string = '<a href="tel:8004337300">800-433-7300</a>';
    break;
  case "800-633-3711":
    $rand_string = '<a href="tel:8006333711">800-633-3711</a>';
    break;
  default:
    $rand_string = '<a href="tel:8007568613">800-756-8613</a>';
    break;
}

And now you can echo the string where you want :

echo $rand_string;
484

Answer

Solution:

If I understand right, you want a different number to appear on each page refresh. If that is the case, I'd cycle through an array of phone numbers using cookies.

And you can use the same number for each page refreshment because it is stored in variable$phones[$phoneIndex]

If you need more numbers, just crank them up in the array.

$phones = ['800-433-7300','800-633-3711','800-756-8613'];

$phoneIndex= isset($_COOKIE['phoneIndex']) ? $_COOKIE('phoneIndex') : 0;

echo "<a href=\"$phones[$phoneIndex]\">$phones[$phoneIndex] </a>";

setcookie('phoneIndex', ($phoneIndex+1)%3);

People are also looking for solutions to the problem: PHP How to owerwrite object cast to array like: (array) $object with custom function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.