如何在循环中使用PHP内部的HTML

时间:2014-04-23 20:25:13

标签: php mysql

我使用while循环在配置文件页面上提供来自数据库的用户信息。我希望能够写出这样的内容,$ username&s的个人资料。我似乎无法弄明白。这是我的代码。

<?php
//open database connection
include 'page-start.php';
include 'core/init.php';
?>
<?php
$myQuery = ("SELECT user_id, username, profile, city FROM `users` WHERE user_id = '" . mysql_real_escape_string($_GET['ID']) . "' ") or die(mysql_error());

    //run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));

if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
} ?>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/studentsupport/defines.php'; ?>



    <?php include_once("head.php");
            while ($row = $result->fetch_array(MYSQLI_ASSOC))
            {

        echo '<div class="sixteen columns" id="user-profile">';

                    echo'<h2 class="username"> ' . $row['username'] . '  </h2> ';//bob's Profile
                echo'<p>' . $row['city'] . '</p>'; //city: london

                    echo '<div class="eight columns" id="user-profile-img">';

                    echo'<img src="'. $row['profile'] . '"/>';

        echo '</div>';
                echo '</div>';
}
            ?>

编辑:抱歉,我没有解释清楚。

我希望能够从数据库中获取一些信息,并将一些信息作为标准html,例如:

<p><?php echo $username; ?>'s profile </p>
<p>city: <?php echo $city; ?> </p>

3 个答案:

答案 0 :(得分:-1)

在while中的每个循环中,$ row [&#39;用户名&#39;]将有一个用户名。

如果您希望以后能够获取所有用户名,请添加以下内容:

$users[] = $row['username'];

现在,你的脚本中的所有地方$ users [0]将拥有第一个用户名,$ users [1]将拥有第二个用户名等。

答案 1 :(得分:-1)

这可能会这样做。

<?php include_once("head.php");
    while ($row = $result->fetch_array(MYSQLI_ASSOC))
    { ?>
       <div class="sixteen columns" id="user-profile">
            <h2 class="username"><?php echo $row['username']; ?></h2>
           <p><?php echo $row['city']; ?></p>
           <div class="eight columns" id="user-profile-img">
               <img src="<?php echo $row['profile']; ?>"/>
            </div>
        </div>
 <? }     ?>

答案 2 :(得分:-1)

如果你是初学者而不是我说你现在应该使用简单方法似乎没问题

就像你制作想要的整个页面一样

喜欢

                    

然后你可以在while Loop中的变量中获取数据

    while ($row = $result->fetch_array(MYSQLI_ASSOC))
   {
     $profile = $row['profile'];
      $name = $row['name'];//you can take as many variables as you want 
     $city = $row['city'];
    }        

然后你可以在html中使用这些变量

<div class="sixteen columns" id="user-profile">

                   <h2 class="username"><?php echo $name; ?> </h2>

</div>

开始像上面的下一步将变得容易你:)

相关问题