PHP string with multiple variables

doctorbill
doctorbill used Ask the Experts™
on
if (isset($_POST['company'])) {
$sql = "SELECT frm_id, frm_companyname, frm_contact, frm_email, frm_rep, frm_date, frm_return, frm_timefrom, frm_timeto, frm_comment1, frm_comment2, frm_comment3, frm_comment4, frm_comment5, frm_todo1, frm_todo2, frm_todo3, frm_todo4, frm_todo5, frm_updated FROM siteform WHERE frm_companyname = '$search'";
$result = $conn->query($sql);
                
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<div class = "divechoimages">'.'<a href="'. $link .$row["frm_id"].'">Create Individual PDF</a>'.$break.'<font color="blue">' . "id:" . '<font color="purple">' .$space.$row["frm_id"]. " - Company: " . $row["frm_companyname"]. $space."Contact: " . $row["frm_contact"]. $break." email: " . $row["frm_email"]. '<font color="red">'.$break." Rep: " . $row["frm_rep"]. '<font color="green">' .$break."Date Entered: " . $row["frm_date"]. '<font color="purple">' .$break."Return Date: " . $row["frm_return"]. $break."Time From: " . $row["frm_timefrom"]. $break."Time To: " . $row["frm_timeto"]. $break."Comment1: " . $row["frm_comment1"]. $break."Comment2: " . $row["frm_comment2"]. $break."Comment3: " . $row["frm_comment3"]. $break."Comment4: " . $row["frm_comment4"]. $break."Comment5: " . $row["frm_comment5"]. $break."ToDo1: " . $row["frm_todo1"]. $break."ToDo2: " . $row["frm_todo2"]. $break."ToDo3: " . $row["frm_todo3"]. $break."ToDo4: " . $row["frm_todo41"]. $break."ToDo5: " . $row["frm_todo5"]. $break."Updated: " . $row["frm_updated"]. "<br>". "<br>".'</div>';
    }
} else {
    echo "0 results";
}}

Open in new window


This recordset produces the correct records on the page but I need to create a link from the ID  "frm_id" array in order to pass them to a webpage for generating a pdf document based on all of the id values
This page uses dom pdf and is working well for individual record values sent to it but not for multiple values. It fails to generate a pdf of multiple records
What I need is the following as an example:
webpage.php?id=[frm_id1] [frm_id2] [frm_id3] etc......

Sorry if it is not too accurate but I am just trying to convey the idea of what is needed
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Bill,

Are you wanting one DIV containing one link with all the IDs. If so, how do you know which record to get things like company_name from. Not entirely sure about your logic, but to pass an array to your link, you'll need some way to serialize it. Simple method is to just implode the array on a known character (e.g. comma):

$ids = array();

while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}

$idString = implode(",", $ids);

echo "<div class='divechoimages'><a href='webpage.php?ids={$idString}'> ... </a>";

Open in new window

Now in the webpage.php script, you will receive a comma delimited list of ids, that you an turn back into an array:

$ids = explode(",", $_GET['ids']);
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
How is this going to work?
webpage.php?id=[frm_id1] [frm_id2] [frm_id3] etc......

Open in new window

What I mean is - how do you envisage the documents (PDF) are going to be sent back?

HTTP is a request / response protoctol - you request a resource it sends it back but you sound as if you want to send back multiple resources for a single query.

In practical terms - how are you envisaging this will play out when the link is clicked?

Author

Commented:
The array url works as in produces a nice list of id numbers but  I will explain what I was expecting it to do
I need to document it properly
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
Send URL: This is the string that is sent to the "pdf_workformPDF". I know the url brings up the correct records because it works if it is sent directly to the "searchWorkFormPDF.php"
------------------------------------------------------
echo "<a href=\"pdf_workformPDF.php?company=" . $_POST['company'] ."\" target=\"_blank\">Create PDF of All Search Results.... </a>";}

Open in new window

------------------------------------------------------

pdf_workformPDF.php: This creates a pdf from the referenced php page ("searchWorkFormPDF.php")
------------------------------------------------------
<?php
require_once("/includes/pdf/dompdf_config.inc.php");

//$companyname = $row_emOID['CustName'];

$filename = time();


$html = "";
ob_start();
//include('orders_find_visit_print_custom_status_noforms_nocosts_PDF.php');
include('searchWorkFormPDF.php');

//$filename2 = $row_emOID['CustName'];
//$filename3 = $row_emOID['Date'];
$filename2 = "Company";
$filename3 = "Sites";
$html = ob_get_contents();
ob_end_clean();

$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf = set_paper("a4");

$dompdf->render();
$dompdf->stream("siteworkform-$filename2-$filename3-$filename.pdf");

?>

Open in new window

------------------------------------------------------

searchWorkFormPDF.php:
------------------------------------------------------
<?php
$search4 = $_GET['company'];

 if (isset($_GET["company"])) {
$sql = "SELECT frm_id, frm_companyname, frm_contact, frm_email, frm_rep, frm_date, frm_return, frm_timefrom, frm_timeto, frm_comment1, frm_comment2, frm_comment3, frm_comment4, frm_comment5, frm_todo1, frm_todo2, frm_todo3, frm_todo4, frm_todo5, frm_updated FROM siteform WHERE frm_companyname = '$search4'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<a href="'. $link .$row["frm_id"].'">Create Individual PDF</a>'.$break.'<font color="blue">' . "id:" . '<font color="purple">' .$space.$row["frm_id"]. " - Company: " . $row["frm_companyname"]. $space."Contact: " . $row["frm_contact"]. $break." email: " . $row["frm_email"]. '<font color="red">'.$break." Rep: " . $row["frm_rep"]. '<font color="green">' .$break."Date Entered: " . $row["frm_date"]. '<font color="purple">' .$break."Return Date: " . $row["frm_return"]. $break."Time From: " . $row["frm_timefrom"]. $break."Time To: " . $row["frm_timeto"]. $break."Comment1: " . $row["frm_comment1"]. $break."Comment2: " . $row["frm_comment2"]. $break."Comment3: " . $row["frm_comment3"]. $break."Comment4: " . $row["frm_comment4"]. $break."Comment5: " . $row["frm_comment5"]. $break."ToDo1: " . $row["frm_todo1"]. $break."ToDo2: " . $row["frm_todo2"]. $break."ToDo3: " . $row["frm_todo3"]. $break."ToDo4: " . $row["frm_todo41"]. $break."ToDo5: " . $row["frm_todo5"]. $break."Updated: " . $row["frm_updated"]. "<br>". "<br>";
    }
} else {
    echo "0 results";
}
            }

?>

Open in new window

------------------------------------------------------

What I am trying to achieve:
The idea of the url with multiple id's was to see if the "searchWorkFormPDF.php" page required this to generate a pdf of multiple records. At present it only generates a pdf if one record company id is sent to it. If multplle records are referenced it just hangs.

Author

Commented:
I hope it is not too confusing
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Bill,

It's not making a lot of sense. It looks from the code above that you're passing a 'company' to your PDF generation script, and that script is creating a PDF that contains a list of <a> tags, based on the company.

If you want to pass a list of IDs and use that in the search instead, then you can use the IN statement in your WHERE clause. For example;

$ids = implode(',' array(1,2,3,4,5));
$sql = "SELECT ..... FROM siteform WHERE frm_id IN ( {$ids} )";

Open in new window

It takes an array of IDs and implodes it on the comma. The IN syntax looks like this:

SELECT * FROM someTable WHERE someColumn IN (1,2,3,4,5);

That would retrieve all records where the ID matches the list of IDs.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
So are you basically wanting a multipage report that does what the single report page does N times once for each ID that is passed to it and sends back a SINGLE PDF with a page for each company?
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
But have to agree with Chris - the output from the PDF script seems to be a list of links?

Author

Commented:
Ah - that may well do it. I know I had this working many moons ago but I cannot locate the method I used for it. It was on another site I developed

Question 1:

How do I include this:
$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}
$idString = implode(",", $ids);

Open in new window

--------------------------------------------------
In this so that I can use it from the recordest (I managed it yesterday but cannot remember how I did it). I only need one link on the page:
if ($result->num_rows > 0) {
    // output data of each
    while($row = $result->fetch_assoc()) {
        echo '<div class = "workformsearch">'.'<a href="'. $link .$row["frm_id"].'">Create Individual PDF</a>'.$break.'<font color="blue">' . "id:" . '<font color="purple">' .$space.$row["frm_id"]. " - Company: " . $row["frm_companyname"]. $space."Contact: " . $row["frm_contact"]. $break." email: " . $row["frm_email"]. '<font color="red">'.$break." Rep: " . $row["frm_rep"]. '<font color="green">' .$break."Date Entered: " . $row["frm_date"]. '<font color="purple">' .$break."Return Date: " . $row["frm_return"]. $break."Time From: " . $row["frm_timefrom"]. $break."Time To: " . $row["frm_timeto"]. $break."Comment1: " . $row["frm_comment1"]. $break."Comment2: " . $row["frm_comment2"]. $break."Comment3: " . $row["frm_comment3"]. $break."Comment4: " . $row["frm_comment4"]. $break."Comment5: " . $row["frm_comment5"]. $break."ToDo1: " . $row["frm_todo1"]. $break."ToDo2: " . $row["frm_todo2"]. $break."ToDo3: " . $row["frm_todo3"]. $break."ToDo4: " . $row["frm_todo41"]. $break."ToDo5: " . $row["frm_todo5"]. $break."Updated: " . $row["frm_updated"]. "<br>". "<br>".'</div>';
       
                
    }
} else {
    echo "0 results";
}
            
            }

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Bill,

Something like this might work, although it's still not entirely clear what you're trying to do here.

$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}

$idString = implode(",", $ids);

echo '<div class = "workformsearch">'.'<a href="'. $link .$idString.'">Create PDF</a>...

Open in new window

That will obvioulsy depend on what $link contains - I'm guessing it should contain a url and the start of a querystring : webpage.php?ids=

Author

Commented:
How do I include it in the following (If I include it after the "while...." below it stops all records from displaying except the first on in the array (presumably due to the extra while loop):

if (isset($_POST['company'])) {
$sql = "SELECT frm_id, frm_companyname, frm_contact, frm_email, frm_rep, frm_date, frm_return, frm_timefrom, frm_timeto, frm_comment1, frm_comment2, frm_comment3, frm_comment4, frm_comment5, frm_todo1, frm_todo2, frm_todo3, frm_todo4, frm_todo5, frm_updated FROM siteform WHERE frm_companyname = '$search'";
$result = $conn->query($sql);
                
if ($result->num_rows > 0) {
    // output data of each
    while($row = $result->fetch_assoc()) {
/*
  HIGHLIGHT FROM HERE
*/
$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}
$idString = implode(",", $ids);
echo "<div class='divechoimages'><a href='webpage.php?ids={$idString}'> ... </a>";
/*
  TO HERE
*/
        
        echo '<div class = "workformsearch">'.'<a href="'. $link .$row["frm_id"].'">Create Individual PDF</a>'.$break.'<font color="blue">' . "id:" . '<font color="purple">' .$space.$row["frm_id"]. " - Company: " . $row["frm_companyname"]. $space."Contact: " . $row["frm_contact"]. $break." email: " . $row["frm_email"]. '<font color="red">'.$break." Rep: " . $row["frm_rep"]. '<font color="green">' .$break."Date Entered: " . $row["frm_date"]. '<font color="purple">' .$break."Return Date: " . $row["frm_return"]. $break."Time From: " . $row["frm_timefrom"]. $break."Time To: " . $row["frm_timeto"]. $break."Comment1: " . $row["frm_comment1"]. $break."Comment2: " . $row["frm_comment2"]. $break."Comment3: " . $row["frm_comment3"]. $break."Comment4: " . $row["frm_comment4"]. $break."Comment5: " . $row["frm_comment5"]. $break."ToDo1: " . $row["frm_todo1"]. $break."ToDo2: " . $row["frm_todo2"]. $break."ToDo3: " . $row["frm_todo3"]. $break."ToDo4: " . $row["frm_todo41"]. $break."ToDo5: " . $row["frm_todo5"]. $break."Updated: " . $row["frm_updated"]. "<br>". "<br>".'</div>';
       
                
    }
} else {
    echo "0 results";
}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If you want to loop through your query twice then you'd need to call the data_seek method on the result set:

$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}
$idString = implode(",", $ids);

$result->data_seek(0); // reset the result

while($row = $result->fetch_assoc()) {
    ...
}

Open in new window

Author

Commented:
Ah - managed to get it working
This is the url being sent:

.....webpage.php?ids=1,2,3,4,5,6,7,...23 etc......
Obviously I will change the webpage name

So - in anser to this:
So are you basically wanting a multipage report that does what the single report page does N times once for each ID that is passed to it and sends back a SINGLE PDF with a page for each company?
Yes - I need the pdf to show reports for all the records not just one

And this:
$ids = implode(',' array(1,2,3,4,5));
$sql = "SELECT ..... FROM siteform WHERE frm_id IN ( {$ids} )";
How exactly should the ids values be referenced for the implode (ie what are 1,2,3,4,5 in relation to the values submitted)
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
That was just an example - you would actually build your IDs from your DB query:

$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}
$idString = implode(",", $ids);

And then you can pass the $idString into the IN part of your WHERE clause:

SELECT ..... FROM siteform WHERE frm_id IN ( {$idString} )";

Author

Commented:
Ok - got you
Will try and feed back

Author

Commented:
Julian - I take your point about using tags
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I also notice you put your $search (opening post) directly into the query. If $search originates from POST data you are opening yourself to a potential injection attack - you should consider using prepared statements.

I must admit I am still in the dark as to what the report actually outputs - I have not progressed passed the bit where you appear to be outputting links.

Author

Commented:
Julian - this is just in development. I take your point about prepared statements

Chris:
What do I use at the target page after the string has been passed to it to search the records?
They are being passed as ....webpage.php?1,2,3,4,5 etc.......



$ids = array();
while($row = $result->fetch_assoc()) {
    $ids[] = $row["frm_id"];
}
$idString = implode(",", $ids);

And then you can pass the $idString into the IN part of your WHERE clause:

SELECT ..... FROM siteform WHERE frm_id IN ( {$idString} )";

Author

Commented:
The target page is a completely separate page

Author

Commented:
ie do I need to explode the string at the target end
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Bill,

Yes - you'll need to do something with the variable, but as it stands, it doesn't have a key. Generally a querystring will have a key and a value. You then use the key to access the value from the GET array:

webpage.php?ids=1,2,3,4,5

From that you can see that the ids key has a value of 1,2,3,4,5, and you would access it like so:

$_GET['ids']

Depending on how you need to use it, you'll probably want to explode it into an array, so something like this:

$ids = explode("," $_GET['ids']);

That will explode the ids value from the querystring on the comma, and store the resulting array in the $ids variable.

Author

Commented:
ok - will try and give you feedback

Author

Commented:
Latest trial:

Dom PDF:
<?php
require_once("/includes/pdf/dompdf_config.inc.php");

//$companyname = $row_emOID['CustName'];

$filename = time();


$html = "";
ob_start();
//include('orders_find_visit_print_custom_status_noforms_nocosts_PDF.php');
include('searchWorkFormPDF.php');

//$filename2 = $row_emOID['CustName'];
//$filename3 = $row_emOID['Date'];
$filename2 = "Inventas";
$filename3 = "Sites";
$html = ob_get_contents();
ob_end_clean();

$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf = set_paper("a4");

$dompdf->render();
$dompdf->stream("siteworkform-$filename2-$filename3-$filename.pdf");

?>

---------------

Records passed from "searchWorkFormPDF.php" to Dom pdf page above:
<html>
<head>
    <link rel="shortcut icon" href="images/favicon.png" type="image/x-icon">
<title>Inventas Sites Visits</title>
<link rel="stylesheet" type="text/css" href="css/stylePDF.css">
     <link href="css/layoutW.css" rel="stylesheet" type="text/css" />
        <link href="css/menu.css" rel="stylesheet" type="text/css" />
   
    <style>
    #three {
    border-style: dotted;
    border-width: 2px;
}
 
}
   </style>
        <?php
    //Connect to our MySQL database using the PDO extension.
    require_once('incSearchDatabaseConnectionBySelection.php'); ?>
       
    <?php
   
$space = "&nbsp;";
$break= "<br>";
$search2 = $_GET['company'];
$search8 = $_GET['ids'];
   
    $line = "-----------------------------";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} ?>
</head>
    <body align=center>
        <div class="menudivalign"></div>
        <br />
        <div class = "pdfreport"><h1>Inventas WorkForm Report</h1></div><br /><br /><br />
        <div style= "text-align: left; margin-left:50px; width:600px;">
    <?php
           
           
            if (isset($_GET["company"])) {
             
$sql = "SELECT frm_id, frm_companyname, frm_contact, frm_email, frm_rep, frm_date, frm_return, frm_timefrom, frm_timeto, frm_comment1, frm_comment2, frm_comment3, frm_comment4, frm_comment5, frm_todo1, frm_todo2, frm_todo3, frm_todo4, frm_todo5, frm_updated FROM siteform WHERE frm_companyname = '$search2'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<font color="blue">' . "id:" . '<font color="purple">' .$space.$row["frm_id"]. " - Company: " . $row["frm_companyname"]. $space."Contact: " . $row["frm_contact"]. $break." email: " . $row["frm_email"]. '<font color="red">'.$break." Rep: " . $row["frm_rep"]. '<font color="green">' .$break."Date Entered: " . $row["frm_date"]. '<font color="purple">' .$break."Return Date: " . $row["frm_return"]. $break."Time From: " . $row["frm_timefrom"]. $break."Time To: " . $row["frm_timeto"]. $break."Comment1: " . $row["frm_comment1"]. $break."Comment2: " . $row["frm_comment2"]. $break."Comment3: " . $row["frm_comment3"]. $break."Comment4: " . $row["frm_comment4"]. $break."Comment5: " . $row["frm_comment5"]. $break."ToDo1: " . $row["frm_todo1"]. $break."ToDo2: " . $row["frm_todo2"]. $break."ToDo3: " . $row["frm_todo3"]. $break."ToDo4: " . $row["frm_todo4"]. $break."ToDo5: " . $row["frm_todo5"]. $break."Updated: " . $row["frm_updated"]. "<br>". "<br>";
    }
} else {
    echo "0 results";
}
            }
           
           
$conn->close();
?>
        </div>
    <div id = "pdfdivs">
        Generated for Inventas<br />
        Inventas Ltd cannot guarentee anything, especially if Russell did it. Your'e lucky the building isn't on fire or your car isn't ruined. Make sure you pay your bill or we send the boys round</div><br /><br /><br /><br /><br /><br /><br /><br />
        <div style = "width:700px; text-align:right;" >Signed (Company): ............................................</div><br /><br /><br />
        <div style = "width:700px; text-align:right;" >Signed (Inventas): ............................................</div><br /><br /><br />
        <div style = "width:700px; text-align:right;" >Signed (Inventas): ............................................</div>
    </body>
</html>

Ongoing issue:
The above "Dom PDF" receives records from the "searchWorkFormPDF.php" and it produces a pdf document perfectly well if only a single record is on the page
If the records on the "searchWorkFormPDF.php" page consist of multiple records the page just hangs. No pdf is produced. Apparently a variable referred to as $dompdf needs to be included in the "searchWorkFormPDF.php" records loop but I cannot see how to get it to work. The Dom.pdf refers to the following: $dompdf = new DOMPDF();
This is driving me round the bend !!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Bill,

I don't see anywhere that you're trying to do the explode / implode so I'm a little unsure what you're oing here.

Let's take a step back and work through this pieve by piece.

You have 2 main moving parts to your application - one part generates a full, complete HTML page. The other part converts that HTML into a PDF. These two parts aren't related, so we can work on them individually.

Firstly, we need to make sure your HTML page is being generated properly under all circumstances - single record / multiple records / no records, so let's start there and clean a few things up.

Let's keep this simple and forget about styling for the time being - we just want to query the database and output some information. Debugging is a lot easier if we keep things simple. Once it's working we can build it up however we like. Take a look at the following:

<?php require_once 'data.php'; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Inventas Sites Visits</title>
    </head>
    <body>

        <h1>Inventas WorkForm Report</h1>

        <?php if ( ! isset($_GET["company"]) ): ?>

            <p>No Company Info has been passed in!</p>

        <?php else: ?>

            <?php
            // Let's run the query
            $query = $conn->prepare("SELECT frm_id, frm_companyname, frm_contact FROM siteform WHERE frm_companyname = ?");
            $query->bind_param("s", $_GET['company']);
            $query->execute();
            $results = $query->get_result();
            ?>

            <?php while ($row = $results->fetch_assoc()): ?>

                <div class="row">
                    ID: <?= $row['frm_id'] ?>
                    Company: <?= $row['frm_companyname'] ?>
                    Contact: <?= $row['frm_contact'] ?>
                </div>

            <?php endwhile; ?>

        <?php endif; ?>

        <div id="footer">
            <p>Generated for Inventas</p>
            <p>Inventas Ltd cannot guarentee anything, especially if Russell did it. You're lucky the building isn't on fire or your car isn't ruined. Make sure you pay your bill or we send the boys round.</p>
            <p>Signed (Company): </p>
            <p>Signed (Inventas): </p>
            <p>Signed (Inventas): </p>
        </div>

    </body>
</html>

Open in new window

It's a very simple page, built using a prepared statement. It will query the database and loop through records, outputting each one. It expects a querystring containing the company key. It also sets up the DB connection by including a file called data.php. This file should create the connection to your database and store the connection in the $conn variable.

Give that a go and make sure it's generating the HTML correctly. If it's not, then there's no point in trying to get a PDF out of it. Get this part working properly first and then worry about the PDF.

Author

Commented:
I did try the explode / implode but no joy there either
Will try your suggestion and report back

Author

Commented:
I just get the following:

Inventas WorkForm Report
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Post up your code Bill and I'll take a look

Author

Commented:
<?php
require_once("/includes/pdf/dompdf_config.inc.php");

//$companyname = $row_emOID['CustName'];

$filename = time();


$html = "";
ob_start();
//include('orders_find_visit_print_custom_status_noforms_nocosts_PDF.php');
include('trialpdf.php');

//$filename2 = $row_emOID['CustName'];
//$filename3 = $row_emOID['Date'];
$filename2 = "Inventas";
$filename3 = "Sites";
$html = ob_get_contents();
ob_end_clean();

$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf = set_paper("a4");

$dompdf->render();
$dompdf->stream("siteworkform-$filename2-$filename3-$filename.pdf");

?>
-------------------------------------
trial.pdf:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Inventas Sites Visits</title>
        <?php require_once 'incSearchDatabaseConnectionBySelection.php'; ?>
    </head>
    <body>

        <h1>Inventas WorkForm Report</h1>

        <?php if ( ! isset($_GET["company"]) ): ?>

            <p>No Company Info has been passed in!</p>

        <?php else: ?>

            <?php
            // Let's run the query
            $query = $conn->prepare("SELECT frm_id, frm_companyname, frm_contact FROM siteform WHERE frm_companyname = ?");
            $query->bind_param("s", $_GET['company']);
            $query->execute();
            $results = $query->get_result();
            ?>

            <?php while ($row = $results->fetch_assoc()): ?>

                <div class="row">
                    ID: <?= $row['frm_id'] ?>
                    Company: <?= $row['frm_companyname'] ?>
                    Contact: <?= $row['frm_contact'] ?>
                </div>

            <?php endwhile; ?>

        <?php endif; ?>

        <div id="footer">
            <p>Generated for Inventas</p>
            <p>Inventas Ltd cannot guarentee anything, especially if Russell did it. You're lucky the building isn't on fire or your car isn't ruined. Make sure you pay your bill or we send the boys round.</p>
            <p>Signed (Company): </p>
            <p>Signed (Inventas): </p>
            <p>Signed (Inventas): </p>
        </div>

    </body>
</html>
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
OK. As I said previously, we're going to forget about creating the PDF for now, so that first code block can be completely ignored. We need to get the HTML right before we even consider the PDF. You seem to have decided to include the DB connection halfway through the HTML <head> element rather than before the HTML starts. Any reason why ?

You should also turn on error reporting for ALL development work, so add that right that the start of your file. Let's also add in some var_dumps so we can see what's being passed to your page:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'incSearchDatabaseConnectionBySelection.php';
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Inventas Sites Visits</title>
    </head>
    <body>

        <h1>Inventas WorkForm Report</h1>

        <?php var_dump($_GET); ?>

        <?php if ( ! isset($_GET["company"]) ): ?>

            <p>No Company Info has been passed in!</p>

        <?php else: ?>

            ....

Open in new window

Try that and see what you get.

Author

Commented:
ok:
This is the changed code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'incSearchDatabaseConnectionBySelection.php';
?>
<?php require_once 'incSearchDatabaseConnectionBySelection.php'; ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Inventas Sites Visits</title>
       
    </head>
    <body>

        <h1>Inventas WorkForm Report</h1> .......

The error is:
Inventas WorkForm Report
Notice: Undefined variable: conn in C:\xampp\htdocs\Development\Inventas\Sites\trialpdf.php on line 30
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\Development\Inventas\Sites\trialpdf.php on line 30
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
OK. Firstly, you're now requiring your Database connection twice! You only need it once.

The error you're getting is because you haven't stored your DB connection in a variable called $conn.

In order for this to work, you must have a connection to your database, and you must store that connection in a variable. In the example above, I've used a variable called $conn but it's up to you what you call it. I'm guessing that your included file is where you're creating the DB connection, but I don't know what's in that file so I can't comment on it.

Author

Commented:
Sorry - I copied and pasted but didn't notice the extra connection
Should the $conn be equal to the database name

ie

$conn="databasenamd";
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
No. The $conn is the variable that you store the connection in. If you're using mysqli, then it would be something like this:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$hostname = 'localhost';
$username = 'yourUsername';
$password = 'yourPassword';
$database = 'yourDBName';

try {
    $conn = new mysqli($hostname, $username, $password, $database);
}
catch (Exception $e)
{
    die("Could not connect to the DB");
}

Open in new window

You can see here that we're creating a connection to the database and storing that connection in the $conn variable.

Author

Commented:
It is pdo
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hmmm ! Are you sure? All the code you've posted so far has been mysqli:

if ($conn->connect_error) { ...
if ($result->num_rows > 0) ...
while($row = $result->fetch_assoc()) { ...

All of that code is MySQLi, not PDO!

Author

Commented:
Chris
Thanks SO much - the output is as follows:

--------------------------------------------

Inventas WorkForm Report
ID: 1 Company: 1922 Clothing Contact: bill
ID: 2 Company: 1922 Clothing Contact: bill
ID: 3 Company: 1922 Clothing Contact: Bill
ID: 4 Company: 1922 Clothing Contact: Bill
ID: 5 Company: 1922 Clothing Contact: Bill
ID: 6 Company: 1922 Clothing Contact: Bill
ID: 7 Company: 1922 Clothing Contact: Bill
ID: 8 Company: 1922 Clothing Contact: Bill
ID: 9 Company: 1922 Clothing Contact: Bill
ID: 10 Company: 1922 Clothing Contact: Bill
ID: 11 Company: 1922 Clothing Contact: Bill
ID: 12 Company: 1922 Clothing Contact: Bill
ID: 13 Company: 1922 Clothing Contact: Bill
ID: 14 Company: 1922 Clothing Contact: Bill
ID: 15 Company: 1922 Clothing Contact: Bill
ID: 16 Company: 1922 Clothing Contact: Bill
ID: 22 Company: 1922 Clothing Contact: Bill
ID: 23 Company: 1922 Clothing Contact: Bill
Generated for Inventas
Inventas Ltd cannot guarentee anything, especially if Russell did it. You're lucky the building isn't on fire or your car isn't
ruined. Make sure you pay your bill or we send the boys round.
Signed (Company):
Signed (Inventas):
Signed (Inventas):

--------------------

So do you think the initial problem is database connection - ie the include details?
If so it is odd that it worked with single records
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Good stuff.

Not sure why you were having original problems, but like I say, it's always easier to develop and debug if you break things down into manageable, logical steps.

You now have a script that is correctly generating the records you want, so you can continue to develop that and do further testing. I still don't really see where the part or your question comes in regarding imploding / exploding, but you at least have a basic working model that you can build on.

Keep building that script up and testing it against the various inputs (querystrings) that you need to make sure that it's always generating the correct HTML. Once you know it's working the way you want, then creating the PDF will be very simple.

Author

Commented:
Again - thanks so much for this Chris
Out of interest, what would be the PDO database connection string
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No worries Bill.

Something like this:

$hostname = 'localhost';
$username = 'yourPassword';
$password = 'yourPassword';
$database = 'yourDbName';

try {
    $db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch(PDOException $e) {
    echo $e->getMessage();
}

Open in new window

I'd personally recommend PDO over MySQLi - it's just a much cleaner interface in my opinion :)

Author

Commented:
Again - thanks so much for the support
One thing - I noticed that my css style sheet was causing a lot of the pdf output issues

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial