I have this following PHP code in search.php to gather information from the form in the same file. It's a search engine and when I put something into the search bar I get only one row even when I should get more than one row.
Here is php script:
<?php
$error = "";
$con=mysqli_connect('localhost', 'root', '');
$db=mysqli_select_db($con, 'vodici');
if(isset($_POST['button'])){ //trigger button click
$search=$_POST['search'];
$query=mysqli_query($con,"select distinct * from users where meno like '%{$search}%' || priezvisko like '%{$search}%' || mesto like '%{$search}%' || kraj like '%{$search}%' || rok_narodenia like '%{$search}%' || email like '%{$search}%' ");
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
}
mysqli_close($con);
?>
Here is form:
<form class="search-form" action="" method="post">
<div class="input-group">
<input name="search" type="text" class="form-control" placeholder="Vyhľadať...">
<div class="input-group-append">
<button class="btn btn-secondary" type="submit" name="button"><i class="fa fa-search"></button></i>
</div>
</div>
</form>
<p><?php if(isset($_POST['button']) && $error == "") {
while($row = mysqli_fetch_array($query)) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}
}?></p>
<p><?php if(isset($_POST['button']) && $error !== "") {echo $error;}?></p>
What can I do here? I am out of ideas
Thanks
You only added one row. You need to add each row to the generated html.
$result = '';
if (mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
$div = "<div style='border: 1px solid black; border-radius: 5px;'>".$row['id'];
$vysledok = "<h2 style='text-decoration: underline; padding-left: 2%;'>".$row['meno']." ".$row['priezvisko']."</h2>";
$mail = "<p style='padding-left : 2%; font-size: 11px;'><strong>Rok narodenia:</strong> ".$row['rok_narodenia']." <br><strong>E-mail: </strong> ".$row['email']."</p>";
$mesto = "<p style='padding-left: 2%'><strong>Mesto:</strong> ".$row['mesto']." <strong>Kraj:</strong> ".$row['kraj']."</p>";
$div_end = "</div>";
$result .= $div. $vysledok. $mail.$mesto. $div_end ;
}
}else{
$error = "Nič sme nenašli :/ <br><br>";
}
echo $result;
}
In compensation to wakeels answer, that is correct, I would like to ask you to use prepared statements like in my example below
$sql="
SELECT `id`,`meno`,`priezvisko`,`rok_narodenia`,`email`,`mesto`,`kraj`
FROM `users`
WHERE
`meno` LIKE '%?%'
OR `priezvisko` LIKE '%?%'
OR `mesto` LIKE '%?%'
OR `kraj` LIKE '%?%'
OR `rok_narodenia` LIKE '%?%'
OR `email` LIKE '%?%'
;"
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $search);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $div,$vysledok,$mail,$mesto,$div_end;
}
I am trying to set the variable $OnWishList to display a tick or a plus symbol depending on if the property is in the wish list. Though it does this it skips the first property in the wile loop and shows the correct symbol for the first property on the second property. How can I get the symbols to show on the correct properties. I apologize if this is a simple question as I am new to PHP and SQL and have struggled with this problem for a while.
$city = (isset($_GET['city'])) ? $_GET['city'] : 0;
$suburb = (isset($_GET['suburb'])) ? $_GET['suburb'] : 0;
$minBed = (isset($_GET['minBed'])) ? $_GET['minBed'] : 0;
$maxBed = (isset($_GET['maxBed'])) ? $_GET['maxBed'] : 0;
$minBath = (isset($_GET['minBath'])) ? $_GET['minBath'] : 0;
$maxBath = (isset($_GET['maxBath'])) ? $_GET['maxBath'] : 0;
$minPrice = (isset($_GET['minPrice'])) ? $_GET['minPrice'] : 0;
$maxPrice = (isset($_GET['maxPrice'])) ? $_GET['maxPrice'] : 0;
$pagingVariable = '';
$cityName ='';
//}
$rowsPerPage = 8; // edit the number of rows per page
$query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property. price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image, tbl_city.cityName FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID INNER JOIN tbl_city ON tbl_property.city_ID=tbl_city.city_ID";
//$query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property. price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID";
$pagingLink = getPagingLink($query, $rowsPerPage);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
if($city == 0){
$query .= " WHERE ";
}
if($city != 0){
$query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property.price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image, tbl_city.cityName FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID INNER JOIN tbl_city ON tbl_property.city_ID=tbl_city.city_ID WHERE tbl_property.city_ID ='$city'";
$pagingVariable .= "&city=".$_GET['city'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($suburb != 0){
$query .= "AND suburb_ID ='$suburb'";
$pagingVariable .= "&suburb=".$_GET['suburb'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBed != 0 && $maxBed == 0 && $city == 0){
$query .= " bedrooms >='$minBed'";
$pagingVariable .= "&minBed=".$_GET['minBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBed != 0 && $city != 0){
$query .= "AND bedrooms >='$minBed'";
$pagingVariable .= "&minBed=".$_GET['minBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxBed != 0 && $minBed == 0 && $city == 0){
$query .= " bedrooms <='$maxBed'";
$pagingVariable .= "&maxBed=".$_GET['maxBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxBed != 0 && $minBed == 0 && $city != 0){
$query .= "AND bedrooms <='$maxBed'";
$pagingVariable .= "&maxBed=".$_GET['maxBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBed != 0 && $maxBed != 0 && $city != 0){
$query .= "AND (bedrooms BETWEEN '$minBed' AND '$maxBed')";
$pagingVariable .= "&minBed=".$_GET['minBed']."&maxBed=".$_GET['maxBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBed != 0 && $maxBed != 0 && $city == 0){
$query .= " (bedrooms BETWEEN '$minBed' AND '$maxBed')";
$pagingVariable .= "&minBed=".$_GET['minBed']."&maxBed=".$_GET['maxBed'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if ( ((($minBed != 0 || $maxBed != 0) && $city != 0)) || ((($minBed != 0 || $maxBed != 0) && $city == 0)) || ((($minBed == 0 && $maxBed == 0) && $city != 0))){
$query .= " AND";
}
if($minBath != 0 && $maxBath == 0 && $city == 0){
$query .= " bathrooms >='$minBath'";
$pagingVariable .= "&minBath=".$_GET['minBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBath != 0 && $maxBath == 0 && $city != 0){
$query .= " bathrooms >='$minBath'";
$pagingVariable .= "&minBath=".$_GET['minBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBath != 0 && $maxBath != 0 && $city == 0){
$query .= " (bathrooms BETWEEN '$minBath' AND '$maxBath')";
$pagingVariable .= "&minBath=".$_GET['minBath']."&maxBath=".$_GET['maxBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minBath != 0 && $maxBath != 0 && $city != 0){
$query .= " (bathrooms BETWEEN '$minBath' AND '$maxBath')";
$pagingVariable .= "&minBath=".$_GET['minBath']."&maxBath=".$_GET['maxBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxBath != 0 && $minBath == 0 && $city == 0){
$query .= " bathrooms <='$maxBath'";
$pagingVariable .= "&maxBath=".$_GET['maxBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxBath != 0 && $minBath == 0 && $city != 0){
$query .= " bathrooms <='$maxBath'";
$pagingVariable .= "&maxBath=".$_GET['maxBath'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
//
//
if ($minBath != 0 || $maxBath != 0){
$query .= " AND";
}
if($minPrice != 0 && $maxPrice == 0 && $city == 0){
$query .= " Price >='$minPrice'";
$pagingVariable .= "&minPrice=".$_GET['minPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minPrice != 0 && $maxPrice == 0 && $city != 0){
$query .= " Price >='$minPrice'";
$pagingVariable .= "&minPrice=".$_GET['minPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minPrice != 0 && $maxPrice != 0 && $city == 0){
$query .= " (Price BETWEEN '$minPrice' AND '$maxPrice')";
$pagingVariable .= "&minPrice=".$_GET['minPrice']."&maxPrice=".$_GET['maxPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($minPrice != 0 && $maxPrice != 0 && $city != 0){
$query .= " (Price BETWEEN '$minPrice' AND '$maxPrice')";
$pagingVariable .= "&minPrice=".$_GET['minPrice']."&maxPrice=".$_GET['maxPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxPrice != 0 && $minPrice == 0 && $city == 0){
$query .= " Price <='$maxPrice'";
$pagingVariable .= "&maxPrice=".$_GET['maxPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if($maxPrice != 0 && $minPrice == 0 && $city != 0){
$query .= " Price <='$maxPrice'";
$pagingVariable .= "&maxPrice=".$_GET['maxPrice'];
$pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}
if (mysqli_num_rows($result) < 1) {
$noResults = "Sorry, no results were found!";
} else {
while($row = mysqli_fetch_array($result)) {
extract($row);
?>
<div class="col-md-6">
<div class="propertyCardOuter card">
<div title="add to watchlist" class="displayWishAdd"><div class="plusSymbol"><?php echo $OnWishList;?></div></div>
<a class="propertyCardLink" href="viewProperty.php?propertyID=<?php echo $row['property_ID']; ?>"><div class="card propertyCard">
<div class="propertyImgContainer">
<img alt="Photo" class="PropertyImagesHome img-fluid" src="<?php echo 'property-images/'.$row['property_image']; ?>" title="<?php echo $row['name']; ?>" />
</div>
<div class="row">
<div class="col-md-9 propertyInfoBox">
<div class="propertyName">
<?php echo $row['name']; ?></div>
<div style="font-size: 14px;">
<?php echo $row['location']; ?>
</div>
<hr>
<div class="houseDetails">
<?php echo $row['bedrooms']; ?> <i class="fa fa-bed icons"></i><?php echo $row['bathrooms']; ?>
<i class="fa fa-bath icons"></i><?php echo $row['garage']; ?>
<i class="fa fa-car icons"></i><?php echo $row['landsize'] . 'sqm'; ?>
<img src="images/landSizeIcon.png" width="19px;" alt="landsize"/>
<span class="propertyPrice"><?php echo '$' . number_format($row['price']); ?></span>
</div>
</div>
<div class=" col-sm-12 col-md-3">
<div class="agentContainer">
<div class="agentPhotoContainer">
<img alt="Photo" class="agentImage" src="<?php echo 'property-images/'.$row['agent_image']; ?>" title="photo" />
</div>
<div style="text-align:center; margin-top: 10px;"><?php echo $row['agent_name']; ?></div></div>
</div>
</div>
</div></a></div>
<h3 style="text-align:right;">
</h3>
</div>
<?php
$propertyID2 = $row['property_ID'];
$query2 = "SELECT * FROM tbl_wishlist WHERE member_ID='$memberID' && property_wishList_ID='$propertyID2' ";
$result2 = mysqli_query($link, $query2); // execute the SQL
if ($row = mysqli_fetch_array($result2)) {
$OnWishList = "<span id='tickSpan'>✓</span>";
}
else {
$OnWishList = "<span id='plusSpan'>+</span>";
}
}
} // end of while loop
}
?>
I would like the $OnWishList variable to display the tick and plus symbols starting with the first property in the while loop. However the symbols are displayed skipping the first property in the loop and showing its symbol on the second property and so on.
It is generally considered, though there is some debate, that it yields better performance to craft a prepared statement before any loops and execute multiple times within a loop with different variables being passed in. That was the intention here in using a prepared statement and assigning variables to placeholders in the sql.
By moving this code to near the top of the loop on the first iteration into the loop it will be called before you attempt to use the variable $OnWishList
<?php
$sql='select * from `tbl_wishlist` where `member_id`=? and `property_wishlist_id`=?';
$stmt=$link->prepare( $sql );
$stmt->bind_param( 'ii', $memberID, $wishlistid );
while( $row = mysqli_fetch_array( $result ) ) {
extract( $row );
$wishlistid=$row['property_ID'];
$stmt->execute();
$stmt->store_result();
$count=$stmt->num_rows;
$OnWishList=$count > 0 ? "<span id='tickSpan'>✓</span>" : "<span id='plusSpan'>+</span>";
?>
<div class="col-md-6">
<div class="propertyCardOuter card">
<a href="addWishList.php?propertyID=<?php echo $row['property_ID']; ?>">
<div title="add to watchlist" class="displayWishAdd">
<div class="plusSymbol"><?php echo $OnWishList;?></div>
</div>
</a>
<a class="propertyCardLink" href="viewProperty.php?propertyID=<?php echo $row['property_ID']; ?>">
<div class="card propertyCard">
<div class="propertyImgContainer">
<img alt="Photo" class="PropertyImagesHome img-fluid" src="<?php echo 'property-images/'.$row['property_image']; ?>" title="<?php echo $row['name']; ?>" />
</div>
<div class="row">
<div class="col-md-9 propertyInfoBox">
<div class="propertyName">
<?php echo $row['name']; ?>
</div>
<div style="font-size: 14px;">
<?php echo $row['location']; ?>
</div>
<hr>
<div class="houseDetails">
<?php echo $row['bedrooms']; ?><i class="fa fa-bed icons"></i>
<?php echo $row['bathrooms']; ?><i class="fa fa-bath icons"></i>
<?php echo $row['garage']; ?><i class="fa fa-car icons"></i>
<?php echo $row['landsize'] . 'sqm'; ?><img src="images/landSizeIcon.png" width="19px;" alt="landsize"/>
<span class="propertyPrice"><?php echo '$' . number_format($row['price']); ?></span>
</div>
</div>
<div class=" col-sm-12 col-md-3">
<div class="agentContainer">
<div class="agentPhotoContainer">
<img alt="Photo" class="agentImage" src="<?php echo 'property-images/'.$row['agent_image']; ?>" title="photo" />
</div>
<div style="text-align:center; margin-top: 10px;"><?php echo $row['agent_name']; ?></div>
</div>
</div>
</div>
</div>
</a>
</div>
<h3 style="text-align:right;"></h3>
</div>
<?php
}//end while loop
?>
There is huge error in your second if clause:
if ($row = mysqli_fetch_array($result2)) {
// ^ only one equal sign !!
$OnWishList = "<span id='tickSpan'>✓</span>";
}
else {
$OnWishList = "<span id='plusSpan'>+</span>";
// this will NEVER be executed
}
So instead checking if the row is equal to something, you are actually checking, if $row can be assigned mysqli_fetch_array($result2) Therefore, the else clause will be executed only if you cannot assign new value to the $row variable.
Check first iteration in while loop like
<?php
$i=1;
while($row = mysqli_fetch_array($result)) {
?>
<?php
if($i==1){
echo "Yes or your code";
}
else
{
echo "no or your code";
}
?>
<?php $i++; } ?>
I have run program in simpal query it is work.
<?php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['search']))
{
$category = $_POST['category_id'];
$country = $_POST['country_id'];
$sql = "SELECT DISTINCT p.name,p.product_id,pr.image
FROM oc_product_description p, oc_product_filter pf ,oc_product_to_category ptc, oc_product pr
WHERE p.product_id=pf.product_id AND p.product_id=ptc.product_id AND p.product_id=pr.product_id
AND ptc.category_id=$category AND pf.filter_id=$country";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "</br>name: " . $row["name"];
$abc = $row["product_id"];
//echo "</br>id: " . $row["image"];
//echo "</br>id: " . $row["description"];
?>
<div>
<div class="row" style="border: 1px solid;height: 210px;padding: 10px 10px;">
<div class="product-block item-full" itemtype="http://schema.org/Product" itemscope="">
<div class="block-img" style="width: 13%;float: left;margin-right: 10px;" >
<?php
$temp = $row["name"];
$str = explode(" ",$temp);
$pname = implode("",$str);
?>
<div class="image" >
<a class="img" itemprop="url" title="<?php echo $row['name']; ?>" href="http://hrmssystem.com/<?php echo $pname; ?>">
<img class="img-responsive" itemprop="image" src="image/<?php echo $row['image']; ?>" title="<?php echo $row['name']; ?>" alt="<?php echo $row['name']; ?>" />
</a>
</div>
</div>
<div class="product-meta" style="height: 30%;width: 85%;float: left;">
<div style="height: 100%; width: 100%; ">
<h3 class="name" itemprop="name">
<a href="http://hrmssystem.com/<?php echo $pname; ?>">
<?php echo $row['name'];
?>
</a>
</h3>
<?php
$sql2 = "SELECT description from oc_product_description where product_id = $abc ";
$res = $conn->query($sql2);
if ($res->num_rows > 0) {
// output data of each row
while($col = $res->fetch_assoc()) {
//echo $col["description"];
if( isset($col['description']) ){
$des = utf8_substr( strip_tags($col['description']),0,220);
?>
<p style="display: block;" class="description" itemprop="description"><?php echo $des; ?>...</p>
<?php }
} } ?>
<div class="cart" style="float: right;margin-top: -30px;">
<a style="color: #3498DB;" class="btn btn-default" href="http://www.hrmssystem.com/Get-Quote">Get Quote</a><?php //echo $button_cart; ?>
</br>
<a style="color: #3498DB;" class="btn btn-default" href="http://www.hrmssystem.com/Request-demo">Request Demo</a><?php //echo $button_cart; ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
} else {
echo "0 results";
}
}
$conn->close();
?>
Above Code is running. But I have select only category than no result. But I have Select category and country than i have find result of search.
<?php
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['search']))
{
$category = $_POST['category_id'];
$country = $_POST['country_id'];
$sql = "SELECT DISTINCT p.name,p.product_id,pr.image, pf.filter_id
FROM oc_product_description p, oc_product_filter pf ,oc_product_to_category ptc, oc_product pr ";
//$val = array();
if(isset($category) && !empty($category)){
$sql .= ' WHERE p.product_id=pf.product_id AND p.product_id=ptc.product_id AND p.product_id=pr.product_id AND ptc.category_id=$category ';
}else(isset($country) && !empty($country)){
$sql .= ' WHERE p.product_id=pf.product_id AND p.product_id=ptc.product_id AND p.product_id=pr.product_id AND pf.filter_id=$country';
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "</br>name: " . $row["name"];
$abc = $row["product_id"];
//echo "</br>id: " . $row["image"];
//echo "</br>id: " . $row["description"];
?>
<div>
<div class="row" style="border: 1px solid;height: 210px;padding: 10px 10px;">
<div class="product-block item-full" itemtype="http://schema.org/Product" itemscope="">
<div class="block-img" style="width: 13%;float: left;margin-right: 10px;" >
<?php
$temp = $row["name"];
$str = explode(" ",$temp);
$pname = implode("",$str);
?>
<div class="image" >
<a class="img" itemprop="url" title="<?php echo $row['name']; ?>" href="http://hrmssystem.com/<?php echo $pname; ?>">
<img class="img-responsive" itemprop="image" src="image/<?php echo $row['image']; ?>" title="<?php echo $row['name']; ?>" alt="<?php echo $row['name']; ?>" />
</a>
</div>
</div>
<div class="product-meta" style="height: 30%;width: 85%;float: left;">
<div style="height: 100%; width: 100%; ">
<h3 class="name" itemprop="name">
<a href="http://hrmssystem.com/<?php echo $pname; ?>">
<?php echo $row['name'];
?>
</a>
</h3>
<?php
$sql2 = "SELECT DISTINCT description from oc_product_description where product_id = $abc ";
$res = $conn->query($sql2);
if ($res->num_rows > 0) {
// output data of each row
while($col = $res->fetch_assoc()) {
//echo $col["description"];
if( isset($col['description']) ){
$content = substr(strip_tags(htmlspecialchars_decode($col['description'])),0,220) . "...";
?>
<p style="display: block;width: 87%;" class="description" itemprop="description"><?php echo $content; ?></p>
<?php }
} } ?>
<div class="cart" style="float: right;margin-top: -90px;">
<a style="color: #3498DB;" class="btn btn-default" href="http://www.hrmssystem.com/Get-Quote">Get Quote</a><?php //echo $button_cart; ?>
</br>
<a style="color: #3498DB;" class="btn btn-default" href="http://www.hrmssystem.com/Request-demo">Request Demo</a><?php //echo $button_cart; ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
} else {
echo "No Result Found";
}
}
$conn->close();
?>
Try this:
$string = "";
if(isset($category) && !empty($category))
{
$string .= ' AND ptc.category_id=$category ';
}
if(isset($country) && !empty($country))
{
$string .= ' AND pf.filter_id=$country ';
}
$sql = "SELECT DISTINCT p.name,p.product_id,pr.image, pf.filter_id
FROM oc_product_description p, oc_product_filter pf ,oc_product_to_category ptc, oc_product pr
WHERE p.product_id=pf.product_id AND p.product_id=ptc.product_id AND p.product_id=pr.product_id
$string
";
Please remove if - else condition and use only if condition and use variable to store the condition.
I'm working on a real basic chat that is mostly php powered. I'd like to use jQuery to check for new messages and update the #cbox div every few seconds. So far I have tried accomplishing that using the code below. For a frame of reference, this is my first time including jQuery into any of my scripts, so the problem is likely a very simple error that I am overlooking in my newbishness.
The problem I'm running into is the #cbox div displays blank, it doesn't seem to be calling to chat_post.php at all. I will post that code as well, in case the problem is with that and not with my jQuery. To fix, I tried also including <div id='cbox'> in chat_post.php, but that yielded no results. At this point, I'm pretty much grasping at straws.
<div id='sb_content'>
<center><div id='chat'>
<div id='ribbon' style='position: relative; margin-top:-50px; margin-left:-32px;'><center><span class='ribbon'>chat box</span></center></div>
<div style='margin-top: -20px;'><center><span style='text-shadow: 0 0 0.2em #000;' class='admin'>admin</span> || <span style='text-shadow: 0 0 0.2em #000;' class='mod'>mod</span></center></div>
<div id="cbox">
<script>
$(document).ready(function() {
setInterval(function(){getUpdates()}, 2000);
});
function getUpdates() {
$("#cbox").load("chat_post.php");
}
</script>
</div>
<form name="message" method='post' action="chat_post.php" id="cbox_input">
<input name="usermsg"id='usermsg' type="text" size="63" maxlength="255" />
</form>
</div></center>
</div>
chat_post.php
<div id='cbox' align='left'>
<?php
$username = $_SESSION['login'];
$ug = $_SESSION['user_group'];
// Display messages
$sql = <<<SQL
SELECT *
FROM `chat_entries`
ORDER BY `ts` DESC
LIMIT 0,50
SQL;
$result = $db->query($sql);
$count = $result->num_rows;
if ($count != 0){
while ($row = mysqli_fetch_array($result)) {
$c_name = $row['author'];
$c_text = $row['text'];
$c_ts = $row['ts'];
$c_id = $row['id'];
$sqlm = <<<SQL
SELECT *
FROM `users`
WHERE username = '$c_name'
SQL;
$resultm = $db->query($sqlm);
$countm = $resultm->num_rows;
if ($count != 0){
while ($rowm = mysqli_fetch_array($resultm)) {
$c_level = $rowm['user_group'];
}
}
if ($c_level == 'mod') {
echo "
<a href='/user/" . $c_name . "' class='mod'>" . $c_name . "</a>
";
}
if ($c_level == 'admin') {
echo "
<a href='/user/" . $c_name . "' class='admin'>" . $c_name . "</a>
";
}
if ($c_level == 'member') {
echo "
<a href='/user/" . $c_name . "' class='member'>" . $c_name . "</a>
";
}
if ($c_level == 'guest') {
echo "
<i>" . $c_name . "</i>
";
}
echo "
: " . $c_text . "<div id='cbox_div'>";
echo "<span style='float:left;'><i>" . $c_ts . "</i></span>";
echo "<span style='float:right;'>";
if ($ug == 'mod' || $ug == 'admin'){
echo " <a href='#'>Delete</a> || <a href='#'>Block</a> ||";
}
if ($_SESSION['login']) {
echo "Report</span></div>
";
}
}
}
?>
After removing the extra id on chat_post.php, my code worked just fine. Funny how something so simple can really mess things up. (: Thank you everyone for your help!
I am developing a website and i have run into an issue. Basically if you view http://nexusdesign.org.uk/irejobs/training-courses.html it says no result, but in the backend it shows clearly that these have been added to the CMS the same as job listings etc have.
Now ive checked the database and any listings in the backend are showing there just not showing on the main website, the coding for this area is below:
<?php
ob_start();
require_once("conn.php");
$pagename = "training";
$submenu = "trainings";
require_once("header.php");
require_once("pager.php");
$__table = "db_trainings";
$__page = $_SERVER['PHP_SELF'];
$__id = "TrainingID";
$__uploadfolder = "myimages/";
$NUM_COL = 2;
$NUM_ROW = 5;
$perPage = $NUM_COL * $NUM_ROW;
$page=isset($_GET['page']) ? intval($_GET['page']):1;
$MemberID = $_GET['MemberID'];
if($MemberID != "")
$andmember = " and MemberID = '".$MemberID."'";
$category = "";
$i = 0;
$r1 = mysql_query("select * from db_training_categories") or die(mysql_error());
while($a1 = mysql_fetch_array($r1))
{
if($_GET['category-'.$a1['CatID']] == "1")
{
if($i != 0)
$andcategory .= " or ";
$andcategory .= " t.category = ".$a1['CatID'];
$i++;
}
}
if($andcategory != "")
$andcategory = " and (".$andcategory.")";
$city = $_GET['city'];
if($city == "enter city")
$city = $_GET['citys'];
if($city != "" and $city != "enter city")
$andcity = " and t.city = '".$city."'";
$country = $_GET['country'];
if($country != "")
$andcountry = " and t.country = '".$country."'";
$stxt = $_GET['srchTxt'];
if($stxt == "enter keywords")
$stxt = $_GET['srchTxts'];
if($stxt != "" and $stxt != "enter keywords")
{
$srchTxt = " and (t.title like '".$stxt."%' or t.title like '%".$stxt."' or t.title like '%".$stxt."%'";
$srchTxt .= " or t.provider like '".$stxt."%' or t.provider like '%".$stxt."' or t.provider like '%".$stxt."%'";
$srchTxt .= " or t.city like '".$stxt."%' or t.city like '%".$stxt."' or t.city like '%".$stxt."%'";
$srchTxt .= " or t.terms like '".$terms."%' or t.city like '%".$stxt."' or t.terms like '%".$stxt."%'";
$srchTxt .= " or t.price like '".$stxt."%' or t.price like '%".$stxt."' or t.price like '%".$stxt."%'";
$srchTxt .= " or t.duration like '".$stxt."%' or t.duration like '%".$stxt."' or t.duration like '%".$stxt."%'";
$srchTxt .= " or t.qualification like '".$stxt."%' or t.qualification like '%".$stxt."' or t.qualification like '%".$stxt."%'";
$srchTxt .= " or t.awardingbody like '".$stxt."%' or t.awardingbody like '%".$stxt."' or t.awardingbody like '%".$stxt."%'";
$srchTxt .= " or t.description like '".$stxt."%' or t.description like '%".$stxt."' or t.description like '%".$stxt."%'";
$srchTxt .= " )";
}
if ($_GET['sortby'] == "newest") {
$sortby = "newest";
$sortbyt = " order by t.TrainingID desc";
}
elseif ($_GET['sortby'] == "oldest") {
$sortby = "oldest";
$sortbyt = " order by t.TrainingID asc";
}
else
$sortbyt = " order by t.TrainingID desc";
$textQuery = "select *,t.city,t.country from db_trainings t, db_members m where t.MemberID <> '' and t.MemberID = m.MemberID and m.advertisementexpiry > 0 and t.active = 'yes' ". $andmember . $andcountry . $andcity . $andcategory . $andmember . $srchTxt . $sortbyt;
//for paging
$ress = mysql_query($textQuery);
$total=mysql_num_rows($ress);
$maxpage = floor($total/$perPage);
if($total%$perPage !=0)
$maxpage += 1;
if($page == 0)
{
$start = 0;
$page = 1;
}
else
$start = ($perPage * $page) - $perPage;
$stop = $start + $perPage;
$qry = $textQuery." limit $start, $perPage";
//echo $qry;
$r = mysql_query($qry);
?>
<script language="javascript">
function showRecords(id)
{
document.frmItems.page.value = id;
document.frmItems.method = "get";
document.frmItems.action = "training-courses.html";
document.frmItems.submit();
}
function validateForm()
{
document.frm.action = "training-courses.html";
document.frm.submit();
}
</script>
<div id="container">
<div class="wrap">
<div class="corner-box-bot">
<?
if($_SESSION['MemberType'] == 'Employer')
include("employer-menu.html");
elseif($_SESSION['MemberType'] == 'Seeking')
include("seeker-menu.html");
elseif($_SESSION['MemberType'] == 'Training')
include("training-menu.html");
?>
<div class="content">
<div style="float:left;width:280px;"><h1>Training & Skills</h1>
<strong class="blue" style="line-height: 31px; font-size: 14px;">
Upgrade
your skills and qualifications?<br />
Find out what's available near you:
<br /><br />
</strong></div>
<div style="float:right;width:550px;">
<p><strong>Training providers: </strong>use www.IrishNursingJobs.ie to effectively promote your courses to students in the UK & internationally .</p>
<strong>Reach out and speak to a global market of clients and students</strong>
<?
if($_SESSION['MemberID'] == "")
echo "<a href='login-training.html' class='blue-btn-04' style='font-size:13px;margin-top:20px;'>ADVERTISE YOUR COURSES HERE</a>";
else
echo "<a href='training-courses-add.html' class='blue-btn-04' style='font-size:13px;margin-top:20px;'>ADVERTISE YOUR COURSES HERE</a>";
?>
</div>
<div class="clear"></div>
<form name="frmItems" id="frmItems">
<input type="hidden" name="page" value="">
<input type="hidden" name="MemberID" value="<?=$MemberID?>">
<input type="hidden" name="srchTxts" value="<?=$stxt?>">
<div class="side_bar">
<h1 style="line-height:1;">Advertising</h1>
</div>
<div class="col_710 r-content">
<?
if(mysql_num_rows($r) > 0)
{
$query = $_SERVER['QUERY_STRING'];
$arr = explode('&', $query);
if(strchr($arr[count($arr)-1],'page')!='')
array_pop($arr);
$purl = 'training-courses.html?'.implode('&', $arr);
?>
<div class="search-box">
<div>
<label>Training Title (e.g. Nurse)</label>
<input type="text" class="txt-field" name="srchTxt" />
</div>
<div>
<label>Training Location (e.g. Dublin)</label>
<input type="text" class="txt-field" name="citys" value="<?=$city?>" />
</div>
Search
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="sort-bar">
<span style="float: left; display: block; padding-top: 4px; width: 420px;"><strong class="blue"><?=$total?> courses found</strong></span>
<span style="float: left; display: block; padding-top: 4px;">Sort by</span>
<div class="custom-select-2" id="select-box-05">
<select name="sortby" id="sortby" class="input" onChange="showRecords(1);">
<option value="newest" <? if($_GET['sortby']=='newest') echo "selected"; ?>>Newest</option>
<option value="oldest" <? if($_GET['sortby']=='oldest') echo "selected"; ?>>Oldest</option>
</select>
</div>
<div class="clear"></div>
</div>
<div style="padding: 25px 0 0 25px;">
<?
$i=0;
while($a = mysql_fetch_array($r))
{
$r1 = mysql_query("select * from db_members where MemberID = '".$a['MemberID']."'") or die(mysql_error());
$a1 = mysql_fetch_array($r1);
$r2 = mysql_query("select * from db_categories where CatID = '".$a['category']."'") or die(mysql_error());
$a2 = mysql_fetch_array($r2);
$r3 = mysql_query("select * from db_countries where CountryID = '".$a['country']."'") or die(mysql_error());
$a3 = mysql_fetch_array($r3);
$r4 = mysql_query("select * from db_training_categories where CatID = '".$a['category']."'") or die(mysql_error());
$a4 = mysql_fetch_array($r4);
echo "
<div class='block-result'>
<img alt='' src='".$__uploadfolder.$a['logo']."' width='134' />
<div class='desc'>
<a href='training-courses-view.html?TrainingID=".$a['TrainingID']."'> ".cleaninput($a['title'],"ss|he|tr")."</a>
<span class='blue'>Location:</span> ".cleaninput($a['city'].", ".$a3['Name'],"ss|he|tr")."<br/>
<span class='blue'>Sector:</span> ".cleaninput($a4['CatName'],"ss|he|tr")."<br/>
<span class='blue'>Price:</span> ".$currency.cleaninput($a['price'],"ss|he|tr")."<br/>
<span class='blue'>Duration:</span> ".cleaninput($a['duration'],"ss|he|tr")." hours<br/>
</div>
</div>
";
$i++;
}
?>
</div>
<?
if($maxpage > 1)
echo showpage($page,$purl,$total,$maxpage,$perPage);
}
else
echo "
<div class='sort-bar' style='margin-top: 0px'>
<span style='float: left; display: block; padding-top: 4px; width: 420px;'><strong class='blue'>No results found...</strong></span>
<div class='clear'></div>
</div>
";
?>
</div>
</form>
<div class="clear"></div>
</div>
</div>
<? include("bottom-featuredcourses.php"); ?>
<div class="clear"></div>
</div>
</div>
<?php
require_once("footer.php");
ob_flush();
?>
Can anyone advise whats going wrong?