Rendering data in a table using jQuery in PHP
<p>I have a data and I use PHP to access them. So I created a simple button and when I click it my program needs to create a table containing the data. <strong>Below you can see the jQuery code that reads the data and creates a table. </strong>But the problem is, I can't access every element in DATA.
I want to clarify that since I've also added the code, I'm making a selection in the data. <em>It's called "household.php"</em></p>
<pre class="brush:php;toolbar:false;"><html lang="en">
<head>
<script
src="https://code.jquery.com/jquery-3.6.2.js"
integrity="sha256-pkn2CUZmheSeyssYw3vMp1 xyub4m e QK4sQskvuo4="
crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
<script>
function suchen() {
jQuery(document).ready(function($) {
$.ajax({
type: "get",
url: "household.php",
dataType: "json",
success: function(response) {
var data = JSON.parse(response)
var html_table = "";
data.forEach(function(item) {
var row = $("<tr>");
row.append($("<td>").text(item.contact_id_a));
row.append($("<td>").text(item.contact_id_b));
// add more cells for additional columns
html_table = row[0].outerHTML;
});
$("#tabelle").html(html_table);
}
});
});
}
</script>
</head>
<body>
<form id="form" onsubmit="suchen()" method="get">
<label>Enter your age: </label>
<br />
<input type="number" name="min" min="0">
<br />
<input type="number" name="max" min="0">
<br />
<input type="submit">
</form>
<div id="tabelle"></div>
</body>
</html></pre>
<p><strong>This is the code for the family.php file. It works without problems. But I can't connect between my main php files. </strong></p>
<pre class="brush:php;toolbar:false;"><?php
require_once '/var/www/html/wordpress/wp-content/plugins/civicrm/civicrm/civicrm.config.php';
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton();
$relationships = \Civi\Api4\Relationship::get()
->addSelect('contact_id_a', 'contact_id_b', 'contact_id_a.display_name', 'contact_id_b.household_name', 'relationship_type_id')
->addClause('OR', ['relationship_type_id', '=', 7], ['relationship_type_id', '=', 8])
->setLimit(25)
->execute();
foreach ($relationships as $relationship) {
// do something
}
var_dump(json_encode($relationships));
?></pre>
<p>I cannot access the data using the php file. I also can't connect to my main php file by searching for php. </p>