Support » Plugins » Using Return Function

  • I am very new to plugin development and don’t know much of PHP either. I needed a very simple plugin to display and rotate entries from a database. I have written the following code:

    <?php
    /*
    Plugin Name: FAQ Rotator Plugin
    Plugin URI: https://www.ezeetranslate.com
    Description: Simple non-bloated WordPress Contact Form
    Version: 1.0
    Author: Cethos International Limited
    Author URI: https://www.ezeetranslate.com
    */

    /**
    * MY SHORTCODE
    **/

    function faq(){

    $servername = “localhost”;
    $username = “ezeetran_wp944”;
    $password = “5S7X@4@3P3”;
    $dbname = “ezeetran_wp944”;
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
    }

    $sql = “SELECT * FROM faq_custom”;
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”;
    }
    }
    $conn->close();
    }

    add_shortcode(‘faq_rotator’, ‘faq’);

    ?>

    It is doing what i want it to do, but the problem is with the echo command. When I use echo “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”; the output reaches the top of the page. I have tried to use return “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”; but the problem with this one is that it returns only one entry from the db.

    Any help will be appreciated.

Viewing 1 replies (of 1 total)
  • I am very new to plugin development and don’t know much of PHP either. I needed a very simple plugin to display and rotate entries from a database. I have written the following code:

    ‘<?php
    /*
    Plugin Name: FAQ Rotator Plugin
    Plugin URI: https://www.ezeetranslate.com
    Description: Simple non-bloated WordPress Contact Form
    Version: 1.0
    Author: Cethos International Limited
    Author URI: https://www.ezeetranslate.com
    */

    /**
    * MY SHORTCODE
    **/

    function faq(){

    $servername = “localhost”;
    $username = “ezeetran_wp944”;
    $password = “5S7X@4@3P3”;
    $dbname = “ezeetran_wp944”;
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
    }

    $sql = “SELECT * FROM faq_custom”;
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
    echo “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”;
    }
    }
    $conn->close();
    }

    add_shortcode(‘faq_rotator’, ‘faq’);

    ?>’

    It is doing what i want it to do, but the problem is with the echo command. When I use ‘echo “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”;’ the output reaches the top of the page. I have tried to use ‘return “<div>”. $row[“faq_question”]. “</div><div> ” . $row[“faq_answer”]. “</div>”;’ but the problem with this one is that it returns only one entry from the db.

    Any help will be appreciated.

Viewing 1 replies (of 1 total)
  • The topic ‘Using Return Function’ is closed to new replies.