我想显示用户数据,假设如下所示。
<?php session_start() ?>
<form method="post" action="">
<input type="text" name="name"/>
<input type="text" name="mobile"/>
<input type="submit" name="submit"/>
</form>
<?php
$_SESSION['name']=$_POST['name'];
$_SESSION['mobile']=$_POST['mobile'];
现在我不想将这些值存储在数据库中,而是在下面的表格中显示值
S.NO NAME MOBILE 1 Yogesh 9717797354 2 BHASKAR 9898225441 3 ANIKESH 9594474557 4 ABHISHEK 9854774144
现在我要显示每个名字和在没有使用会话存储在数据库中的情况下,如下所示在表中移动没有输入。我已尝试但只能插入单个值,并插入其更新现有值旁边。 怎么能用lop实现呢?请帮忙?? o
答案 0 :(得分:1)
Session可以存储数据数组,因此您可以执行以下操作:
<?php
session_start();
$contact = array(
"1" => array("Yogesh" => "9717797354"),
"2" => array("BHASKAR" => "9898225441"),
"3" => array("ANIKESH" => "9594474557"),
"3" => array("ABHISHEK" => "9854774144"),
);
$_SESSION["contact"] = $contact;
$cl = $_SESSION["contact"];
foreach ($cl as $pos => $info) {
foreach ($info as $name => $number) {
echo $pos.": ".$name.": ".$number."</br>";
}
}
?>
&#13;
每次有人添加新联系人时,您都可以附加此数组。像这样:
<?php
session_start();
$contact = array(
"Yogesh" => "9717797354",
"BHASKAR" => "9898225441",
"ANIKESH" => "9594474557",
"ABHISHEK" => "9854774144",
);
// Load the predifined list if not already loaded
if (!isset($_SESSION["contact"])) {
$_SESSION["contact"] = $contact;
}
// if new contact added
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["name"]) && isset($_POST["mobile"])) {
if (isset($_POST["name"]) && isset($_POST["mobile"])) {
$name = $_POST["name"];
$number = $_POST["mobile"];
$appendArray = array(
$name => $number
);
// create new array by merging the existing one and the new data.
$newList = array_merge($_SESSION["contact"], $appendArray);
$_SESSION["contact"] = $newList;
}
}
}
?>
<table>
<tr>
<th>S.NO</th>
<th>NAME</th>
<th>MOBILE</th>
</tr>
<?php
$cl = $_SESSION["contact"];
$size = 0;
foreach ($cl as $name => $number) {
?>
<tr>
<td><?php echo ++$size; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $number; ?></td>
</tr>
<?php } ?>
</table>
<form method="post" action="">
<input type="text" name="name"/>
<input type="text" name="mobile"/>
<input type="submit" name="submit"/>
</form>
&#13;
这里没有预定义的内容选项
<?php
session_start();
// if new contact added
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["name"]) && isset($_POST["mobile"])) {
if (isset($_POST["name"]) && isset($_POST["mobile"])) {
$name = $_POST["name"];
$number = $_POST["mobile"];
$appendArray = array(
$name => $number
);
// Run if the first time
if (!isset($_SESSION["contact"])) {
$_SESSION["contact"] = $appendArray;
} else {
// create new array by merging the existing one and the new data.
$newList = array_merge($_SESSION["contacts"], $appendArray);
$_SESSION["contact"] = $newList;
}
}
}
}
?>
<table>
<tr>
<th>S.NO</th>
<th>NAME</th>
<th>MOBILE</th>
</tr>
<?php
if (isset($_SESSION["contact"])){
$cl = $_SESSION["contact"];
$size = 0;
foreach ($cl as $name => $number) {
?>
<tr>
<td><?php echo ++$size; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $number; ?></td>
</tr>
<?php }} ?>
</table>
<form method="post" action="">
<input type="text" name="name"/>
<input type="text" name="mobile"/>
<input type="submit" name="submit"/>
</form>
&#13;
答案 1 :(得分:0)
<?php
echo '<table><tr><th>ONE</th><th>TWO</th></tr>';
for($i = 0; $i < 3; $i++) {
echo '<tr>';
echo '<td>';
echo $i;
echo '</td>';
echo '<td>';
echo $i*2;
echo '</td>';
echo '</tr>';
}
echo '</table>';
它使用for循环来绘制带有值的表。您必须使用所需的会话变量替换变量,并更改for循环,使其仅循环到您希望结束的时间