How do you choose the data from the mass?



  • There's a violin that fills up a database of data from the mass. Now the data from the mass is added by accident, how can the violin be changed so that the data can be added consecutively (One, two, three,..., one, two, two... ♪ ♪ ... ?

    Php:

    if(100==100){
    

    $rowsToCreate = 30;

    $name = array("Один", "Два", "Три", "Четыре", "Пять");
    $text = array("Текст 1", "Текст 2", "Текст 3", "Текст 4", "Текст 5");

    do {
    $name2 = $name[array_rand($name)];
    $text2 = $text[array_rand($text)];

    $data = "INSERT INTO `blog` (id, name, text) VALUES (NULL, '".$name2."', ' ".$text2."' ) "; 
    
    $Result = mysql_query($data);
    $rowsToCreate--;
    

    } while ($rowsToCreate>=1);

    if ($Result) {
        echo "<h3>Информация добавлена!!</h3>";
    }else{
        echo "<h3>Увы, но информация не добавлена!!</h3>";
    }
    

    }

    Bd:

    CREATE TABLE IF NOT EXISTS blog (
    id int(10) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    text varchar(100) NOT NULL,
    PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;



  • Is that how it works?

    $inArrayCnt = 0; // здесь будем хранить порядковый номер записи из массива
    do {
        $name2 = $name[$inArrayCnt];
        $text2 = $text[$inArrayCnt];
    
    $data = "INSERT INTO `blog` (id, name, text) VALUES (NULL, '".$name2."', ' ".$text2."' ) "; 
    
    $Result = mysql_query($data);
    $rowsToCreate--;
    $inArrayCnt++; // увеличиваем порядковы номер
    if ($inArrayCnt==Count($name)) $inArrayCnt=0; // если новый номер последний - обнуляем счетчик
    

    } while ($rowsToCreate>=1);


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2