Div not to display if no results found - - php

I have an area on my page of coloured swatches (and the tile AVAIALBLE COLOURS) that i only want to display if there is a result for them in the database.
here is my code:
private function getWTVariationHighlights($productId, $variationId)
{
$output_str = "";
$output_str .="<div class=\"mainProductSwatchesTitle\"><b>AVAILABLE COLOURS</b>\n";
$output_str .= "<div class=\"mainProductSwatches\">\n";
$sql = "select voptionid, vovalue from [|PREFIX|]product_variation_options where vovariationid=$variationId and (voname='Colour' or voname='Color')";
$result = $this->db->Query($sql);
while ($row = $this->db->fetch($result)) {
$sql = "select * from [|PREFIX|]variation_option_highlight where variation_option_id=" . $row['voptionid'] . " and product_id=" . $productId;
$result2 = $this->db->Query($sql);
if($row2 = $this->db->fetch($result2)){
if($row2['thumb_location']){
$output_str .= "<a href=\"#\" onmouseover=\"changeMain('" . $row2['location'] . "')\"><img src=\""
. $GLOBALS['ShopPath'] . "/product_images/hfh_highlight_images/" . $row2['thumb_location'] . "\" /></a>\n";
}
}
}
$output_str .= "</div>\n";
$output_str .= "<script type=\"text/javascript\">\n";
$output_str .= "function changeMain(src)\n";
$output_str .= "{\n";
$output_str .= "document.getElementById('phthumb').src = '" . $GLOBALS['ShopPath'] . "/product_images/hfh_highlight_images/' + src;\n";
$output_str .= "}\n";
$output_str .= "</script>\n";
return $output_str;
}

simple use css
div:empty
{display:none;}

Related

MYSQL SELECT QUERY ONLY ONE DATA

view_products() below displays only one data from my table product which contains 20 data. Depending on where you place return $output; either inside while loop which displays the first data or outside the while loop which displays the last data.
<?php echo view_products(); ?>
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password#edadmin";
$dbname = "estore";
$dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//Test if connection occurred,
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")");
}
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($products = mysqli_fetch_assoc($result)) {
$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
return $output;
}
} else {
return "No product yet";
} // return $output;
}
The reason is that you are resetting the content of $output in the first line of your loop $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
So if you put the return at the end of the loop, in the first loop it will return the first record and exit the function, if you put it at the end of the function, in each loop $output will be cleared and the content of that loop will only be written in $output so at the end of the function you will only have the content of the last loop in $output
What you can to is to set $output to an empty string and then just append everything in your loop.
Also set the value of $output in the else block and then at the end return $output
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
$output = "";
while ($products = mysqli_fetch_assoc($result)) {
$output .= "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "&#x20a6 " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
}
} else {
$output = "No product yet";
} // return $output;
return $output;
}

Display comments for each post in PHP/mySQL site

I'm trying to display multiple posts, each with its own multiple comments. This, below, works for the first post (or, at least shows 2 of the 3 comments), but not for the rest. I searched but couldn't find (or didn't recognize) an answer to this exact problem on this site, or elsewhere. Thanks.
$result1 = #mysql_query('select * from posts,comments where comment_post_ID = ID and post_status="publish" and comment_approved="1" ORDER BY post_date,category,subcat1,subcat2 ASC');
$result2 = #mysql_query('select * from comments where comment_approved="1" ORDER BY comment_date');
$row = mysql_fetch_array($result1);
$row2 = mysql_fetch_array($result2);
while ($row = mysql_fetch_array($result1)) {
$post = $row['post_content'];
$id = $row['ID'];
$title = $row['post_title'];
$content = "<br /><h3 align=\"left\">" . $title . "</h3>\n";
$content .= "<h4 align=\"left\">by " . $row['post_author'] . " - " . $row['post_date'] . "</h4>\n";
$content .= "<p align=\"left\">" . var_export($post, true) . "</p>\n<p>\n";
echo $content;
while ($row2 = mysql_fetch_array($result2)) {
$comment = "<blockquote>";
$comment .= "<h4 align=\"left\">" . $row2['comment_author'] . " commented on " . $row2['comment_date'] . "</h4>\n";
$comment .= "<p align=\"left\">" . $row2['comment_content'] . "</p>\n<p>\n";
$comment .= "</blockquote>";
if($row2['comment_post_ID'] == $id) {
echo $comment;
}
}
}
This, below, finally worked as hoped:
$postData = mysqli_query($conn,"SELECT * FROM wp_posts ORDER BY post_date ASC) or die(mysqli_error());
$commentData = mysqli_query($conn,"SELECT * FROM wp_comments WHERE comment_approved = '1' ORDER BY comment_date ASC") or die(mysqli_error());
$posts = array();
while($row = mysqli_fetch_assoc($postData)) {
$posts[] = $row;
$commentrow = mysqli_fetch_assoc($commentData);
$comments[] = $commentrow;
echo '<h3>' . $row['post_title'] . '</h3>';
echo '<h5>' . $row['post_author'] . ', ' . $row['post_date'] . '</h5>';
echo '<p>' . $row['post_excerpt'] . '... read more</p>';
if($row['comment_count'] > 0) {
echo '<blockquote>';
echo '<b>Comments</b><br />';
foreach($comments as $comment) {
if($row['ID']==$comment['comment_post_ID']) {
$comment_excerpt = substr($comment['comment_content'],0,100);
echo '<br>' . $comment_excerpt . ' - <b>' . $comment['comment_author'] . '</b>, ' . $comment['comment_date'] . '<br>';
}
}
echo '</blockquote>';
}
}

MySQL fetch array not collecting and returning all the rows | PHP | MySQL

I have a function (see below)
public function ReadBets_Open($id) {
$queryBase = "SELECT * FROM `vr_wp_bets` WHERE `is_open` = 'true' AND `id` = '%s';";
$queryBase2 = sprintf($queryBase, $id);
$selectQuery = mysql_query($queryBase2);
$return = "<div style='max-height: 400px; overflow: scroll;'>";
while ($result = mysql_fetch_array($selectQuery)) {
//var_dump($result);
$return = "<div style='border: 1pt solid black; width: 99%;'>";
$return .= "<h2>" . $result['title'] . "</h2>";
$return .= "<table>";
$return .= "<tr><td style='width:50%;'>Sport: </td><td>" . $result['sport'] . "</td></tr>";
$return .= "<tr><td style='width:50%'>Participant: </td><td>" . $result['participant'] . "</td></tr>";
$return .= "<tr><td>Market: </td><td>" . $result['market'] . "</td></tr>";
$return .= "<tr><td>Time: </td><td>" . date("H:i", strtotime($result['bettilltime'])) . "</td></tr>";
$return .= "<tr><td>Odds: </td><td>" . $result['odds'] . "</td></tr>";
$return .= "<tr><td>Stake: </td><td>£" . $result['stake'] . "</td></tr>";
if ($result['is_ew'] == "true") {
$return .= "<tr><td>Each Way: </td><td>" . $this->CalculateEachWay($result['odds'], $result['ew_odds']) . "</td></tr>";
$return .= "<tr><td>Estimated Return:</td><td>" . "N/A" ."</td></tr>";
}
else if ($result['odds'] == "SP") {
$return .= "<tr><td>Estimated Return:</td><td>" . "N/A" ."</td></tr>";
}
else {
$return .= "<tr><td>Estimated Return:</td><td>£" . $result['estimated_return'] ."</td></tr>";
}
$return .= "</table>";
$return .= "</div><br>";
}
$return .= "</div>";
return $return;
}
And I know for a fact that there is 4 results in this that should be selected, however only one is returned by the fetch array, I was wondering if anyone can see an issue with this and if so what is it? This has had me stumped for a few days now and I really need an answer to this.
The way that I call the method is:
$classInstance->ReadBets_Open($current_user->ID);
NOTE
I am aware that I am using depreciated mysql_* functionality, this will be changed in the future, this needs to be made and released.
You are resetting $return each time you loop. So by the end of the loop $return will only have the last row's details.

Sorting nested While() Loops

I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}

Else statement doesn't work in option select

I am trying to implement a dropdown search option. All my search results are working. All the commands that I have assigned to if statements work, but when it does to else it deosn't work.
Here is my code:
if(isset($_REQUEST['submit'])){
$opt = $_POST['opt'];
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}else{
echo "Your Searched keyword did not match";
}
}
What to do?
Try this: Take a flag to check if record exists.
$flag = false;
if($opt==1){//if opt = 1
$sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
$resulte = mysql_query($sqle,$con) or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($resulte)){
echo "<h4>" . $row['title'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}else if($opt==2){//if opt = 2
$sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
$results = mysql_query($sqls,$con)or die(mysql_error());
if(mysql_num_rows($resulte) > 0) {
$flag = true;
while($row=mysql_fetch_array($results)){
echo "<h4>" . $row['games_name'] . "</h4><br/>";
echo "<p>" . $row['description'] . "<p>";
}
}
}
if(!$flag){
echo "Your Searched keyword did not match";
}

Categories