How to execute PDO in a variable PHP - php

while($row =$result->fetch())
{
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>
'.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id",$row["Entretien"]);
$q2->execute();
$entretien=$q2->fetch().'
<td>'.$entretien["Date"].'</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>
<a class="btn btn-danger" href="delete_personnes.php?ID_personne='.$row["ID_personne"].'"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;
I have always an Error: ( ! ) Catchable fatal error: Object of class PDOStatement could not be converted to string in C:.......... on line 61('.$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$row["Entretien"] it's a foreign key

There was some syntax error in your code as $q2 is being concat with html string. Improved code should be:
while ($row = $result->fetch()) {
$output .= '
<tr>
<td>' . $row["ID_personne"] . '</td>
<td>' . $row["Nom"] . '</td>
<td>' . $row["Prenom"] . '</td>
<td>' . $row["Telephone"] . '</td>
<td>' . $row["Mail"] . '</td>
<td>' . $row["Categorie"] . '</td>
<td>' . $row["Type"] . '</td>';
$q2 = $db->prepare("SELECT * FROM entretiens WHERE ID_entretien=:id");
$q2->bindValue(":id", $row["Entretien"]);
$q2->execute();
$entretien = $q2->fetch();
$output .= '<td>' . $entretien["Date"] . '</td>
<td width=250>
<a class="btn btn-primary" href="update_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-pencil"></span> Modifier</a>
<a class="btn btn-danger" href="delete_personnes.php?ID_personne=' . $row["ID_personne"] . '"><span class="glyphicon glyphicon-remove"></span> Supprimer</a>
</td>
</tr>';
}
$output .= '</table>';
echo $output;

Related

My code is outputting all data in 1 row, how do i make it output seperate data into seperate rows?

When my data is outputted from my server onto a website it outputs it onto 1 row instead of going down a row and outputting the next row of data
!
session_start();
$view = new stdClass();
$view->pageTitle = 'Camp Sites';
require_once('views/CampSites.phtml');
require_once('Models/campData.php');
require_once('Models/campDataSet.php');
$campDataSet = new campDataSet();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<tr><td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td></tr>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
You need to wrap tags around the code in your foreach like above.

Method 'fetch_assoc' not found in array - Error how do i fix it?

i'm trying to retrieve information from a database and put it into a table in PHP and HTML however, i've encountered a problem where it says ive got an error:
Fatal error: Uncaught Error: Function name must be a string in
F:\Client Server\Website\campsites.php:41 Stack trace: #0 {main}
thrown
when I check the line that the error is in it says :
Method 'fetch_assoc' not found in array
How can I fix this?
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if($result > 0){
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
while($row = $result->fetch_assoc()){
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
EDIT :
var_dump($result);
array(1) { [0]=> object(DBdata)#6 (11) { ["db"]=> NULL ["conn"]=> object(PDO)#7 (0) { }
["id_campsite"]=> string(1) "1" ["campsite_name"]=> string(13) "lake district" ["address"]=> string(32) "lake distrcit greater manchester"
["postcode"]=> string(7) "m21 8fx" ["country"]=> string(7) "England" ["lattitude"]=> string(13) "1 0800 243230" ["longitude"]=> string(12) "120312310978"
["email"]=> string(14) "lake#gmail.com" ["phone_number"]=> string(12) "059874327823" } }
One possible reason for this error is that $campDataSet->fetchAllCamps($sqlQuery) returns an array. In this case your code should look like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search = $_POST['search'];
$_SESSION['search'] = $search;
$sqlQuery = "SELECT * FROM campsites ='$search'";
$result = $campDataSet->fetchAllCamps($sqlQuery);
if (count($result) > 0) {
echo'<div class="table-responsive">
<table class="table">
<thead id="table1Head">
<tr><td>campID</td>
<td>Name</td>
<td>Address</td>
<td>Postcode</td>
<td>Country</td>
<td>Latitude</td>
<td>Longitude</td>
<td>email</td>
<td>Phone<td></tr>
</thead>
<tbody>
</div>';
foreach ($result as $row) {
echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
}
echo "</tbody></table>";
} else {
print " 0 results";
}
}
?>
Update:
Your $campDataSet->fetchAllCamps returns an array of objects. This is more appropriate in this case:
<?php
...
foreach ($result as $row) {
echo '<td>' . $row->id_campsite. '</td> <td>' . $row->campsite_name . '</td> <td>' . $row->address . '</td> <td>' . $row->postcode . '</td> <td>' . $row->country. '</td> <td>' . $row->lattitude . '</td> <td>' . $row->longitude . '</td> <td>' . $row->email . '</td> <td>' . $row->phone_number . '</td> </td>';
}
...
?>
Assuming that fetchAllCamps() does the fetching for you, this is why you have an array rather than a result set, so instead of the line...
while($row = $result->fetch_assoc()){
replace it with
foreach ( $result as $row ) {

Two values in the same column

I have two columns with different values:
And I need to echo those two values into one column, like that:
Printing method:
if($result->num_rows > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
</tr>';
}
}
Any suggestions on how I could do that?
Do you mean like this
echo '<tr>
<td scope="row">' . $row["id"].'</td>
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>
</tr>';
Merge the two cells:
<td>' . $row["data"] .'</td>
<td>' . $row["ip"] .'</td>
Into one:
<td>' . $row["data"] . '(' . $row["ip"] . ')</td>

DOMPDF not open in Windows

I have made a PDF document from PHP, but I do not open in Microsoft Widnows 7 and 8, It has made the PDF with tables, and insert the data correctly with cycles, but in Windows this problem occurs and does not open the downloaded PDF document, I've done the test with 3 computers with Windows.
public function pdfAction() {
require_once("dompdf/dompdf_config.inc.php");
$seguimiento = new Application_Model_DbTable_Seguimientos();
$datos_seguimiento = $seguimiento->casosReportes();
//var_dump($datos_seguimiento);
for ($i = 0; $i < count($datos_seguimiento); $i++) {
$caso = new Application_Model_DbTable_AsuntosEstudiantiles();
.........continuecode...................
$html = '<html><body>' .
'<table border="1" rules="all">
<tr>
<th colspan="10"> TITLE</th>
</tr>
<tr>
<td>Periodo Academico</td>
<td>ESTAMENTO (Estudiante, Profesor, administrativo)</td>
<td>Nombre</td>
<td>GENERO (Femenino - Masculino)</td>
<td>TIPO DE INDENT(c.c. - t.i. - c.ext.)</td>
<td>NUMERO DE IDENT</td>
<td>Codigo</td>
<td>PROYECTO CURRICULAR o DEPENDENCIA</td>
<td>FACULTAD</td>
<td>Tipo Caso</td>
<td>Observaciones</td>
</tr>';
$datoshtml = $datoshtml . '
<tr>
<td>' . $fecha . '</td>
<td>' . $tipo_beneficiario . '</td>
<td>' . $nombre_beneficiario . '</td>
<td>' . $genero . '</td>
<td>' . $tipo_identificacion . '</td>
<td>' . $numero_identificacion . '</td>
<td>' . $codigo_ud . '</td>
<td>' . utf8_decode($nombre_proyecto) . '</td>
<td>' . utf8_decode($nombre_facultad) . '</td>
<td>' . $tipoasunto . '</td>
<td>' . utf8_decode($observaciones) . '</td>
</tr>';
/* */
}
$html1 = '</table></font></body></html>';
$info = $html . $datoshtml . $html1;
//echo $info;
$dompdf = new DOMPDF();
$dompdf->set_paper("A4", "landscape");
$dompdf->load_html($info);
$dompdf->render();
$dompdf->stream("reporte.pdf");
}
Open in Linux
Open in Windows

how to get ID value from looped table using php and javascript

I've been trying to work out how to create a table using php and mysql, this is what i've got so far...
PHP :-
foreach($result as $res){
echo '<tr><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Javascript:-
$('button#deleteBtn').click( function(){
data = $('p#ID').html();
alert(data);
// window.location('admin.php?link=LF1'+data);
});
when i click deleteBtn i want to delete the row the button is on, therefore i need the ID...
How do I get the value of $ID. my jquery alerts only the first ID for every button...
Thanks!
First of all, id should be unique, so you cannot do id="ID" for every row.
Try with:
foreach($result as $res){
echo '<tr data-id="' . $ID . '">';
// ...
echo '</tr>';
}
And in JavaScript:
$('button#deleteBtn').click( function(){
var id = $(this).parents('tr').data('id');
alert(id);
}
It looks like if you have more than 1 row, you'll inevitably end up with a number of elements with the same ID. A cleaner up version would be:
More easily, you can simply use .closest()
PHP:
echo '<tr id='.$ID.'><td style="display:none;"><p id="ID">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button class="delete btn btn-danger btn-xs">Delete</button></td></tr>'
jQuery:
$('button.delete').click( function(){
var toDelete = $(this).closest('tr').attr('id');
alert(toDelete);
});
You can use data attribute to keep your id. like data-id="1"
foreach($result as $res) {
echo '<tr><td style="display:none;"><p data-id="'.$ID.'" id="ID"'.$ID.' data-id="">' . $ID . ' </p></td><td>' . $res['NAME'] . '</td>
<td>' . $res['option2'] . '</td>
<td>' . $res['Option3'] . '</td>
<td>' . $res['Option4'] . '</td>
<td>' . $res['Option5'] . '</td>
<td class="deleteBtn"><button onclick="delete("ID'.$ID.')" class="btn btn-danger btn-xs" id="deleteBtn">Delete</button></td></tr>';
}
Now you can Id like this:-
$('button#deleteBtn').click( function(){
data = $(this).parents('tr').data('id');
alert(data);
});

Categories