仅在填写值时才回显某些文本

时间:2013-09-10 07:25:15

标签: php mysqli while-loop echo

伙计们,我是整个php的新手,我熟悉术语和所有功能等等。 我相信我在这里要求的是超级简单的事情,我只是无法在任何地方找到它。

在下面的代码中,你们会注意到并不是所有字段都显示在下拉菜单TYPE的每次更改中,当然它们没有显示,这就是重点。我的意思是,我已安排每个TYPE拥有自己的字段。问题是:我如何适应html ECHO代码才能显示“标签”(让我们用粗体来表示我所说的内容,例如:。“< br /> 预览任务:“。$ row ['taskpre'])和所有类似的元素),我的意思是,while中的HTML片段($ row = mysqli_fetch_array($ result)),对于填写的字段?我的意思是,我不希望在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>

P.S。:该页面是一个下拉菜单,选中其中的每个选项后,将更改页面中显示的字段。 我的意思是,当我在下拉菜单中选择“视频”时,它会显示字段 - 这是textareas:“videopre”,“videowhile”和“videopost”。但是,如果我选择“练习”,它显示的唯一字段是“exerciseinstructions”。

所以,我希望这段HTML“"<br/>Preview the Task:"”只显示该值。 $ row ['taskpre']被填写,等等,用于所有oether的。否则,我不希望"<br/>Preview the Task:"与其余代码相呼应,好吗?

这是我提交“任务”时获得的ECHO(任务值为:'taskpre','taskdescription'和'outcome'):

http://i296.photobucket.com/albums/mm177/JuniorCodi/php_zps5ca6aa90.png

在上面的图片中,我标记了我想要回显的内容,当我选择TASK时,我想要回显的是红色内容。

我是一个新手,我不知道IF语句是否可以在WHILE语句中工作(至少当我尝试有错误时)。 任何人都可以通过向我提供这段代码的样本来帮助我吗?

1 个答案:

答案 0 :(得分:0)

if($row['taskpre']) {
   //your echo here
}

或使用php标签:

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

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

修改

在您的特殊情况下:

echo "..." . ($row['practicepre'] ? "<br/> Preview the Practice:" . $row['practicepre'] : "") . "<br/>Practice Instructions:" . "...";