My code works with echo but if I use return it wount display the wanted data, but i want to use code with return so the data will display wherever the shortcode is inserted and not just at the top as it is with echo. So here are the codes , how con i righten this ?
// this works fine but only displays at top of page
function generate_list(){
$dbhost = "list.com.mysql";
$dbname = "list_com";
$dbuser = "list_com";
$dbpwd = "list";
$con=mysqli_connect($dbhost,$dbuser,$dbpwd, $dbname);
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM listing");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Sirname</th>
<th>Email</th>
<th>Phonenumber</th>
<th>Information</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Firstname'] . "</td>";
echo "<td>" . $row['Sirname'] . "</td>";
echo "<td>" . $row['Email'] . "</td>";
echo "<td>" . $row['Phonenumber'] . "</td>";
echo "<td>" . $row['Information'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
}
add_shortcode('Insert_list','generate_list');
// this doesnt display data
function generate_list(){
$dbhost = "list.com.mysql";
$dbname = "list_com";
$dbuser = "list_com";
$dbpwd = "list";
$con=mysqli_connect($dbhost,$dbuser,$dbpwd, $dbname);
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM listing");
return "<table border='1'>
<tr>
<th>Firstname</th>
<th>Sirname</th>
<th>Email</th>
<th>Phonenumber</th>
<th>Information</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
return "<tr>";
return "<td>" . $row['Firstname'] . "</td>";
return"<td>" . $row['Sirname'] . "</td>";
return"<td>" . $row['Email'] . "</td>";
return"<td>" . $row['Phonenumber'] . "</td>";
return"<td>" . $row['Information'] . "</td>";
return"</tr>";
}
return "</table>";
mysqli_close($con);
}
add_shortcode('Insert_list','generate_list');