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');

From the documentation:

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call.

It is like doing:

echo 'hello';
exit;
echo 'world';

so in your function you can store the value that you want to return into a variable, you can use the assignment operator .= and then return the variable at the end of the function execution:

function generate_list($rows)
{
    # initialize the $str variable
     $str = "
     <table>
     <tr>
         <th>Firstname</th>
        <th>Sirname</th>
        <th>Email</th>
        <th>Phonenumber</th>
        <th>Information</th>
     </tr>";

     while($row = $rows->fetch_assoc())
     {
         # append values to the $str variable
         $str .= "
         <tr>
            <td>{$row['Firstname']}</td>
            <td>{$row['Sirname']}</td>
            <td>{$row['Email']}</td>
            <td>{$row['Phonenumber']}</td>
            <td>{$row['Information']}</td>
        </tr>";
     }

     # append values to the $str variable
     $str .= "</table>";

     # finally return the contents of $str
     return $str;
}

$results = $con->query("SELECT * FROM listing");

# store the results of the function into $list
$list = generate_list($results);

# print the contents of $list when needed
echo $list;

$results->free();
$con->close();

About the connection to the database, you could set that outside of the function scope, what happens if you have 30 functions pulling results from the database and you have to change the credentials?

Cereal thank you very much. Your post help me understand alot. Hopefully by weekend when i have some sparetime , i will finally make that code run. Should work. Big thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.