gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:06:42 25 4
gpt4 key购买 nike

我在互联网上搜索过这个问题,以及这里的问题,但大多数都使用 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 以来已经有很长时间了,所以我感谢任何帮助!

如果您需要任何说明,请随时询问。

最佳答案

首先,您不需要 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>";

关于php - 循环遍历数据库中的每条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22611913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com