drqxfmfa804578 2016-05-12 09:13
浏览 86
已采纳

如何将4行结果(在屏幕上显示)从while循环存储到表中

I have calculated the average price of products by type in a table and gotten the results into my screen. How do I store them into a table as well. Here is the code I have. it works other than to store into a table.

function mostrarDatos ($resultado) {
    if ($resultado !=NULL) {
        echo "- Promedio de Categoria ".'Producto_Prioridad'." es:".$resultado['total_average']."<br/>";
    }
    else {
        echo "<br/>No hay más datos!!! <br/>";
    }
}
$link = mysqli_connect("db", "user", "pass");
mysqli_select_db($link, "db");
$promedio = mysqli_query($link, "SELECT AVG( Producto_Precio ) as total_average FROM`Natan_Productos` GROUP BY Producto_Prioridad");
while ($fila = mysqli_fetch_array($promedio)){  // loops 4 times and displays on screen. How to store into table as well
    mostrarDatos($fila);
}
mysqli_free_result($promedio);
mysqli_close($link);

for results I get:

  • Promedio de Categoria Producto_Prioridad es:150.0000 //Loop 1
  • Promedio de Categoria Producto_Prioridad es:38.3333 //Loop 2
  • Promedio de Categoria Producto_Prioridad es:30.0000 //Loop 3
  • Promedio de Categoria Producto_Prioridad es:31.6667 //Loop 4

Thanks for the help.

  • 写回答

3条回答 默认 最新

  • dongyang7152 2016-05-12 09:47
    关注

    I guess you are not familiar with php & mysql.

    First, you should have a table for storing the result.Let's create a simple table if you do not have one.

    create table result_for_total_average ( avg_info varchar(256) );
    

    Then, modify your function like this.

    function mostrarDatos ($resultado, $link) {
        if ($resultado !=NULL) {
            mysqli_query($link, "INSERT INTO result_for_total_average values("."- Promedio de Categoria ".'Producto_Prioridad'." es:".$resultado['total_average'].")");
        }else {
            echo "<br/>No hay más datos!!! <br/>";
        }
    }
    $link = mysqli_connect("db", "user", "pass");
    mysqli_select_db($link, "db");
    $promedio = mysqli_query($link, "SELECT AVG( Producto_Precio ) as total_average FROM`Natan_Productos` GROUP BY Producto_Prioridad");
    while ($fila = mysqli_fetch_array($promedio)){  // loops 4 times and displays on screen. How to store into table as well
       mostrarDatos($fila);
    }
    mysqli_free_result($promedio);
    mysqli_close($link);
    

    The table I created is just simple enough to store the result, you can create your own one. If you do not know how to, here is a good start:http://www.w3schools.com/sql/default.asp.
    If you want learn more about how php and mysql work together, you can reference to http://www.w3schools.com/php/php_mysql_intro.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算