Php count cannot work for my notification - php

When I echo ''.$count.'' its display 11101101. But I want to total count like 8 or 7 etc. What is my wrong here in my code please.
I used to try $count = mysqli_num_rows($u); also but result was same.
my code:
$g = mysqli_query($dbh,"SELECT id FROM update WHERE from_id`='".$b."' OR `to_id`='".$session->id."'") or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($g)) {
$ids[]= $rows['id'];
}
foreach ( $ids as $id ){
$u = mysqli_query($dbh,"SELECT id FROM updateside WHERE `id`='".$id."' AND `view` = '0'") or die(mysqli_error($dbh));
$count = mysqli_affected_rows($dbh);
while ($rows = mysqli_fetch_assoc($u)) {
$nid= $rows['id'];
}
echo ''.$count.'';
}

Assign total count to a variable something like this:
Assumes mysqli_num_rows needs query result $u as argument.
$total = 0;
foreach ( $ids as $id ){
$u = mysqli_query($dbh,"SELECT id FROM updateside WHERE `id`='".$id."' AND `view` = '0'") or die(mysqli_error($dbh));
$count = mysqli_num_rows($u);
$count = ($count == "") ? 0 : $count;
$total = $total + $count;
while ($rows = mysqli_fetch_assoc($u)) {
$nid= $rows['id'];
}
}
echo $total;

You will need to change your code a bit like this
foreach ( $ids as $id ){
$u = mysqli_query($dbh,"SELECT id FROM updateside WHERE `id`='".$id."' AND `view` = '0'") or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($u)) {
$nid= $rows['id'];
}
echo mysqli_num_rows($u);
}
If you need to echo after all foreach statements use this
$count=0;
foreach ( $ids as $id ){
$u = mysqli_query($dbh,"SELECT id FROM updateside WHERE `id`='".$id."' AND `view` = '0'") or die(mysqli_error($dbh));
while ($rows = mysqli_fetch_assoc($u)) {
$nid= $rows['id'];
}
$count = $count + mysqli_num_rows($u);
}
echo $count;

I think your issue is simply formatting bug. Why use the '' here?
Try simply write:
echo $count;

you can simply use
$count=$u->num_rows;
echo $count;

Related

how to get count(*) the right way in php

I tried to count() the data from database with php but it don't show me the total data but it show the datas.
this is how I count
$query = "SELECT
mitra.*,
user.username,
user.privilege,
user.name
FROM
mitra
INNER JOIN
user ON mitra.id_user = user.id "
$result = $connection->query($query);
if ($result->num_rows > 0) {
foreach ($result as $row) :
$id = "" . $row["id"] . "";
$total = "" . $row["total_puas"] . "";
$privilege = "" . $row["privilege"] . "";
if ($privilege == 2) :
$calculate = $total / count($id);
var_dump(count($id));
endif;
endforeach;
}
===================
= id = total =
= 1 = 45.84 =
= 2 = 75.45 =
= 3 = 34.54 =
===================
when I var_dumb it it shows int(1)int(1)int(1) not int(3) that what I wanted.
actually I want to count $calculate with the data in $total that should be there is float and the amount from $total that I want to divided with count id
is there any solution that how to count the amount from $total and can be devided with count $id that should be 3?. please help
what I really trying to do from that table example is like 45.84 + 75.45 + 34.54 / 3
Sounds like you want a COUNT() with GROUP BY in your query. Doing count($id) in PHP will always yield one, as its not an array of values.
$query = "SELECT COUNT(id) as cnt, id, total_puas
FROM table
GROUP BY id";
$result = $connection->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$calculate = $row["total_puas"] / $row['cnt'];
echo $row['id']." has a count of ".$row['cnt'].", a total of ".$row['total_puas']." and calculated to ".$calculate."<br />\n";
}
}
From your updated question, the output becomes a bit more clear. Based on the output data and the result you desire, you want to calculate the sum of all total_paus, divided by the number of rows. You can do this directly in one query, like this
$query = "SELECT SUM(total_puas) / COUNT(u.id) as total
FROM mitra AS m
INNER JOIN user AS u
ON mitra.id_user = user.id";
$result = $connection->query($query);
$row = $result->fetch_assoc();
$total = $row['total'];
echo $total;
You can try this code:
<?php
$i = 0;
$total =0.00;
if ($result->num_rows > 0) {
while ($row = $result->fetch_array()):
if ($row["privilege"] == 2) :
$total = $total + $row["total"];
$i++;
endif;
endwhile;
echo $total."<br>";
echo $i."<br>";
echo $calculate = $total / $i;
}
?>
output
=====================================
$total = 155.83;
$i = 3;
$calculate = $total/$i;
$ans = 51.943333333333;
=====================================
You can try this code:
$query = "SELECT * FROM table"
$result = $connection->query($query);
if ($result->num_rows > 0) {
$total = 0.0;
foreach ($result as $row) :
$id = "" . $row["id"] . "";
$total = $total + $row['total'];
endforeach;
$calculate = $total / $result->num_rows;
echo $total.<br>;
echo $calclulate;
}

Count all foreach PHP

I've got a question:
I have the following code:
foreach($_COOKIE as $key => $value) {
$sql = "SELECT * FROM producten WHERE id='$key'";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) {
$prijs = $row['prijs'];
echo $prijs;
}
}
What I want to do is to count all the $prijs variables (int).
I tried to use count($prijs) but didn't work.
To get the count of number of time loop, you can use counter variable and to get the sum up, you can use += to sum the values in loop.
If you want to sum up only numeric values, you can use is_numeric() as well.
$i=0;
$sum=0;
while($row = $result->fetch_assoc()) {
$prijs = $row['prijs'];
$sum += $prijs;
$i++;
}
echo $i; //will give you count
echo $sum; //will give you sum
I think you've got your wording mixed up and you mean summing up the values of $prijs, in that case, use a new variable and add to it:
$sum = 0;
foreach($_COOKIE as $key => $value) {
$sql = "SELECT * FROM producten WHERE id='$key'";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) {
$prijs = $row['prijs'];
echo $prijs;
$sum += $prijs;
}}
As per your code, $prijs will only returns single value, so when ever you are trying to count it will only gives a 1. But if you want to set all the values come up and store as an array please try $prijs[] = $row['prijs']
try
$i=0;
foreach($_COOKIE as $key => $value) {
$sql = "SELECT * FROM producten WHERE id='$key'";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) {
$prijs = $row['prijs'];
echo $prijs;
$i++;
echo $i;
}
}
You can simplify your code as follows:
$values = array_map('mysqli_real_escape_string', array_keys($_COOKIE));
$sql = "SELECT * FROM producten WHERE id in ('".implode("','",$values."')";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) {
$prijs = $row['prijs'];
echo $prijs;
}
echo "Total rows: ".$result->num_rows;

Why Getting only 1 array instead of many arrays?

I am a completely newbie in programming php I would like to make this code below return many arrays(to flash as3), however I only receive one array.Can anyone please pinpoint what is my mistake here? thanks.
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
echo "returnStr=$data_array";
exit();
}
When you write your exit insight your loop you stop executing your program and you get only one record. You should set the echo and exit after your while loop.
$data_array = "";
$i = 0;
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql)) {
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1) {
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
} else {
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
Those two last line of your should be outside of your loop:
$data_array = "";
$i = 0;
//if(isset($_POST['myrequest']) && $_POST['myrequest'] == "get_characters")
//{
$sql = mysqli_query($conn, "SELECT * FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql))
{
$i++;
$fb_name = $row["Username"];
$fb_id = $row["Fb_id"];
$fb_at = $row["Access_token"];
$fb_sig = $row["Fb_sig"];
$char_id = $row["Char_id"];
if($i == 1)
{
$data_array .= "$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
else
{
$data_array .= "(||)$fb_name|$fb_id|$fb_at|$fb_sig|$char_id";
}
}
echo "returnStr=$data_array";
exit();
If you would name the columns that you want in the SELECT then it's much simpler. Make sure to use MYSQLI_ASSOC in the fetch:
$sql = mysqli_query($conn, "SELECT Username, Fb_id, Access_token, Fb_sig, Char_id FROM ns_users ORDER BY Char_id");
while($row = mysqli_fetch_array($sql, MYSQLI_ASSOC))
{
$data_array[] = implode('|', $row);
}
echo "returnStr=" . implode('(||)', $data_array);
exit();

foreach doesn't look like working properly in php

I used following code to get the total price, but it outputs value that is higher than the expected value. What would be the reason for this?
$query = " SELECT *
FROM `mytable`
WHERE `sale_id` = $id";
$result = mysql_query($query);
$item = mysql_fetch_array($result);
foreach ($item as $row) {
$total_price += $row['price'];
}
echo $total_price;
Try this,
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$total_price += $row['price'];
}
echo $total_price;

error printing array when looping over it

Sorry for the relatively newbie question.
Ive been stuck the past hour trying to figure out why the array named $propname does not want to print when i loop over it. Here follows my code
$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or die(mysql_error()) ;
while($row = mysql_fetch_array($result)) //create arrays
{
$id[] = $row['player_id'];
$propName[] = $row['name'];
$propLastName[]= $row['surname'];
}//end while
//create variables for looping
$x = sizeof($propName);
$index = 0;
while($index < $x) //print variables
{
echo $propName[$index];
$index ++;
}
Just simplify your code
$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or die(mysql_error()) ;
while($row = mysql_fetch_array($result)) //create arrays
{
echo $row['name'];
}
$result = mysql_query("SELECT * FROM `player_info` WHERE `position` = 'THP'") or die(mysql_error()) ;
while($row = mysql_fetch_assoc($result)) //create arrays
{
$id = $row['player_id'];
echo $propName = $row['name'];
$propLastName = $row['surname'];
}

Categories