如何在表中插入行的章节数

时间:2017-04-08 17:36:52

标签: php mysql

我在这里有问题表我有584行,即584个问题,本章节表中的章节表有类型栏,我为不同类型添加了章节,即类型1的18章和类型2和18的18章类型3的章节,因此章节的id是不同的。

现在我在类型1的问题表中添加了584个问题,现在这些584个问题我想在同一个问题表中添加但是对于类型2和问题表有chapter_id列,所以chapter_id是80到97.所以对于每章如果我们用章节号划分584,即18则结果为32,所以对于每章32个问题将添加章节id,如前32章章节id将为80,另一章章节id将为81等等

但是18 * 32 = 576和584-576 = 08,所以根据我的逻辑,8个问题不会在表格中插入,但如果我使用foreach章节id变量并且我没有得到增量。

    <?php

require 'Database.php';


$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');

$sth = $dbh->prepare("SELECT * FROM `questions`");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();


$chapterId = 80;
$i = 0;
foreach ($result as $chapter) {

    while ($i <= 32)
    {
        $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
        $stmt->execute(array($result['question'], $result['answer_a'], $result['answer_b'], $result['answer_c'], $result['answer_d'], $result['answer'], 2, $chapterId));
    }
    $i++;
    $chapterId ++;
}

?>

Chapter table :

id
title
type

33
Ch 1: Air Force heritage
1

34
Ch 2: Enlisted History
1

35
Ch 3: Organization
1

36
Ch 4: Air Force doctrine, AEF, Joint force
1

37
Ch 5: Emergency management
1

38
Ch 6: Standards of conduct
1

39
Ch 7: Enforcing standards and legal issues
1

40
Ch 8: Military customs, courtesies, and protocol f...
1

41
Ch 9: Enlisted force development
1

42
Ch 10: Leadership
1

43
Ch 12: Training and education
1

44
Ch 13: Resource management
1

45
Ch 14: Communicating in today's Air Force
1

46
Ch 15: Personnel programs
1

47
Ch 16: Wing support
1

48
Ch 17: Dress and appearance
1

49
Ch 18: Fit force
1

50
Ch 19: Security
1

80
Ch 1: Air Force heritage
2

81
Ch 2: Enlisted History
2

82
Ch 3: Organization
2

83
Ch 4: Air Force doctrine, AEF, Joint force
2

84
Ch 5: Emergency management
2

85
Ch 6: Standards of conduct
2

86
Ch 7: Enforcing standards and legal issues
2

87
Ch 8: Military customs, courtesies, and protocol f...
2

88
Ch 9: Enlisted force development
2

89
Ch 10: Leadership
2

90
Ch 12: Training and education
2

91
Ch 13: Resource management
2

92
Ch 14: Communicating in today's Air Force
2

93
Ch 15: Personnel programs
2

94
Ch 16: Wing support
2

95
Ch 17: Dress and appearance
2

96
Ch 18: Fit force
2

97
Ch 19: Security
2

98
Ch 1: Air Force heritage
3

99
Ch 2: Enlisted History
3

100
Ch 3: Organization
3

101
Ch 4: Air Force doctrine, AEF, Joint force
3

102
Ch 5: Emergency management
3

103
Ch 6: Standards of conduct
3

104
Ch 7: Enforcing standards and legal issues
3

105
Ch 8: Military customs, courtesies, and protocol f...
3

106
Ch 9: Enlisted force development
3

107
Ch 10: Leadership
3

108
Ch 12: Training and education
3

109
Ch 13: Resource management
3

110
Ch 14: Communicating in today's Air Force
3

111
Ch 15: Personnel programs
3

112
Ch 16: Wing support
3

113
Ch 17: Dress and appearance
3

114
Ch 18: Fit force
3

115
Ch 19: Security
3


Questions :


id
question
answer_a
answer_b
answer_c
answer_d
answer
type
chapterId


56
_______ achieved the first powered, sustained, con...
Otto Lilienthal
Orville and Wilbur Wright
The Tuskegee Airmen
Lieutenant Thomas E. Selfridge
B
1
24


57
_______ achieved the first powered, sustained, con...
Otto Lilienthal
Orville and Wilbur Wright
The Tuskegee Airmen
Lieutenant Thomas E. Selfridge
B
1
33


58
The U.S. and ___________ signed the Anti-Ballistic...
Iran
North Korea
the Union of Soviet Socialist Republics
Vietnam War
C
1
33


59
The purpose of Operation Desert Shield was to: (Ch...
defend Saudi Arabia and Persian Gulf states
respond to the attack on the U.S. Embassy in Iran
avoid a repetition of the Iranian hostage crisis
all of the above
A
1
24

编辑:

require 'Database.php';

ini_set('max_execution_time', 300);

$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');

$sth = $dbh->prepare("SELECT * FROM `questions`");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();

$chapterId = 80;
$i = 0;

foreach ($result as $chapter) {

    while ($i <= 32)
    {
        $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
        $stmt->execute(array($result['question'], $result['answer_a'], $result['answer_b'], $result['answer_c'], $result['answer_d'], $result['answer'], 2, $chapterId));

     //   $count = $stmt->rowCount();

        if($stmt)
        {
            echo 'row inserted';
        }
    }
    $i++;
    if($i > 32)
    {
        $i = 0;
    }
    $chapterId ++;
    if($chapterId > 91)
    {
        $chapterId = 80;
    }
}

?>

我能为此做些什么?请帮忙谢谢。

1 个答案:

答案 0 :(得分:1)

经过讨论,以下内容将复制问题,并为每一章分配一个(大致)相同的数字。

require 'Database.php';

ini_set('max_execution_time', 300);

$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook','siddhi', 'siddhi');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare("SELECT * FROM `questions` WHERE `type` = 1 ORDER BY `id`;");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $sth->rowCount();

$StartChapterId = 79;   //  Will update first through foreach, so it should be 1 less than the 1st ID.
$NumChapters = 18;
$questionsPerChapter = ceil(584 / $NumChapters);
$chapterId = 0;
$num_inserted = 0;

foreach($result as $rowno => $chapter) {
    if($rowno % $questionsPerChapter === 0) {
        $chapterId++;
        echo "<li>".$num_inserted." inserted in chapterId: ".$chapterId + $StartChapterId."</li>\n";
        $num_inserted = 0;
    }
    $stmt = $dbh->prepare("INSERT INTO questions (question,answer_a,answer_b,answer_c,answer_d,answer,type,chapterId) VALUES (?, ?, ?, ?, ?, ?, ?,?)");
    $stmt->execute(array($chapter['question'], $chapter['answer_a'], $chapter['answer_b'], $chapter['answer_c'], $chapter['answer_d'], $chapter['answer'], 2, $chapterId + $StartChapterId));
    $num_inserted++;
}
echo "<li>".$num_inserted." inserted in chapterId: ".$chapterId + $StartChapterId."</li>\n";

作为替代方案,这里是一个SQL语句,它将根据章节表中的内容进行复制。

这是一个SQL语句,应该复制问题,并根据您的要求更改类型和chapterId。

INSERT INTO `questions`
(`question`,
    `answer_a`,
    `answer_b`,
    `answer_c`,
    `answer_d`,
    `answer`,
    `type`,
    `chapterId`)
SELECT `question`,
    `answer_a`,
    `answer_b`,
    `answer_c`,
    `answer_d`,
    `answer`,
    c2.`type` as `type`,
    c2.`id` as `chapterId`
FROM `questions` q
JOIN `chapters` c1
ON c1.`id` = q.`chapterId` AND c1.`type` = 1
JOIN `chapters` c2
ON c2.`title` = c1.`title` AND c2.`id` <> c1.`id`
WHERE q.`type` = 1
ORDER BY c2.`type`, c2.`id`, q.`id`;