Array and DESC LIMIT - php

Here's my problem:
$q = 'SELECT * FROM s_stats WHERE srv_id='.$sid.' ORDER BY date DESC LIMIT 5';
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ($row[percent] == null) // don't work
$procent[] = 1;
else
$procent[] = $row[percent];
}
$procent[] = implode('-', $procent);

Try: if ($row["percent"] == null || $row["percent"] == "")

try
if ($row[percent] === null)
When using the non-strict == operator, 0 == null and '' == null will evaluate to true as well, which is probably not desirable.

$q = 'SELECT * FROM s_stats WHERE srv_id='.$sid.' ORDER BY date DESC LIMIT 5';
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo '*', $row['percent'], '*<br/>';
if (!isset($row["percent"]))
$procent[] = 1;
else
$procent[] = $row[percent];
}
$procent[] = implode('-', $procent);
and print:
12
4
66

Maybe if (! isset($row['percent'])) instead of if ($row['percent'] == null)

Related

implode() glue not working PHP

The expected behaviour of the code is
dynamically assign value to array (this is done a few times) and then print it
Code:
$PLLinks = array();
$sql245 = "SELECT `Username`, `Password`, `MacAddress` FROM `Users` WHERE `Username` LIKE '$guestUsername' AND `Password` LIKE '$guestPassword' AND `MacAddress` LIKE '$guestMacAddress'";
$result4 = mysql_query($sql245);
if (mysql_num_rows($result4) == 1) {
for ($i = 0; $i<count($Checknames); $i++)
{
if ($PLNames[$i] == 'Valid' && $PLChecked[$i] == 'Invalid'){
$result = mysql_query("SELECT `Link` FROM `Checksums` WHERE `Name` LIKE '$ValidNames[$i]' LIMIT 1");
$value = mysql_fetch_object($result);
$PLChecked[] = $value->{'Link'};
echo $value->{'Link'};
}
else if ($PLNames[$i] == 'Valid' && $PLChecked[$i] != 'Invalid') {
$PLLinks[] = 'noUpd';
}
else if ($PLNames[$i] != 'Valid') {
$PLLinks[] = 'Remove';
}
}
echo implode(',', $PLLinks);
// this should print "a,b,c" but instead it prints "abc"
}
If the solution is simple, please hint it only.
Thanks

Update multiple variables in an IF Statement

I am having some trouble updating multiple variables inside an IF Statement and not sure where i'm going wrong.
Here is my code:
$id = $_POST['retrive_quantity'];
$n_quantity = $_POST['n_quantity'];
$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");
while ($row1 = mysql_fetch_array($result2))
{
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
$orders_status=$row1['orders_status'];
$orders=$row1['orders'];
}
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0 && $orders_status=0 && $orders=0;
} else {
$update=2 && $orders_status=1 && $orders=$orders;
}
mysql_query("UPDATE stock_control SET quantity=quantity + '$n_quantity',
email_sent = '$update', orders_status = '$orders_status', orders = '$orders'
WHERE id = '$id' ");
The first line of the IF Statement works and also with just one of the variables, but fails with multiple.
You can not update variables like this you have to separate with ;
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0;
$orders_status=0;
$orders=0; // or $update = $orders_status = $orders=0;
} else {
$update=2;
$orders_status=1;
//$orders=$orders; //no need for this
}
Declare the variable before used in if block
$id = $_POST['retrive_quantity'];
$n_quantity = $_POST['n_quantity'];
$update=2;
$orders_status=1;
$orders=0;
$result2 = mysql_query ("SELECT * FROM stock_control WHERE id = '$id' ");
while ($row1 = mysql_fetch_array($result2))
{
$quantity=$row1['quantity'];
$threshold=$row1['threshold'];
$emailSent=$row1['email_sent'];
$orders_status=$row1['orders_status'];
$orders=$row1['orders'];
}
if ($quantity + $n_quantity > $threshold && $emailSent == 2) {
$update=0;
$orders_status=0;
$orders=0;
} else {
$update=2;
$orders_status=1;
$orders=$orders;
}
mysql_query("UPDATE stock_control SET quantity=quantity + '$n_quantity',
email_sent = '$update', orders_status = '$orders_status', orders = '$orders'
WHERE id = '$id' ");

How to run a function inside a function if a condition is met

Please I've been trying to create a function that would query a DB, select from the table, and if the row count is not equal to 6, then a row from the table and repeat (or maybe duplicate) until the row count is equal to 6. I've search for this here in StackOverflow, but didn't get anything close. Please if you have a similar link to this, please post it here, and I'll give it a go.
Here's my code:
//List All active adverts
function showActiveAdverts()
{
$status = 1;
//Build final queries.
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
# My question here, check if the $count >= 1 && $count != 6, then do getDefaultBannner() and repeat it until it runs for 6 times.
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Here's the code for getDefaultBannner()
function getDefaultBannner()
{
$status = 6;
$query = mysql_query("SELECT id FROM table WHERE status = '".mysql_real_escape_string($status)."' ")
or die(mysql_error());
$count = mysql_num_rows($query);
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}
Thanks in advance!
You can rewrite your code as this
//List All active adverts
$query = mysql_query("SELECT * FROM table WHERE
status = '".mysql_real_escape_string($status)."' ORDER BY rand() LIMIT 6") or die(mysql_error());
$count = mysql_num_rows($query);
if($count >= 1 && $count != 6)
$list = getDefaultBannner();
else
$list = showActiveAdverts($query);
function showActiveAdverts($query)
{
$status = 1;
//Build final queries.
$row = mysql_fetch_assoc($query);
if($count >= 1){
do{
$list[] = $row['id'];
}while($row = mysql_fetch_assoc($query));
return $list;
}
else{ return FALSE; }
}

PHP SQL User Profile Completeness calculation not working

I'm trying to show a profile completeness bar on the users account and the progress bar is showing but it's not adding the number values in order to calculate the percentage of completed fields ie:
if($row['title'] != '')
$completedTitle = 20;
My shortened code is as follows:
<?php
$result = mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='$id' LIMIT 1");
while($row = mysql_fetch_assoc($result))
$maximumPoints = 100;
{
if($row['title'] != '')
$completedTitle = 20;
if($row['name'] != '')
$completedName = 20;
if($row['surname'] != '')
$completedSurname = 20;
if($row['identityno'] != '')
$dcompletedIdentityno = 20;
if($row['gender'] != '')
$completedGender = 20;
}
$percentage = ($dcompletedTitle+$completedName+$completedSurname+$completedIdentityno+$completedGender)*$maximumPoints/100;
echo "".$percentage."%";
?>
The percentage shows in the echo but the total is wrong - it's not taking the values of 20 points for each field that is completed and including them in the "addition" part of the percentage calculation. Please can you tell me where I'm going wrong - I've been trying to figure this out for 4 days and have googled this and read over 2000 forums but can't find the answer. Any help would be greatly appreciated.
I think you are getting it all wrong from your condition, try this... it worked for me
$sql = "SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='{$id}' LIMIT 1";
$result = $DBconnection->query($sql);
$maxvalue = 100;
$point = 0;
if ($result) {
while($row = $result->fetch_assoc()){
if($row['title'] != ''){
$point1 = $point+20;
}elseif($row['title'] == ''){
$point1 = $point+=0;
}
if($row['name'] != ''){
$point2 = $point+20;
}elseif($row['name'] == ''){
$point2 = $point+=0;
}
if($row['surname'] != ''){
$point3 = $point+20;
}elseif($row['surname'] == ''){
$point3 = $point+0;
}
if($row['identityno'] != ''){
$point4 = $point+20;
}elseif($row['identityno'] == ''){
$point4 = $point+0;
}
if($row['gender'] != ''){
$point5 = $point+=20;
}elseif($row['gender'] == ''){
$point5 = $point+0;
}
// otherwise
}
}else{
echo "error completing query";
}
$pint = $point1+$point2+$point3+$point4+$point5;
$percentage = ($pint*100)/100;
echo $percentage."%";
use {} around single quotes in query
<?php
// fisrt select database
$result = mysql_query("SELECT title,name,surname,identityno,gender FROM cic_candidates WHERE id='{$id}' LIMIT 1");
//use of MYSQL_QUERY is deprecated so don't use it
//use mysqli_query instead of mysql_query
if (!$result) {
echo "Could not successfully run query " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while($row = mysql_fetch_assoc($result))
$maximumPoints = 100;
$point = 0;
{
if($row['title'] != '')
$point+=20;
if($row['name'] != '')
$point+=20;
if($row['surname'] != '')
$point+=20;
if($row['identityno'] != '')
$point+=20;
if($row['gender'] != '')
$point+=20;
}
$percentage = ($point*$maximumPoints)/100;
echo $percentage."%";
?>

PHP OOP number of results

Can someone tell me how to return the number of results if I use this function to grab data from the database? I have tried including this:
$this->number = $result->num_rows;
But that didn't do the trick. Also, if anyone can give me some advice to do the below code in a better way, that would be helpful too.
<?php
public function grabResults($table, $values = '*', $where = NULL, $field1 = NULL, $and = NULL, $field2 = NULL, $order = NULL)
{
$result = 'SELECT '.$values.' FROM '.$table;
if($where != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where;
}
if($and != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where.' AND '.$field2.' = '.$and;
}
if($order != NULL)
{
$result = 'SELECT '.$values.' FROM '.$table.' WHERE '.$field1.' = '.$where.' ORDER BY '.$order.' ASC';
}
$query = $this->data->mysqli->query($result);
if($query)
{
while($row = $query->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
}
else {
return false;
}
}
?>
$result->num_rows;
sounds like a Codeigniter function that you tried to use in raw php.
Have you tried to count() the query?
$count=count($query);

Categories