Hiding links with 0 query value results by php - php

I have a menu block that contains some links like: Some_link1: 5pcs, Some_link2: 13pcs, Some_link3: 0pcs, Some_link4: 0pcs.
I want to hide link "Some_link" with 0pcs value. I write a code with MySQL query, but it's not working! "Some_link" with 0pcs not hiding but still show 0pcs value.
What i'm doing wrong or what my mistake? I can't understand. Thank you for help.
<?
$resultonline = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='Y' and saled='N'");
$resultonshafasaled = mysql_query("SELECT count(customers_id) from tbl_customers WHERE shafa='Y' and saled='Y'");
$resultonlinenonactive = mysql_query("SELECT count(customers_id) from tbl_customers WHERE active='N' and saled='N'");
$topmenuNotOnShafa = mysql_result($resultonshafasaled, 0);
$topmenuonline = mysql_result($resultonline, 0);
$topmenuoffline = mysql_result($resultonlinenonactive, 0);
$topmenuonlineText = "Some text : ";
$topmenuOnShafaText = "Some text 2 : ";
?>
<?php if ($topmenuonline!=0): ?><?=$topmenuonlineText;?><?php endif; ?>
<?php if ($topmenuonline!=0): ?><?=$topmenuonline;?>
<?php endif; ?>
<?php if ($topmenuoffline!=0): ?> / <?=$topmenuoffline;?>
<br /><?php endif; ?>
<?php if ($topmenuNotOnShafa!=0): ?>
<span class="saled-warning"><a href="some_link" target="_self" ><?=$topmenuNotOnShafa;?></a></span>
<?php endif; ?>

You can check if the value of the item is 0 or not and print it only if it is not 0:
Example:
<?php
$items='0';
if(isset($items)){
if($items != 0){
echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
} else {
echo "Oh sorry, there are no items!";
}
} else {
echo "items variable is not declared!";
}
?>
In this example you will get the else condition, if you change the variable $items to 1 you will get printed the html code. This is a small test, the variable can be the mysql query result, a manual input like this, etc.
If you dont want to print anything if the value is 0 or not declared, like I understand you want you can do only this:
<?php
$items='1';
if(isset($items)){
if($items != 0){
echo "<a href='non_zero_item.php'>Item from menu (".$items.")";
}
}
?>
For debuging I recommend you to use allways the else condition.

use
mysql_num_rows
<?php
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
?>
http://php.net/manual/en/function.mysql-num-rows.php

Related

Unable to return desired value, query and loop keeps returning i

Hello I was trying to put the results of the query to an array but it's not working intead it only displays i
<?php
$sqlo = mysqli_query($conn, "SELECT * FROM users");
$i=1;
while ($h=mysqli_fetch_assoc($sqlo)) {
echo "<br>counter[i] : ".$counter[$i] = $h['username'];
echo "<br>i++ : ".$i++;
}
?>
As Barmar suggestion is recommended separating the assignment from echo.
Try this code:
<?php
$sqlo = mysqli_query($conn, "SELECT * FROM users");
$i=1;
$counter = [];
while ($h=mysqli_fetch_assoc($sqlo)) {
$counter[$i] = $h['username']; //if you need to store username inside an array
echo "<br>counter[i] : ".$counter[$i];
echo "<br>i++ : ".$i++;
}
create array variable before loop.
$i=1;
$counter[];
while ($h=mysqli_fetch_assoc($sqlo)) {
echo "<br>counter[i] : ".$counter[$i] = $h['username'];
e
echo "<br>i++ : ".$i++;
}

rewrite queries from deprecated mysql_connect to PDO in PHP

i am rewriting code mysql_connect deprecated below to work in PDO but cannot get it to work properly. no error is showed. i have tried everything I could. can someone help me
deprecated mysql_connect
<?php include('config.php'); ?>
<?php
if(isset($_POST['page'])):
$paged=$_POST['page'];
$sql="SELECT * FROM `users` where qualify='Po' ORDER BY `uid` desc ";
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" LIMIT 0 , $resultsPerPage";
}
$result=mysql_query($sql.$pagination_sql);
$num_rows = mysql_num_rows($result);
if($num_rows>0){
while($data=mysql_fetch_array($result)){
$userid=$data['uid'];
$fullname=$data['fullname'];
echo "<li><h3>$userid</h3><p>$fullname<p></li>";
}
}
if($num_rows == $resultsPerPage){?>
<li class="loadbutton"><button class="loadmore" data-page="<?php echo $paged+1 ;?>">Load More</button></li>
<?php
}else{
echo "<li class='loadbutton'><h3>No More Data</h3></li>";
}
endif;
?>
convert to PDO
<?php
$resultsPerPage=1;
$db = new PDO (
'mysql:host=localhost;dbname=chat;charset=utf8',
'root', // username
'' // password
);
?>
<?php include('pdo.php'); ?>
<?php
if(isset($_POST['page'])):
$paged=$_POST['page'];
$prefix = "";
//Loadmore configuarion
$resultsPerPage=1;
$sql = $db->prepare("SELECT * FROM users where qualify=:qualify ORDER BY uid desc");
$sql->execute(array(':qualify'=>'po'));
if($paged>0){
$page_limit=$resultsPerPage*($paged-1);
$pagination_sql=" LIMIT $page_limit, $resultsPerPage";
}
else{
$pagination_sql=" LIMIT 0 , $resultsPerPage";
}
$result = $db->prepare($sql.$pagination_sql);
$num_rows = $result->rowCount();
if($num_rows>0){
while ($row = $result->fetch()) {
$userid=htmlentities($row['uid'], ENT_QUOTES, "UTF-8");
$fullname=htmlentities($row['fullname'], ENT_QUOTES, "UTF-8");
echo "<li><h3>$userid</h3><p>$fullname<p></li>";
}
}
if($num_rows == $resultsPerPage){?>
<li class="loadbutton"><button class="loadmore" data-page="<?php echo $paged+1 ;?>">Load More</button></li>
<?php
}else{
echo "<li class='loadbutton'><h3>No More Data</h3></li>";
}
endif;
?>
Thank you so much
Your solution here is to look at http://php.net/manual/en/pdo.setattribute.php and put the following code after your PDO construct:
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
This won't fix your code but it should show you why the error is occuring and you can debug it fully from there.

Hide the heading if mysql query is empty

Is there a way to hide a section of html if it is used to display a php/mysql query result that comes back empty. Below is the query:
try
{
$park_id = $_GET['park_id'];
$query2="SELECT `name` FROM `tpf_rides` WHERE `park_id` = $park_id AND `top_ride` = 1 ORDER BY `name` ASC";
$result2 = $pdo->query($query2);
}
catch (PDOException $e)
{
$output = 'Unable to pull rides.';
include 'output.html.php';
}
$output = 'Sucessfully pulled rides';
//include 'output.html.php';//
and the part of code used to display the results:
<h2>Top Attractions</h2>
<ul>
<?php foreach ($result2 as $row2): ?>
<li><h3><?php echo $row2['name']; ?></h3></li>
<?php endforeach; ?>
</ul>
<hr>
There are a number of parks on the site that don't yet have top attractions - indicated by "top_ride = 1". Rather than have "<h2>Top Attractions</h2>" show up with no rides listed below I would ideally have the whole code above not show if there are no "top ride = 1" for a particular park.
Is this possible?
Thanks
Try this;
<?php
if($count = $query2->rowcount() < 1) {
echo "No results found";
} else {
echo "<h2>Top Attractions</h2>";
foreach($result2 as $row2) {
if(!empty($row2['name'])) {
echo "<li><h3>{$row2['name']}</h3></li>";
} else {
}
}
}
?>

For Each loop not echoing data (mysql_fetch_assoc problem?)

Hello and Good Morning,
I am still learning PHP and for some reason my script will not post any data in my foreach loop. Any Idea why? The emailRow Echos out fine but I am going to remove My code is below:
<?php
include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
<?php echo $emailRow['fbID']; ?>
<?php echo $emailRow['firstName']; ?>
<?php echo $emailRow['lastName']; ?>
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
?>
<table>
<?php foreach($accountUser as $emailData) { ?>
<tr><td> <?php emailData['fbID']; ?> </td><td><?php emailData['firstName']; ?></td><td><?php emailData['lastName']; ?></td></tr>
<?php } ?>
</table>
You have constructed your SQL query in $emailQuery, but never executed it. Call mysql_query(), and pass its result resource to mysql_fetch_assoc().
$emailQuery = "SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0";
$result = mysql_query($emailQuery);
if ($result)
{
while($emailRow = mysql_fetch_assoc($result, $conn))
{
$accountUser[]=$emailRow;
}
}
else // your query failed
{
// handle the failure
}
Please also be sure to protect your database from SQL injection by calling mysql_real_escape_string() on $upgradeEmail since you're receiving it from $_GET.
$upgradeEmail = mysql_real_escape_string($_GET['currEmail']);
You don't actually echo anything.
as well as not running the query.
and there are some other methods to do things, much cleaner than usual uglyPHP.
a function
function sqlArr($sql){
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
if ($res) {
while($row = mysql_fetch_array($res)){
$ret[] = $row;
}
}
return $ret;
}
a code
$email = mysql_real_escape_string($_GET['currEmail']);
$data = sqlArr("SELECT * FROM users WHERE emailOne='$email' AND authLevel=0");
include 'template.php';
a template
<? include 'includes/header.php' ?>
<table>
<? foreach($data as $row) { ?>
<tr>
<td><?=$row['fbID']?></td>
<td><?=$row['firstName']?></td>
<td><?=$row['lastName']?></td>
</tr>
<? } ?>
</table>
YOU HAVE A SYNTAX ERROR. You can't open a new php tag within an existing php tag. You have to close the already open tag first.
As far as getting the query to work,
First you have to fetch data before printing it or echoing it...
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
then you may write statements..
echo $emailRow['fbID']; etc. code.
Secondly you have not fired a query, just written the query statement. Use mysql_query to fire it.
Your code would be something like this..
<?php include 'includes/header.php';
$accountUser = array();
$upgradeEmail = $_GET['currEmail'];
$emailQuery = mysql_query("SELECT fbID, firstName, lastName FROM users WHERE emailOne='".$upgradeEmail."' AND authLevel=0") or die (mysql_error());
while($emailRow = mysql_fetch_assoc($emailQuery, $conn))
{
$accountUser[]=$emailRow;
}
echo $emailRow['fbID'];
echo $emailRow['firstName'];
echo $emailRow['lastName'];
print '<table>';
foreach($accountUser as $emailData) {
print '<tr><td>'$.emailData['fbID'].'</td><td>'.$emailData['firstName'].'</td><td>'.$emailData['lastName'].'</td></tr>';
}
print '</table';
?>
Feel free to use this code, modifying it to fit your needs.

Help with PHP While function

Why is this not working?
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
$result = mysqli_fetch_assoc($query);
$title = mysqli_real_escape_string($connect,trim($result['title']));
$message = mysqli_real_escape_string($connect,trim($result['message']));
while(($result = mysqli_fetch_assoc($query))){
echo $title;
echo '<br/>';
echo '<br/>';
echo $message;
}
?>
where as this works -
<?php
echo $title;
?>
SORRY TO SAY, BUT NONE OF THE ANSWERS WORK. ANY OTHER IDEAS?
If your mysqli query is returning zero rows then you will never see anything printed in your while loop. If $title and $message are not set (because you would want reference them by $result['title'] & $result['message'] if that are the field names in the database) then you will only see two <br /> tags in your pages source code.
If the while loop conditional is not true then the contents of the while loop will never execute.
So if there is nothing to fetch from the query, then you won't see any output.
Does you code display anything, or skip the output entirely?
If it skips entirely, then your query has returned 0 rows.
If it outputs the <br /> s, then you need to check your variables. I could be wrong, not knowing te entire code, but generally in this case you would have something like
echo $result['title'] instead of echo $title
If $title and $message come from your mysql query then you have to access them through the $result array returned by mysqli_fetch_assoc.
echo $result['title'];
echo $result['message'];
Also if your using mysqli you'd be doing something like this:
$mysqli = new mysqli("localhost", "user", "password", "db");
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
print $row['title'];
}
$result->close();
}
Try this:
<?php
$result = mysql_query($query);
while($row = mysqli_fetch_assoc($result)){
echo $title.'<br/><br/>'.$message;
}
?>
Does this work;
<?php
$select = "select * from messages where user='$u'";
$query = mysqli_query($connect,$select) or die(mysqli_error($connect));
$row = mysqli_num_rows($query);
while(($result = mysqli_fetch_assoc($query))){
echo $result['title'];
echo '<br/>';
echo '<br/>';
echo $result['message'];
}
?>
Basically I've made sure that it's not picking the first result from the query & then relying on there being more results to loop through in order to print the same message repeatedly.

Categories