Jump to content

Selecting Data from MySQL to display in TCPDF


Recommended Posts

Hi all,

I am trying to get data from MySQL to display in a html table in TCPDF but it is only displaying the ID.

$accom = '<h3>Accommodation:</h3>
<table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">
<tr>

<th><strong>Organisation</strong></th>
<th><strong>Contact</strong></th>
<th><strong>Phone</strong></th>
</tr>
<tbody>
<tr>'.
$id = $_GET['id'];
$location = $row['location'];
$sql = "SELECT * FROM tours WHERE id = $id";
$result = $con->query($sql);

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

'<td>'.$location.'</td>
<td>David</td>
<td>0412345678</td>
</tbody>';
}
}
'</tr>
</table>';

Anyone got any ideas?

Link to post
Share on other sites

I'm surprised it's even displaying the id.

You are setting $location = $row['location'] before you have retrieved any rows from the query.

Inside the while loop you don't do anything, just putting a string inside some quotes.

Link to post
Share on other sites

you cannot concatenate general php logic together with a string. you can only concatenate php code that evaluates to a string. the reason it's displaying the id is because you are concatenating the result of the $id = $_GET['id']; assignment statement onto the end of the string in $accom. that's the end of the concatenation in the existing code. the rest of the strings being built in the php code are being discarded, because they are not 'attached' to anything. you would need to use $accom .= to add the string that's being built inside of the while(){} loop and again for the string that's being built after the end of the conditional logic.

edit: you would also need to terminate the string assignment statement, right before the (needless) $id = $_GET['id']; assignment statement, with a ; rather than with a concatenation dot. you should also use a prepared query when supplying external, unknown, dynamic values to a query when it gets executed, instead of putting values directly into the sql query statement. 

 

Edited by mac_gyver
Link to post
Share on other sites

Thanks both.

Sorry it took so long to respond. Been working on the road.

This seems to work:

// add a page
$pdf->AddPage();

$html4 = '<h2>Contacts</h2>
<table width="600px" border="1px">';

//data iteration

$sql = "SELECT * FROM tours";
$result = $con->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
{
    $location=$row['location'];
    // concatenate a string, instead of calling $pdf->writeHTML()
    $html4 .= '<tr><td>'.$location.'</td></tr>';
}
}
}
$html4 .= '</table>';

// output the HTML content

$pdf->writeHTML($html4, true, false, true, false, '');

Hope this is what you guys meant?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.