The error I get is Warning: Invalid argument supplied for foreach() , I have read the relevant post for foreach case, but I still dont know how to fix it. Can I know what to cause this warning and the way to fix it ?
Here is my code,
mysql_connect("localhost","psm","psm") or die("could not connect");
mysql_select_db("planner") or die ("could not find db!");
$output = '';
//collect
if(isset($_POST['search']))
{
$searchq =$_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$query = mysql_query("select c.*,u.username from login_user u left join calendar c on c.user_id = u.user_id where u.matrix LIKE '{$searchq}'" ) or die ("could not search");
$count = mysql_num_rows($query);
if ($count == 0){
$output = 'There are no search result!';
} else {
while($row = mysql_fetch_array($query)){
$searchq = [];
$searchq[] = $row;
}
}
}
foreach($searchq as $u) {
?>
<div class="well" align="left" width="65%" >
<fieldset>
<table>
<tr><th>Matrix No:
<?php echo $query['matrix']; ?>
<?php foreach($query as $c){
if($c['user_id'] == $u['user_id']){
if($c['cate'] == 0){
echo "<p style='background: red;'>Course Name : ";
}else{
echo "<p>Title : ";
}
?>
<?php echo $c['title']; ?></p>
<p>Description :<?php echo $c['description'];?></p>
<p>Location : <?php echo $c['location'];?></p>
<p>Start Time : <?php echo date('Y-m-d',$c['starttime']);?></p>
<p>End Time : <?php if($c['endtime'] != 0)echo date('Y-m-d',$c['endtime']);?></p>
<?php }}?>
</th></tr>
</table>
</div>
<?php
}
?>