在页面的其他部分显示数据库中的数据

时间:2014-04-14 19:58:38

标签: php mysql database

大家好我是php的新手,我想知道如何从我的数据库中检索数据并在网页的不同部分显示更清晰的方式?

例如,请参阅我当前的代码......

<?php
$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$_SESSION['email']."'");
while($row = mysqli_fetch_array($result)){
   if($_SESSION['email'] == $row['email']) {
        // If you find the right user with the same E-Mail address, DO SOME COOL THINGS :)
   }
   echo '<h2>'. $row['username'] . '</h2>' ;
   echo 'Hi, I am a ' . $row['yourage'] . ' year old ' . $row['orientation'] . ' ' . $row['gender'] . ' looking for a ' . $row['lookingfor'] . '.<br /><br />';
   echo 'I am currently living in the ' . $row['location'] . ' area.';
   echo '<button>Get in touch</button>' . '<br>';
   echo '<br /><br />';
   echo '<hr />';
   echo '<h2>About me:</h2>';                   
   echo $row['aboutme'] . '<br>';
}
mysqli_close($mysqli);
?>

如果我在页面上再次复制它会杀死脚本,但想知道最佳做法是什么?

4 个答案:

答案 0 :(得分:1)

也许您应该将查询脚本(下面)与查看部分分开。

query.php

<?php
$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$_SESSION['email']."'");
while($row = mysqli_fetch_array($result)){
   if($_SESSION['email'] == $row['email']) {
        // If you find the right user with the same E-Mail address, DO SOME COOL THINGS :)
   }
   $returnArray[] = $row['username'];
   $returnArray[] = $row['yourage'];
   //and so on;
}
mysqli_close($mysqli);
?>

一些view.php(或index.html重命名为index.php):

<?php
include('query.php');

var_dump($returnArray);
//Or with a foreach or however

但你应该了解MVC Model View Controller,正如其他人所说,使用OO编程

答案 1 :(得分:1)

首先,更好的方法是分离数据库和视图代码,例如使用MVC框架(如CakePHP)。

但是如果以类似的方式对待你,我会做的是:(你只是从数据库中选择一个结果,如果你选择了很多,这将是不同的。) 在页面顶部:

<?php
$email = $_SESSION['email']; 
$result = mysqli_query($mysqli,"SELECT * FROM members WHERE email='".$email."'");
while($row = mysqli_fetch_array($result)){
   $member = $row;
}
mysqli_close($mysqli);
?>

然后,只要您需要在页面中显示数据:(例如)

My username is <?php echo $member['username']; ?>

答案 2 :(得分:0)

了解ajax - 这有助于您将此代码与应用程序分开。只需对这个php进行ajax调用并获得结果并向他们展示你想要显示的内容。

例如 -

在你的应用程序中,说 app.html ,在你想要从db获取内容的那一点 -

$.ajax({
  type: "GET",
  url: "fetch.php", // your aove php file
  function(response){
     console.log(response); // you'll get all the rows here
     response=JSON.parse(response);
     for(var i=0; i<response.length; i++){
        console.log(response[i].username);  // show results in view
        ....
     }
  }
});

<强> fetch.php

$myrows = array();
while($row = mysqli_fetch_array($result)){
   array_push($myrows, $row);
}
echo json_encode($myrows);

答案 3 :(得分:0)

现实情况是,您需要某种模板引擎将数据库结果直接过滤到模板中。

因此,您不需要处理echo命令来设计页面,而是需要一个原始模板,然后将数据从数据库阵列注入该模板。

您可以使用标准化的内容,例如Mustache,也可以轻松推出自己的内容(虽然不成熟):

$output = '';
$tpl = fopen('template.html','r');
$tpl = fread($tpl, 100000);

$templateVars = array();
while($row = mysqli_fetch_array($result)){ 
    $temporaryRow = $tpl;
    foreach ($row as $k=>$v) {
        $temporaryRow = preg_replace("/{{$k}}/i",$v,$temporaryRow);
    }
    $output .= $temporaryRow;
}

echo $output;

然后你的template.html就像是

<div>Hello, {{username}}</div>