Jump to content

Echo certain pieces of text only if a value is filled in


jcodi

Recommended Posts

Hey, guys, I'm new in the whole php thing.

I believe what I am asking here is super simple to do, I just wasn't able to find it anywhere.

In the code below, you guys will notice that not all the fields show in every change of the dropdown menu TYPE, and, of course they don't, that's the whole point. I mean, I have arranged for each TYPE to have it's own fields. The thing is: how can I adapt for the html ECHO code to show ONLY the "labels", I mean, the pieces of HTML in the while($row = mysqli_fetch_array($result)), for the fields that were filled in? I mean, I don't want that in RESULT there are the "labels" of the fields whose values are there.

<?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>
Link to comment
Share on other sites

Ok, sorry that it wasn't clear enough. Thanks for your feedback anyway.

 

Well, here we go, let me try to explain:

 

The page is a dropdown menu that, having each one of its options selected, will change the fields that show in the page.

I mean, when I select "Video" in the dropdown menu, it shows the fields - which are textareas: "videopre", "videowhile", and "videopost". If I select "Exercise", though, the only field it shows is "exerciseinstructions".

Al right, the java for that is working perfectly, but my problem starts here:

 

The code has what I'm calling "Labels" for the fields:

$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'/>";

Here is an example (in bold what I'm calling a 'label'):

. "<br/>Preview the Task:" . $row['taskpre'])

 

So, I want that this piece of HTML ""<br/>Preview the Task:"" only shows if the value . $row['taskpre'] is filled in, and so forth, for all the oether ones. Otherwise, I don't want that the "<br/>Preview the Task:" echoes with the rest of the code, understand?

 

This is the ECHO that I get when I submit a "Task" (the task values are: 'taskpre', 'taskdescription' and 'outcome'):

 

php_zps5ca6aa90.png

I marked with a check what I want to echo and what's in red is what I don't want to echo when I select TASK.

 

Can you see what I would like to do now?

Link to comment
Share on other sites

That's what I thought, but I am a total newbie at this and I don't know if an IF statement would work inside a WHILE statement (at least when I tried there were errors).

Can you help me by giving me a sample of how this code would be?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.