user6618037 user6618037 - 3 months ago 6
PHP Question

Am I using if, elseif and else incorrectly?

Here is the code to start with:

*<?php
$sql_query = "SELECT * FROM DistributorCourses WHERE Distributor = '". $Distributor ."'";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);

echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 1) {
echo '<tr>';
echo '<th>Course Title</th> ';
echo '<th>Price</th>';
echo '<th>Purchase</th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>'. $row['Price'] .'</td> ';
if ($row['Title'] == $course && in_array("". $row['Title'] ."", $descriptions)) {
echo '<td><a href="#" title="Retake Exam" class="btn btn-warning">Retake Exam</a></td>';
}

elseif (in_array("". $row['Title'] ."", $descriptions)) {
echo '<td><a href="#" title="You have already purchased this course!" class="btn btn-default" style="background: #e6e6e6; border: 1px solid #adadad; cursor: default;">You have already purchased this course!</a></td>';
}

else {
echo '<td><a href="purchase.php?courseid='. $row['CourseID'] .'&title='. $row['Title'] .'" title="" class="btn btn-success">Purchase Course</a></td>';
}
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>*


This code works brilliantly however... The first
elseif
statement does not work and I do not understand why. Can anyone figure out why?

I have an array which is
$descriptions
, I want to make it so if the Course Title
($row['Title'])
is in
$descriptions
, to display "Retake Exam" as shown. At the moment it still says 'You have already purchased this course!'.

$sql_query = "SELECT * FROM CustomisePage WHERE Distributor = '$Distributor'";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);

if($count == 1) {
$Image = $row['Image'];
$ImageAlt = $row['ImageAlt'];
$headercolour = $row['headercolour'];
$footercolour = $row['footercolour'];
}

$sql_query1 = "SELECT order_description FROM single_user_orders WHERE username = '". $_SESSION['login_user'] ."'";
$result1 = mysqli_query($dbconfig, $sql_query1);
$count1 = mysqli_num_rows($result1);

if($count1 >= 1) {
while ($row1 = $result1->fetch_assoc()) {
$descriptions[] = $row1['order_description'];
}
}

$sql_query2 = "SELECT pass_or_fail, course FROM single_user_exam_results WHERE user_on_course = '". $_SESSION['login_user'] ."'";
$result2 = mysqli_query($dbconfig, $sql_query2);
$count2 = mysqli_num_rows($result2);

if($count2 >= 1) {
while ($row2 = $result2->fetch_assoc()) {
$passfail[] = $row2['pass_or_fail'];
$course[] = $row2['course'];
}
}

Answer

If I see this right, you said that $course is an array. Just do the same with this like you did after that.

From:

if ($row['Title'] == $course && in_array("". $row['Title'] ."", $descriptions)) {

To:

if (in_array($row['Title'], $course) && in_array($row['Title'], $descriptions)) {

That could do the thing.

Comments