Topic: How can we avoid repeating it.

helo

pleas help me.

i have some question about this code.

we have a program that it Make 4 Column and 10 row.(From the database as a random)

but the data Entered can be repeated.

How can we avoid repeating it.

For example,

11-32-45-28
56-12-11-99
80-13-20-21
...

Number 11 is repeated



i will be glad if you answer to me.

tanks.

<?php
  include("config.php");

  mysql_connect($db_server, $db_username, $db_password);
  mysql_select_db($db_name);

  $query = "SELECT COUNT(ID) FROM ads ";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  $adcnt = $row[0];

  $query = "SELECT * FROM ads ";
  $result = mysql_query($query);
  $i = 1;
  while (($row = mysql_fetch_array($result)))
    {
    $rows[$i] = $row;
    $i++;
    }

  $ids_arr     = null;

    for ($i=1; $i<=10; $i++)
    {
    $ids      = null;

    for ($j=1; $j<=4; $j++)
      {
      $cellno = ($i-1)*4+$j;

        srand((double)microtime()*1000000);
        $adno = mt_rand(1, ($adcnt));
        $ids[$j]     = $rows[$adno]['ID'];
        }

      $ids_arr[$i]     = $ids;
    }
    print( $ids_arr[1][1]);
    print("-");
    print( $ids_arr[1][2]);
    print("-");
    print( $ids_arr[1][3]);
    print("-");
    print( $ids_arr[1][4]);
    echo("<br>");
    print( $ids_arr[2][1]);
    print("-");
    print( $ids_arr[2][2]);
    print("-");
    print( $ids_arr[2][3]);
    print("-");
    print( $ids_arr[2][4]);
    echo("<br>");
    print( $ids_arr[3][1]);
    print("-");
    print( $ids_arr[3][2]);
    print("-");
    print( $ids_arr[3][3]);
    print("-");
    print( $ids_arr[3][4]);
    echo("<br>");
    print( $ids_arr[4][1]);
    print("-");
    print( $ids_arr[4][2]);
    print("-");
    print( $ids_arr[4][3]);
    print("-");
    print( $ids_arr[4][4]);
    echo("<br>");
    print( $ids_arr[5][1]);
    print("-");
    print( $ids_arr[5][2]);
    print("-");
    print( $ids_arr[5][3]);
    print("-");
    print( $ids_arr[5][4]);
    echo("<br>");
    print( $ids_arr[6][1]);
    print("-");
    print( $ids_arr[6][2]);
    print("-");
    print( $ids_arr[6][3]);
    print("-");
    print( $ids_arr[6][4]);


?>

Re: How can we avoid repeating it.

One way to do it would be to build an array of "used" random values as the table is built. If the random number that is generated matches an already used value, it will loop until a unique value is found.  Here's a quick example:

(There may be "better" ways to do this... this is the one that seemed the simplest and most logical)

<?php

    $rows = 5;
    $cols = 5;
    
    echo "<table>";
    for ($i=1; $i <= $rows; $i++) // loop for the number of rows specified
    {
        echo "<tr>";
        for ($j=1; $j <= $cols; $j++) // loop for the number of columns specified
        {
            echo "<td>";
            
            // generate random number between 1, 100
            $rand = mt_rand(1, 100); 
            
            // if num has been already used...
            if (isset($duplicates[$rand]))
            {
                // loop until a unique num is found
                while (isset($duplicates[$rand]))
                {
                    $rand = mt_rand(1, 100); 
                }
                
                // once unique num is found, echo num and add to array of used nums
                $duplicates[$rand] = true;
                echo $rand;
            }   
            // if num hasn't been used, echo num and add to an array of used nums
            else
            {
                $duplicates[$rand] = true;
                echo $rand;
            }    
            
            echo "</td>";
        }
        echo "<tr/>";
    }
    echo "</table>";

?>