循环遍历数据库中的每条记录

时间:2014-03-24 14:15:19

标签: php mysql

我已经在互联网上搜索了这个问题,并在这里提出了问题,但主要是使用除了PHP以外的东西。所以我会问自己的问题。

我需要做的是制作一个按钮,该按钮将从数据库中打印出每个记录的打印输出(从字面上打印,如下载包含所有详细信息的word文档)。

该ID名为'abstract_id'。

我接下来要向您展示的是所有页面上的打印按钮,我的意思是,如果您点击所述按钮,它将打印该特定页面中的所有内容:

$abstract_id = addslashes($_POST['abstract_id']);  

 //Here i connect to the database//

$query = "SELECT * FROM abstracts WHERE abstract_id = '$abstract_id'";
    $result = mysql_query($query);

    $i = 0;
    $title = mysql_result($result,$i,"title");
    $author[1] = mysql_result($result,$i,"author1");
    $organization[1] = mysql_result($result,$i,"organization1");
    $author[2] = mysql_result($result,$i,"author2");
    $organization[2] = mysql_result($result,$i,"organization2");
    $author[3] = mysql_result($result,$i,"author3");
    $organization[3] = mysql_result($result,$i,"organization3");
    $author[4] = mysql_result($result,$i,"author4");
    $organization[4] = mysql_result($result,$i,"organization4");
    $author[5] = mysql_result($result,$i,"author5");
    $organization[5] = mysql_result($result,$i,"organization5");
    $author[6] = mysql_result($result,$i,"author6");
    $organization[6] = mysql_result($result,$i,"organization6");
    $format = mysql_result($result,$i,"format");
    $language = mysql_result($result,$i,"language");
    $presenter = mysql_result($result,$i,"presenter");
    $background = mysql_result($result,$i,"background");
    $purpose = mysql_result($result,$i,"purpose");
    $methods = mysql_result($result,$i,"methods");
    $findings = mysql_result($result,$i,"findings");
    $conclusion = mysql_result($result,$i,"conclusion");
    $word_count = mysql_result($result,$i,"word_count");
    $name = mysql_result($result,$i,"name");
    $email1 = mysql_result($result,$i,"email1");
    $email2 = mysql_result($result,$i,"email2");
    $phone1 = mysql_result($result,$i,"phone1");
    $phone2 = mysql_result($result,$i,"phone2");
    $fax = mysql_result($result,$i,"fax");
    $address = mysql_result($result,$i,"address");
    $country = mysql_result($result,$i,"country");
    $topic = mysql_result($result,$i,"topic");
    $master_status = mysql_result($result, $i, "master_status");
    $last_edit = mysql_result($result,$i,"last_edit");


    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=abstract_" . $abstract_id . ".doc");

    echo "<html>";
    echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
    echo "<body>";
    echo "<p><b>PCNE Abstract</b> $abstract_id</p>";
    echo "<h1>$title</h1>";

    echo "<b>";
    for ($i = 1; $i<= 6; $i++) {
        echo ((!empty($author[$i-1]) && !empty($author[$i])) ? ", " : "") ;
        echo ((!empty($author[$i])) ? $author[$i] . "<sup>" . $i . "</sup>" : "") ;
    }
    echo ".</b>";

    echo "<br>";

    for ($i = 1; $i<= 6; $i++) {
        if (!empty($author[$i])) {
            echo (!empty($organization[$i]) && !empty($organization[$i-1])) ? ". " : "";
            echo ((!empty($organization[$i])) ? "<sup>" . $i . "</sup>"  . $organization[$i]: "") ;
        }
    }
    echo (!empty($email1)) ? " (" . $email1 . ")" : "";

    echo "<br>";
    echo "<br>";

    echo "<b>Background</b>&nbsp;&nbsp;";   
    echo "$background<br>"; 

    echo "<b>Purpose</b>&nbsp;&nbsp;";      
    echo "$purpose<br>";    

    echo "<b>Method</b>&nbsp;&nbsp;";       
    echo "$methods<br>";    

    echo "<b>Findings</b>&nbsp;&nbsp;";     
    echo "$findings<br>";   


    echo "<b>Conclusion</b>&nbsp;&nbsp;";       
    echo "$conclusion<br>"; 


    echo "</body>";
    echo "</html>";

现在,这工作正常,但我需要一个几乎相同的工作按钮,但不是打印1记录它应该打印所有。所以我基本上需要的是一个遍历每个abstract_id的foreach或while循环。

自从我上次编写php以来已经很久了,所以我感谢任何帮助!

如果您需要任何澄清,请随时提出。

1 个答案:

答案 0 :(得分:1)

首先,您不需要30行mysql_result,只需使用mysql_fetch_assoc获取关联数组中的所有值。 然后你只需要做一段时间($ line = mysql_fetch_assoc($ query)),见上文:

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=abstract_all.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";

$query = mysql_query("SELECT * FROM abstracts WHERE 1") or die(mysql_error()) ;
while ( $line = mysql_fetch_assoc($query) )
{
    echo "<p><b>PCNE Abstract</b>".$line['abstract_id']."</p>";
    echo "<h1>".$line['title']."</h1>";

    $authors = Array() ;
    for ($i = 1 ; $i <= 6 ; $i++ )
        if ( isset($line['author'.$i]) && $line['author'.$i] != '' ) $authors[] = $line['author'.$i].' <sup>'.$i.'</sup>' ;
    echo '<b>'.implode(', ',$authors).'</b>' ;

    $organizations = Array() ;
    for ($i = 1; $i<= 6; $i++)
    {
        if ( ! isset($line['author'.$i]) || $line['author'.$i] == '' ) continue ; // Check if there is an author, if no go to next loop
        if ( isset($line['organization'.$i]) && $line['organization'.$i] != '' ) $organizations[] = ' <sup>'.$i.'</sup> '.$line['organization'.$i] ;
    }
    echo '<b>'.implode(', ',$organizations).'</b>' ;

    echo "<b>Background</b>&nbsp;&nbsp;";   
    echo $line['background']."<br>"; 

    echo "<b>Purpose</b>&nbsp;&nbsp;";      
    echo $line['purpose']."<br>";    

        // ...
}

echo "</body>";
echo "</html>";