Click here to Skip to main content
14,878,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have tried the following code but I get syntax error specially during concatination of PHP code and that of HTML markup inside the body of foreach loop.The following code snippet processes product filtering.Please help me ....

What I have tried:

<?php

........other php codes..........
........other php codes..........
........other php codes..........
........other php codes..........

PHP
    if ($total_row > 0) 
    {
        foreach ($result as $row) 
        {
            
            $filteredCourses .=?><div class="card" style="margin-top: 2%;">
                                    <img class="card-img-top" src="<?php echo str_replace("./","Teacher/",$row['Course Img Path']); ?>" alt="Card image cap">
                                    <div class="card-body">
                                        <h5 class="card-title" style="height: 70px; text-align:justify;"><?php echo $row['Course Title']; ?></h5>
                                        <p class="card-text text-muted" style="text-align:justify;height: 67px;">
                                            <?php
                                                if(isset($row['Short Desc']))
                                                {
                                                    if(strlen($row['Short Desc'])<=120) 
                                                    {
                                                        echo $row['Short Desc'];
                                                    } 
                                                    else 
                                                    { 
                                                        echo substr($row['Short Desc'],0,120) . "...";
                                                    }
                                                }
                                                else
                                                {
                                                    //do nothing
                                                }
                                            ?>
                                        </p>
                                        
                                        <table class="card-footer" style="margin-top: 20px;border-bottom: 1px solid lightgray;border-left: 1px solid lightgray;border-right: 1px solid lightgray;margin-bottom: 10px;">
                                            <tr style="text-align: center;"><td style="border-right: 1px solid gray;">Teacher</td><td style="border-right: 1px solid gray;">Medium</td><td>Lessions</td></tr>
                                            <tr style="text-align: center;"><td style="border-right: 1px solid gray;"><a href="" style="color:black"><?php echo $row['Teacher Name']; ?></a></td><td style="border-right: 1px solid gray;"><a href="" style="color:black"><?php echo $row['Medium of Teaching']; ?></a></td><td><?php echo crud::getRecordCountConditional('tbl_lession','lessionID','courseID',$row['Course ID']); ?></td></tr>
                                        </table>
                                        <hr>
                                        <p style="text-align: center;font-size: 20px;">Price:₹<del style="font-size: 20px;"><?php echo number_format($row['Original Price']);?></del> <span style="color: red;font-weight:bolder;font-size: 20px;"> <?php echo $row['Disco Price'];  ?>/-</span></p>
                                        <hr>
                                        <p class="card-text"><a href="courseDetails.php?courseID_Qry=<?php echo base64_encode($row['Course ID']); ?>" class="btn " style=" background-color: royalblue;width:100%;"> View Details</a></p>
                                        <p class="card-text"><a href="courseDetails.php?courseID_Qry=<?php echo base64_encode($row['Course ID']); ?>" class="btn btn-success" style="width:100%;"> Buy Now</a></p>                                      
                                    
                                        
                                    </div>
                                </div>
<?php   }
    } 
    else 
    {
        $filteredCourses = '<h3 style="margin-top: 5%;">No course Found</h3>';
    }
    echo $filteredCourses;
}

?>
Posted
Updated 28-Feb-21 6:04am
v6
Comments
Richard MacCutchan 28-Feb-21 11:25am
   
Where does the error occur?
Member 14649908 28-Feb-21 11:29am
   
hi....
the syntax error comes at $filteredCourses .=?>
Patrice T 28-Feb-21 11:36am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 28-Feb-21 11:58am
   
That suggests the something before that point is incorrect. You need to look more closely at the code that you have not shown.

1 solution

PHP
<pre><?php
require_once('include/DB.php');
include_once('include/crud.php');

if (isset($_POST["action_Key"])) 
{
    $query ="SELECT c.courseID AS 'Course ID',
                    c.courseTitle AS 'Course Title',
                    c.original_price AS 'Original Price',
                    c.discounted_Price AS 'Disco Price',
                    c.coverImageURL AS 'Course Img Path',
                    c.mediumOfTeaching AS 'Medium of Teaching',
                    u.userName AS 'Teacher Name'
            FROM tbl_course c INNER JOIN tbl_users u 
            ON c.userID=u.userID WHERE c.catID !=''";

    if (isset($_POST["minimum_price_Key"], 
              $_POST["maximum_price_Key"]) && 
              !empty($_POST["minimum_price_Key"]) && 
              !empty($_POST["maximum_price_Key"])) 
    {
        $query .= "AND c.discounted_Price BETWEEN '" . $_POST["minimum_price_Key"] . "' AND '" . $_POST["maximum_price_Key"] . "'";
    }
    if (isset($_POST["mediumofTeaching_Key"])) 
    {
        $medium_filter = implode("','", $_POST["mediumofTeaching_Key"]);
        $query .= "AND c.mediumOfTeaching IN('" . $medium_filter . "')";
    }
    if (isset($_POST["difficultyLevel_Key"])) 
    {
        $difficultyLevel_filter = implode("','", $_POST["difficultyLevel_Key"]);
        $query .= "AND c.mediumOfTeaching IN('" . $difficultyLevel_filter . "')";
    }

    
    $statement = $pdoObj->prepare($query);
    $statement->execute();
    $result    = $statement->fetchAll();
    $total_row = $statement->rowCount();
    $filteredCourses    = '';
    if ($total_row > 0) 
    {
        foreach ($result as $row) 
        {
            
            $filteredCourses .= '<div class="card" style="margin-top: 2%;">
                                    <img class="card-img-top" src="'.str_replace("./","Teacher/",$row['Course Img Path']) . '" alt="Card image cap">
                                    <div class="card-body">
                                        <h5 class="card-title" style="height: 70px; text-align:justify;">'. $row['Course Title'] . '</h5>
                                        <p class="card-text text-muted" style="text-align:justify;height: 67px;">';
                                            
                                                if(isset($row['Short Desc']))
                                                {
                                                    if(strlen($row['Short Desc'])<=120) 
                                                    {
                                                        echo $row['Short Desc'];
                                                    } 
                                                    else 
                                                    { 
                                                        echo substr($row['Short Desc'],0,120) . "...";
                                                    }
                                                }
                                                else
                                                {
                                                    //do nothing
                                                }
                                            
                                        '</p>
                                        
                                        <table class="card-footer" style="margin-top: 20px;border-bottom: 1px solid lightgray;border-left: 1px solid lightgray;border-right: 1px solid lightgray;margin-bottom: 10px;">
                                            <tr style="text-align: center;"><td style="border-right: 1px solid gray;">Teacher</td><td style="border-right: 1px solid gray;">Medium</td><td>Lessions</td></tr>
                                            <tr style="text-align: center;"><td style="border-right: 1px solid gray;"><a href="" style="color:black">'. $row['Teacher Name'] . '</a></td><td style="border-right: 1px solid gray;"><a href="" style="color:black">'. $row['Medium of Teaching'] . '</a></td><td>'.  crud::getRecordCountConditional('tbl_lession','lessionID','courseID',$row['Course ID']) . '</td></tr>
                                        </table>
                                        <hr>
                                        <p style="text-align: center;font-size: 20px;">Price:₹<del style="font-size: 20px;">' . number_format($row['Original Price']) . '</del> <span style="color: red;font-weight:bolder;font-size: 20px;">' . $row['Disco Price'] . '/-</span></p>
                                        <hr>
                                        <p class="card-text"><a href="courseDetails.php?courseID_Qry='. base64_encode($row['Course ID']) .'" class="btn " style=" background-color: royalblue;width:100%;"> View Details</a></p>
                                        <p class="card-text"><a href="courseDetails.php?courseID_Qry='. base64_encode($row['Course ID']) .'" class="btn btn-success" style="width:100%;"> Buy Now</a></p>                                      
                                    
                                        
                                    </div>
                                </div>';
       }
    } 
    else 
    {
        $filteredCourses = '<h3 style="margin-top: 5%;">No Such course Found....</h3>';
    }
    echo $filteredCourses;
}

?>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900