calculating the total of values from database with the same id - php

I want to calculate the sub total and grand total of values of amount_collected column with the same id. Here is my code:
<?php
include 'includes/config/db_connection.php'; // include database connection
$sql = "SELECT DISTINCT(gmt_no) FROM driver_travel";
$result = $db->query($sql);
$errorInfo = $db->errorInfo();
echo $error = $errorInfo[2];
while($row = $result->fetch())
{
$sql1 = "SELECT Balance FROM driver_travel WHERE gmt_no =". $row['gmt_no'];
$result1 = $db->query($sql1);
$errorInfo = $db->errorInfo();
$error = $errorInfo[2];
$gmtnocount = $result1->rowCount();
$gmtno[$gmtnocount] = Array();
$bal[] = Array();
$i = 0;
while($getrow = $result1->fetch())
{
echo $gmtnocount. " ";
$bal[$i] = $getrow['Balance']. "<p>";
$countbal = count($bal);
echo $countbal;
$i++;
}
}
?>
The result is
444455666

Related

Query Debugging.

I have two tables i am using two quires to fetch data from them separately.
$query = mysql_query("SELECT * FROM offers");
$query1 = mysql_query("SELECT * FROM Offers_Dalia ") or die(mysql_error());
I am printing the result on one html-table.
.... the problem is that i want to know that why the loop is running for 11 times both.
1). I am using two while loops. 2). each loop is running for row->mysql_fetch_object times. please help me to find error why my both loops are running for 11 times. total 22 rows are present in html table output. but my both table have so many rows. 100 of rows in both table.
please check the code.
$query = mysql_query("SELECT * FROM offers");
$query1 = mysql_query("SELECT * FROM Offers_Dalia ") or die(mysql_error());
$x = 1;
$n = 0;
while($row = mysql_fetch_object($query))
{
$oid = $row->id;
$name = stripslashes($row->name);
$reward = stripslashes($row->credits);
$network = stripslashes($row->network);
$hits = $row->hits; //clicks; $epc = $row->epc;
$campid = stripslashes($row->campaign_id);
$country = $row->countries;
$url2=$row->link;
$desc=$row->description;
if($x%2 == 0)
{
$trColor = "f1f0f0";
}else
{
$trColor = "ffffff";
}
if(stristr($country, ","))
{
$country_arrs = explode(",", $country);
$country = implode(", ", $country_arrs);
}
?>
<tr style="background:#<?=$trColor?>" ><td><input type="checkbox" value="<?=$oid?>" name="ids[]" onclick="uncheckCheckAllbox(this)" /></td><td><?=$campid?>- campId</td><td><?=$name?></td><td><?=$reward?><td><?=$network?><td><?=$desc?></td><td>Click Here</td></td><td><?=$country?></td></tr>
$n++;
while($row1 = mysql_fetch_object($query1))
{
$uuid = $row1->offers_uuid;
$title1 = stripslashes($row1->title);
$info1 = stripslashes($row1->info_short);
$device_kind1 = stripslashes($row1->device_kinds);
$country1 = stripslashes($row1->target_groups_values);
// $status = toggleStatus($row->active);
$reward_dollar1 = $row1->reward_dollar; //clicks;
$url1 = $row1->url;
$network1 = 'Dalia';
$url1 = urldecode($url1);
if($x%2 == 0)
{
$trColor = "f1f0f0";
}else
{
$trColor = "ffffff";
}
if(stristr($country, ","))
{
$country_arrs = explode(",", $country);
$country = implode(", ", $country_arrs);
}
?>
<tr style="background:#<?=$trColor?>" ><td><input type="checkbox" value="" onclick="uncheckCheckAllbox(this)" /></td><td><?=$uuid?></td><td><?=$title1?></td><td><?=$reward_dollar1?></td><td><?=$network1?></td><td><?=$info1?></td><td>Click Here</td><td><?=$country1?></td></tr>
$n++;
$x++;
goto n;
}
n:
$x++;
}

How do i update each array item into mysqli database in one query

With my code below i have array item with multiple record i which to update each record into database with one query but only the last item of each array record was updated here is my code
:
<?php
require("init.php");
$sql = "SELECT item_name, quantity
FROM books WHERE book = 1644445";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query))
{
$da = $row["item_name"];
$qty = $row["quantity"];
$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
$productCount = mysqli_num_rows($sql);
if($productCount > 0)
{
while ($row = mysqli_fetch_array($sql))
{
$id = $row["id"];
$type = $row["name"];
$code = $row["recharge"];
}
}
$set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$coe=substr(str_shuffle($set), 0, 12);
if(preg_match('/(65265)/i', $type))
$type = "20";
if(preg_match('/(562546)/i', $type))
$type = "13";
if(preg_match('/(MTN)/i', $type))
$type = "12";
if(preg_match('/(56556)/i', $type))
$type = "16";
$disp = str_split($code, $type);
for($b = 0; $b<$qty; $b++){
$pin = "$disp[$b]";
$gam = array(0 => array("post" => $pin));
foreach($gam as $gg)
{
$pp = $gg["post"];
$go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
if($go)
{
echo "<br/> $pp";
echo "<br/> $coe";
}
}
}
}
?>
i appliciate your impact
Try this:
<?php
require("init.php");
$sql = "SELECT item_name, quantity
FROM books WHERE book = 1644445";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query))
{
$da = $row["item_name"];
$qty = $row["quantity"];
$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
$productCount = mysqli_num_rows($sql);
if($productCount > 0)
{
while ($row = mysqli_fetch_array($sql))
{
$id = $row["id"];
$type = $row["name"];
$code = $row["recharge"];
}
$set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$coe=substr(str_shuffle($set), 0, 12);
if(preg_match('/(65265)/i', $type))
$type = "20";
if(preg_match('/(562546)/i', $type))
$type = "13";
if(preg_match('/(MTN)/i', $type))
$type = "12";
if(preg_match('/(56556)/i', $type))
$type = "16";
$disp = str_split($code, $type);
for($b = 0; $b<$qty; $b++){
$pin = "$disp[$b]";
$gam = array(0 => array("post" => $pin));
foreach($gam as $gg)
{
$pp = $gg["post"];
$go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
if($go)
{
echo "<br/> $pp";
echo "<br/> $coe";
}
}
}
}
}
?>
The update is only executed if $productCount > 0.

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();

Multiple mysqli select with json

<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
$rowss = array();
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
print json_encode($rowss);
}
}
Output is:
[{"Diseases_type":"fever","id":"114"}][{"Diseases_type":"rhrh","id":"123"}]
How can i get this output:
[{"Diseases_type":"fever","id":"114"},{"Diseases_type":"rhrh","id":"123"}]
You need to initialize the JSON array before the outer loop, and print it at the very end, not each time through.
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "SELECT id from diseases";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysql_fetch_array($result_pag_data)) {
$diseases_id = $row['id'];
$result = mysqli_query($con,"SELECT $diseases_id FROM treatment WHERE gender_id = '10' AND diseases_id = '$diseases_id'");
$row_gid = mysqli_fetch_array($result);
if ($row_gid == TRUE){
$sl_dise = mysqli_query($con,"SELECT Diseases_type, id FROM diseases WHERE id = '$diseases_id'");
while($r = mysqli_fetch_assoc($sl_dise)) {
$rowss[] = $r;
}
}
}
print json_encode($rowss);
Here is you optimised code
<?php
include"../database_conn.php";
$con=mysqli_connect("localhost","admin","123456","ayurveadic");
$query_pag_data = "select diseases.Diseases_type,diseases.id from diseases inner join treatment on treatment.diseases_id = diseases.id where treatment.gender_id = '10' ";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$rowss = array();
while ($row = mysqli_fetch_assoc($result_pag_data)) {
$rowss[] = $row;
}
print json_encode($rowss);
?>
Put your
print json_encode($rowss);
after end of thiswhile ($row = mysql_fetch_array($result_pag_data)) {
Also remove this $rowss = array(); array initialization

Nested Navigation

I want to make a Navigation with 2 levels.
My Code so far
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[] = $row;
}
foreach ($list as $kat) {
echo '<li>' . $kat['name'] . '</li>';
}
?>
Nested Sets are at the moment too tricky for me.
I want at the end this.
<li>$kat['name']
<li>$kat['name'] from PID</li>
</li>
MySQL:
http://i46.tinypic.com/35052m0.png - IMG
No I want to get the things our of the MySQL DB see the image Link.
MySQL:
id—–pid——name
1——0——–name1
2——0——–name2
3——0——–name3
4——3——–name3.1
5——3——–name3.2
<?php
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='' ORDER BY name");
$result = mysql_query($sql);
$list = array();
while ($row = mysql_fetch_assoc($result)) {
$list[$row['id']] = $row;
$sql = ("SELECT name, id, pid FROM tl_table WHERE pid='".$row['id']."' ORDER BY name");
$res = mysql_query($sql);
while($rw = mysql_fetch_assoc($res)){
$list[$row['id']]['sub'][] = $rw;
}
}
echo "<pre>";
print_r($list);
?>

Categories