Im new to php and mysql and doing my final year project, i want to ask u ppl that what if i want to display a row of a table after some if condition, i want to diplay offered courses to all the students and if the student whose course is accepted i dont want to display <th>action</th> in that row.. iam wondering how would i do that, please help me solve the problem, thanks in advance

<?php
   $con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
    {
   die('Could not connect: ' . mysql_error());
    }
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT courses.id AS cid, courses.title, courses.subjectsid, subjects.id AS sid, subjects.subjectname,  requestrecord.status
FROM courses JOIN subjects ON courses.subjectsid = subjects.id 
LEFT JOIN requestrecord 
ON courses.id = requestrecord.coursesid AND requestrecord.accountsid=".$_SESSION['id']);

echo "<table border='1' style='width:500px;'> <br />
<tr>
<th>Course Name</th>
<th>Subject </th>
<th>Action</th>
<th>Status</th>

</tr>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['subjectname'] . "</td>";
    echo "<td><a href='request-for-approval.php?id=".$row['cid']."& sid=".$row['sid']."'>Apply!</a></td>"; 
    echo "<td>" . $row['status'] . "</td>";
  echo "</tr>";
    }
echo "</table>";
mysql_close($con);
?>

Dani AI

Generated

Short answer: check the request status for each row as you output it and only render the action cell when the status indicates the user hasn't applied. You shouldn't try to remove a single <th> for one row — headers describe columns, so keep the header and leave that row's action cell empty or show "Already applied" to keep column alignment and accessibility.

A couple of practical notes that go beyond the original posts: because your query uses a LEFT JOIN the status value may be NULL for rows with no request, so test for NULL/empty as well as the literal string you use (e.g. "applied"). Also stop using the old mysql_* extension — move to mysqli or PDO and prepared statements, cast $_SESSION['id'] to int, and escape output with htmlspecialchars() to avoid XSS.

Example (concise, modern approach using PDO):

$pdo = new PDO($dsn, $user, $pass);
$sql = "SELECT c.id cid, c.title, s.id sid, s.subjectname,
        COALESCE(r.status,'') AS status
        FROM courses c
        JOIN subjects s ON c.subjectsid = s.id
        LEFT JOIN requestrecord r ON c.id = r.coursesid AND r.accountsid = :uid";
$stmt = $pdo->prepare($sql);
$stmt->execute([':uid' => (int)$_SESSION['id']]);

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $title = htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');
    $subject = htmlspecialchars($row['subjectname'], ENT_QUOTES, 'UTF-8');
    echo "<tr><td>$title</td><td>$subject</td>";
    if (strcasecmp(trim($row['status']), 'applied') === 0) {
        echo "<td>Already applied</td>";
    } else {
        $href = 'request-for-approval.php?id='.urlencode($row['cid']).'&sid='.urlencode($row['sid']);
        echo "<td><a href=\"$href\">Apply</a></td>";
    }
    echo "<td>".htmlspecialchars($row['status'])."</td></tr>\n";
}

Tie-back: was right to suggest conditional output rather than deleting DB rows; ignore the DELETE idea from @LastMitch unless you actually want to remove records. If you need the header removed entirely (only when no rows have actions), iterate once to detect any actionable rows, then print the header conditionally. Also consider adding a unique constraint on requestrecord (coursesid, accountsid) so users can't apply twice.

Recommended Answers

All 4 Replies

Member Avatar for Member #949455

@Riu 2009

i want to diplay offered courses to all the students and if the student whose course is accepted i dont want to display <th>action</th> in that row..

I'm very confused of what you are asking?

You mean you want to delete a row from the DB?

Then used this:

$sql = "DELETE FROM table WHERE row =''";

If this is not what you asking. Then explain a little more what you talking about.

@LastMitch
no i donot want to remove a row from db i donot want to display

echo "<td><a href='request-for-approval.php?id=".$row['cid']."& sid=".$row['sid']."'>Apply!</a></td>"; 

when the status is applied

basically i want to remove the option for apply if user already applied hope u understand my question

How about something like:

echo "<td>";
if ("I must display") {
    echo "<a href='request-for-approval.php?id=".$row['cid']."& sid=".$row['sid']."'>Apply!</a>";
}
else {
    echo "&nbsp;";
}
echo "</td>";

yeah sort of :) thanx for ur responce

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.