Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9029319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:13:23+00:00 2026-06-16T07:13:23+00:00

What I want to achieve is to make a webpage which every time generates

  • 0

What I want to achieve is to make a webpage which every time generates different content from MySQL DB. But when I use the rand() function, some options can repeat. So what I want to do is make rand() function with dynamic array "exceptions" that updates every time when the webpage content is generated, so every option is displayed to user only once.

Let’s say I have 5 different options:
1,2,3,4,5

When the rand() function chooses 3 next time there will be no chance for getting 3 as a result..

function randWithout($from, $to, array $exceptions) {
sort($exceptions); 
$number = rand($from, $to - count($exceptions));
foreach ($exceptions as $exception) {
    if ($number >= $exception) {
        $number++; 
    } else  {
        break;
    }
}

    return $number;
}

    $exceptions = array("3","4");
    $random = randWithout(1, $num_rows, $exceptions);

This does what I want, but I want the array "$exceptions" to update every time.

Is there a way to do this by using sessions or some other options? I don’t want to use another MySQL table. I want it to be fast and simple.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:13:24+00:00Added an answer on June 16, 2026 at 7:13 am

    Use Sessions. Sessions are used for data persistence. start your php file like this:

    <?php session_start();
    //your code
    

    Save your exceptions in a session variable :

    $_SESSION['exceptions']='exceptions Array';
    

    Every time user visits a page add that to the session.

    suppose you want to add ‘5’ to it.

    Hers updated code

    <?php session_start();
    function randWithout($from, $to) {
    global $exceptions;
    sort($exceptions); 
    $number = rand($from, $to - count($exceptions));
    foreach ($exceptions as $exception) {
        if ($number >= $exception) {
            $number++; 
        } else  {
            break;
        }
    }
    
        return $number;
    }
    
    if(isset($_SEEION['exceptions'])){
    $exceptions =$_SESSION['exceptions'];
    }
    
    $random = randWithout(1, $num_rows);
    $exceptions[]=$random;
    $_SESSION['exceptions']=$exceptions;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a calculator and i have learnt from many tutorials but
What I want to achieve I'm trying to make a layout equal to the
I'm trying to make a request/reply section in my project. I want to achieve
I want to achieve something relatively simple: I want to retrieve all objects from
I want to achieve hide/show with div's in html but in this way. Here
I want to achieve the effect of a 2D image I have but a
It's easy to explain what I want to achieve, but for me (a novice
When i write some content in div , It shows like But I want
I want to make a GridView in java, I don't want to use an
What I want to achieve is to make links on my site move 1px

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.