drqxfmfa804578 2016-05-12 01: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 01: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部