Déjà vu
Déjà vu

Reputation: 783

Looping through every record from database

I've searched on the internet for this, and questions here on SO but mostly are using something else than PHP. So i will ask my own question.

What i need to do is make a button that will print out (read literally print, as in downloading a word document with all details on it) every record from the database.

The ID is named 'abstract_id'.

What i'm going to show you next is the print button from all pages, what i mean by this is if you click on said button it will print everything from that specific page:

$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>";

Now, this works fine, but i need a same working button which does almost the same, but instead of printing out 1 record it should print all. So all i basically need is a foreach or while loop that goes through every abstract_id.

It's been ages since i last programmed php, so i appreciate any help!

If you need any clarification, feel free to ask.

Upvotes: 0

Views: 78

Answers (1)

Pierre Granger
Pierre Granger

Reputation: 2010

First you don't need your 30 lines of mysql_result, just use mysql_fetch_assoc to get all values in associative array. Then you just have to do a while ( $line = mysql_fetch_assoc($query) ), see above :

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>";

Upvotes: 1

Related Questions