调用php函数将数据放入表中

时间:2018-05-06 13:28:47

标签: php mysql

我有一个名为displayMenu()的函数,它从表中提取数据..这个函数位于一个名为functions.php的php文件中

if (isset($_POST['Starters'])) {
  $sql = "SELECT * FROM product WHERE category_id=1";
  $sql1= "SELECT cat_name,cat_description FROM category WHERE cat_id=1";
  displayMenu();
}

function displayMenu(){
global $db, $sql,$sql1,$nb;
if($result = mysqli_query($db, $sql1)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            echo '<h1 class="header">' . $row['cat_name'] . '</h2>';
            echo '<h3 class="content">' . $row['cat_description'] . '</h3>';
        }
    }
}
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
    echo "<table>";
    while($row = mysqli_fetch_array($result)){
        echo "<tr>";
            echo "<td>" . $row['p_code'] . "</td>";
            echo "<td>" . $row['p_name'] . "</td>";
            echo "<td>" . $row['p_description'] . "</td>";
            echo "<td>" . $row['p_price'] . "</td>";
                            echo "<td>" ;
                            echo '<form class="nbItem" action="order.php" method="post">Nb of items:';
                            echo '<input type="number" name="item"> <button type="submit" class="btn" name="addtocart">Add To Cart</button>';
                            echo '</form>';
                            echo "</td>";
        echo "</tr>";
    }
    echo "</table>";
    // Free result set
    mysqli_free_result($result);
} else{
    echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

这实际上有效..但我想在另一个名为order.php的文件中创建表,并从此文件中调用此函数 我应该怎么做?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要在orders.php文件中包含您的functions.php文件,然后从包含您的订单文件调用displayMenu?

order.php

include 'functions.php';

echo displayMenu();