我有一个作业要求我从表单值中检索分隔文本。文本用星号(*
)分隔。我一直试图使用explode()
函数来做到这一点,但我没有运气。这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Bills</title>
<style>
body {
font-family:Verdana, Geneva, sans-serif;
}
.bad {
color:#FF0000;
}
</style>
</head>
<body>
<h1>My Bills</h1>
<?php
echo "
<p><strong>Sort Order for Items:</strong>
<form method='post'>";
$choice = $_POST['sort'];
if ($choice == 'ascending')
echo "
<label for='ascending'>Ascending</label><input name='sort' type='radio' id='ascending' value='ascending' checked='checked' />
<label for='descending'>Descending</label><input name='sort' type='radio' value='descending' id='descending' />";
if ($choice == 'descending')
echo "
<label for='ascending'>Ascending</label><input name='sort' type='radio' id='ascending' value='ascending' />
<label for='descending'>Descending</label><input name='sort' type='radio' value='descending' id='descending' checked='checked' />";
if ($choice == '')
echo "
<label for='ascending'>Ascending</label><input name='sort' type='radio' id='ascending' value='ascending' checked='checked' />
<label for='descending'>Descending</label><input name='sort' type='radio' value='descending' id='descending' />";
echo "
<table border='0' cellpadding='3' cellspacing='5'>
<tr>
<td><h4>Item</h4></td>
<td><h4>Amount</h4></td>
</tr>
";
for($row=1; $row<5;$row++)
{
$item_name='item_name'.$row;
$item_value = $_POST[$item_name];
$amount_name='amount_name'.$row;
$amount_value = $_POST[$amount_name];
$myelement = "$item_value*$amount_value*";
$myarray = array($myelement);
array_push($myarray, $myelement); //Adds $myelement to the end of the $myarray array
foreach ($myarray as $myName)
{
$list = explode("*", $myName);
echo "<br>".$list[0];
}
echo
"<tr>
<td><input type='text' name='$item_name' value='$item_value' /></td>
<td><input type='text' name='$amount_name' value='$amount_value' /></td>
";
if (!empty($amount_value))
{
if(is_numeric($amount_value))
{
$total = $total + $amount_value;
}
else {
echo
"<td class='bad'>Amount Invalid: $amount_value</td>";
$error_count++;
}
}
echo"
</tr>
";
}
echo "
<tr>
<td colspan='2'>
<input name='' type='submit' value='Submit' />
</td>
</table>
";
if ($error_count > 0)
{
echo "<br /><span class='bad' />Errors: $error_count</span>";
}
else
{
echo "<br />Total: $total";
}
?>
</form>
</body>
</html>
答案 0 :(得分:2)
我不知道你的疯狂代码是做什么的,但这是正确的语法:
$list = explode("*", $sort_line[$array_counter]);
echo "<br>".$list[0];
或
list($list) = explode("*", $sort_line[$array_counter]);
echo "<br>".$list;
请参阅http://php.net/manual/en/function.list.php
我想你应该将$sort_line[$array_counter]
更改为$myName
答案 1 :(得分:2)
鉴于这是一个家庭作业问题,我会尝试给你一些提示来考虑你自己的代码。另外,我实际上有点不确定你到底想要做什么:
explode()
,为什么要先使用*
连接值?array()
电话,并在下面直接查看您的操作。你想让价值在那里两次吗?$sort_line
是什么,$array_counter
是什么?这些变量未定义。$_POST
读取。这真的是你想要的吗?explode()
返回从字符串中拆分的元素数组。 list()
将数组解包为具体变量。您可能想要打印出解压缩的值,对吗?