Php 仅当填写了值时才回显某些文本

Php 仅当填写了值时才回显某些文本,php,mysqli,while-loop,echo,Php,Mysqli,While Loop,Echo,伙计们,我对整个php都是新手,我不熟悉术语和所有函数等等。 我相信我在这里要求的是非常简单的,我只是在任何地方都找不到 在下面的代码中,你们会注意到并不是所有的字段都会显示在下拉菜单类型的每次更改中,当然它们不会,这就是重点。我的意思是,我已经为每种类型安排了自己的字段。问题是:我如何调整html回送代码,使其仅显示填充字段的while($row=mysqli_fetch_数组($result))中的html片段(例如:“预览任务:”$row['taskpre']),以及所有类似元素(我的意思

伙计们,我对整个php都是新手,我不熟悉术语和所有函数等等。 我相信我在这里要求的是非常简单的,我只是在任何地方都找不到

在下面的代码中,你们会注意到并不是所有的字段都会显示在下拉菜单类型的每次更改中,当然它们不会,这就是重点。我的意思是,我已经为每种类型安排了自己的字段。问题是:我如何调整html回送代码,使其仅显示填充字段的while($row=mysqli_fetch_数组($result))中的html片段(例如:“
预览任务:”$row['taskpre']),以及所有类似元素(我的意思是,while($row=mysqli_fetch_数组($result))?我的意思是,我不希望结果中有字段的“标签”,这些字段的值就在那里

代码如下:

    <?php
$con=mysqli_connect("localhost","root","","activitytest1");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_POST['submit'])) {
$stmt = mysqli_prepare($con, "INSERT INTO activitytest1 (type, author, taskpre, taskdescription, outcome, semipre, semiwhile, exercisedescription, practicepre, practiceinstructions, leadin, gamepre, gameinstructions, videopre, videowhile, videopost, songpre, songwhile, songpost, mimioinstructions, otherinstructions, path, books, grouping, time)
VALUES
(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

mysqli_stmt_bind_param($stmt, 'sssssssssssssssssssssssss', $type, $author, $taskpre, $taskdescription, $outcome, $semipre, $semiwhile, $exercisedescription, $practicepre, $practiceinstructions, $leadin, $gamepre, $gameinstructions, $videopre, $videowhile, $videopost, $songpre, $songwhile, $songpost, $mimioinstructions, $otherinstructions, $path, $books, $grouping, $time);

$type = $_POST['type'];
$author = $_POST['author'];
$taskpre = $_POST['taskpre'];
$taskdescription = $_POST['taskdescription'];
$outcome = $_POST['outcome'];
$semipre = $_POST['semipre'];
$semiwhile = $_POST['semiwhile'];
$exercisedescription = $_POST['exercisedescription'];
$practicepre = $_POST['practicepre'];
$practiceinstructions = $_POST['practiceinstructions'];
$leadin = $_POST['leadin'];
$gamepre = $_POST['gamepre'];
$gameinstructions = $_POST['gameinstructions'];
$videopre = $_POST['videopre'];
$videowhile = $_POST['videowhile'];
$videopost = $_POST['videopost'];
$songpre = $_POST['songpre'];
$songwhile = $_POST['songwhile'];
$songpost = $_POST['songpost'];
$mimioinstructions = $_POST['mimioinstructions'];
$otherinstructions = $_POST['otherinstructions'];
$path = $_POST['path'];
$books = $_POST['books'];
$grouping = $_POST['grouping'];
$time = $_POST['time'];
$submit = $_POST['submit'];


mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}

$result = mysqli_query($con,"SELECT * FROM activitytest1");

while($row = mysqli_fetch_array($result))
{
echo "<table width='909' border='1' align='center' cellpadding='5' cellspacing='0'>
<tr>
<th width='125' scope='col'>Type</th>
<th width='680' scope='col'>Description</th>
<th width='120' scope='col'>Author</th>
</tr></table>

<br/>

<table width='909' border='1' align='center' cellpadding='5' cellspacing='0'>
<tr>
<td width='125'>" . $row['type'] . "</th>
<td width='680'>Time:" . $row['time'] . " min. <br><br>" . $row['books'] . "<br/>Grouping: " . $row['grouping'] . "<br/>Preview the Task:" . $row['taskpre'] . "<br/>Instructions:" . $row['taskdescription'] . "<br/>Outcome:" . $row['outcome'] . "<br/>Preview the semi-task:" . $row['semipre'] . "<br/>Instructions:" . $row['semiwhile'] . "<br/>Exercise Instructions: " . $row['exercisedescription'] . "<br/> Preview the Practice:" . $row['practicepre'] . "<br/>Practice Instructions:" . $row['practiceinstructions'] . "<br/>Lead-in:" . $row['leadin'] . "<br/>Preview the Game:" . $row['gamepre'] . "<br/>Game Instructions:" . $row['gameinstructions'] . "<br/>Song PRE:" . $row['songpre'] . "<br/>Song WHILE:" . $row['songwhile'] . "<br/>Song POST:" . $row['songpost'] . "<br/>Video PRE:" . $row['videopre'] . "<br/>Video WHILE:" . $row['videowhile'] . "<br/>Video POST:" . $row['videopost'] . "<br/>Mimio Instructions:" . $row['mimioinstructions'] . "<br/>Instructions:" . $row['otherinstructions'] . "<br/><br>Link/Path" . $row['path'] . "<br/></th>
<td width='120'>" . $row['author'] . "</th>
</tr></table> <br/> <hr size='1'/>";
}

mysqli_close($con);
?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test 1</title>
</head>
<body>

<br>
<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<th width="125" scope="col">Type</th>
<th width="680" scope="col">Description</th>
<th width="120" scope="col">Author</th>
</tr></table>
<br>
<form action="index.php" method="POST">

<table width="909" border="1" align="center" cellpadding="5" cellspacing="0">
<td width="125"><label>

</label>
<select name="type" id="type" onChange="display(this,'Task','Semi-task','Practice','Exercise','Lead-in', 'Game', 'Video','Song','Mimio','Other');">
<option value="Unselected" selected="selected">Choose one:</option>
<option value="Task">Task</option>
<option value="Semi-task">Semi-task</option>
<option value="Practice">Practice</option>
<option value="Exercise">Exercise</option>
<option value="Lead-in">Lead-in</option>
<option value="Game">Game</option>
<option value="Video">Video</option>
<option value="Song">Song</option>
<option value="Mimio">Mimio</option>
<option value="Other">Other</option>
</select></td>
<td width="680"><div id="Task" style="display:none"> Task: <br />
Lead-in/Preview the task: <textarea name="taskpre" cols="70" rows="2"></textarea>
<br />
Instructions: <textarea name="taskdescription" cols="70" rows="2"></textarea>
<br />
Outcome: 
<textarea name="outcome" cols="70" rows="2"></textarea>
<br />
</div>

<div id="Semi-task" style="display:none">Semi-task:<br />
Lead-in/Preview the task: <textarea name="semipre" cols="70" rows="2"></textarea>
<br />
Instructions: <textarea name="semiwhile" cols="70" rows="2"></textarea>
<br />
</div>

<div id="Exercise" style="display:none">Exercise:<br />
Instructions/Obs.: 
<textarea name="exercisedescription" cols="70" rows="2"></textarea>
</div>
<div id="Practice" style="display:none">
Practice:<br />
Lead-in/Preview the task: <textarea name="practicepre" cols="70" rows="2"></textarea>
<br />
Instructions: <textarea name="practiceinstructions" cols="70" rows="2"></textarea>
<br />
</div>

<div id="Lead-in" style="display:none">
Lead-in:<br>
<textarea name="leadin" cols="70" rows="2"></textarea><br>
</div>
<div id="Game" style="display:none">
Game:<br />
Lead-in: 
<textarea name="gamepre" cols="70" rows="2"></textarea>
<br />
Instructions: <textarea name="gameinstructions" cols="70" rows="2"></textarea>
</div>

<div id="Video" style="display:none"> Video: <br />
Pre: <textarea name="videopre" cols="70" rows="2"></textarea>
<br />
While: <textarea name="videowhile" cols="70" rows="2"></textarea>
<br />
Post: 
<textarea name="videopost" cols="70" rows="2"></textarea>
</div>

<div id="Song" style="display:none"> Song: <br />
Pre: <textarea name="songpre" cols="70" rows="2"></textarea>
<br />
While: <textarea name="songwhile" cols="70" rows="2"></textarea>
<br />
Post: 
<textarea name="songpost" cols="70" rows="2"></textarea>
</div>

<div id="Mimio" style="display:none">Mimio:<br />
Instructions:<br>
<textarea name="mimioinstructions" cols="70" rows="2"></textarea>
</div>

<div id="Other" style="display:none">Other:<br />
Instructions:<br>
<textarea name="otherinstructions" cols="70" rows="2"></textarea><br />
</div> </td>
<td width="120"><input name="author" type="text" size="12" maxlength="25" /></td>
</tr>

<tr>
<td colspan="3">
<select name="books" id="Books">
<option value="books open" selected="selected">books open</option>
<option value="books closed">books closed</option>
</select>
<br />
<select name="grouping">
<option value="individual" selected="selected">individual</option>
<option value="pairs">pairs</option>
<option value="trios">trios</option>
<option value="groups of 3 to 4 students">groups of 3 to 4 students</option>
<option value="groups of 5 students">groups of 5 students</option>
<option value="divide the class into 2 groups">divide the class into 2 groups</option>
<option value="divide the class into 3 groups">divide the class into 3 groups</option>
<option value="divide the class into 4 groups">divide the class into 4 groups</option>
</select>
<br />
about 
<input name="time" type="text" id="Time" size="1" />
min </td>
</tr> 
<tr>
<td colspan="3">Link/Path: <input name="path" type="text" id="Path" size="40" /></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Post!" /></td>
</tr>
</table>
</form>


</body>
</html>

或使用php标记:

<?php if($row['taskpre']) : ?>
   <your_html_here>
<?php endif; ?>

请不要在下一个问题中发布所有代码,除非您引用了所有代码。:)

编辑

在您的特殊情况下:

echo "..." . ($row['practicepre'] ? "<br/> Preview the Practice:" . $row['practicepre'] : "") . "<br/>Practice Instructions:" . "...";
echo“…”。($row['practicepre']?”
预览实践:“.$row['practicepre']:”)。“
实践说明:”。"...";
“伙计们,我对整个php都是新手,对术语和所有函数等都不熟悉”-你马上就跳到了“最深处”。你应该先学会游泳,然后才能从奥运会大小的游泳池跳水。至少,在上面的图片中,我用支票做了标记…-就像我有一个PhotoBucket帐户,这样我就可以查看该文件。不行。
echo "..." . ($row['practicepre'] ? "<br/> Preview the Practice:" . $row['practicepre'] : "") . "<br/>Practice Instructions:" . "...";