This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

pwtan14262
pwtan14262

Hi all,

I would like to know how can i display rows of data which i fetch from MySQL. The following is what i have

public function onLoad(){
             $username = "*******"; //mysql username
           $password = "*******"; //mysql password
           $hostname = "******"; //hostname
           $databasename = '*****'; //databasename

           //connect to database
           $mysqli = new mysqli($hostname, $username, $password, $databasename);

           //MySQLi query
           $results = $mysqli->query("SELECT * FROM pwtan_shop_items");

   $display_string = "<table>";
    $display_string .= "<tr>";
    $display_string .= "<th>ITEM</th>";
    $display_string .= "</tr>";

         while($row = mysqli_fetch_array($results)){
                 $display_string .= "<tr>";
                 $display_string .= "<td>" . $row['item'] . "</td>";
                 $display_string .= "</tr>";
        }   
       $display_string .= "</table>";

           $mysqli->close();
 echo $display_string;
 }

in my htm
<button type="submit" class="btn btn-primary" data-request="onLoad" data-request-update="_list_table: '#result'">Fetch

Last updated

LilEssam
LilEssam

I think you have to type return $display_string; at the end of your function.

pwtan14262
pwtan14262

Hi LilEssam,

It's not working for me. Do you have other suggestion?

LilEssam
LilEssam

Well, If you're working in a theme layout or partial you should do $this['string'] = $display_string and then you can echo $string in your page.

Otherwise to know the source of the problem you should return any static word like "hello" to see even if the function return or echo anything.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.