php nested do-while loop terminating after first iteration - php

I'm using nested do-while loop for accessing two json result, unfortunately the loop terminates after the first iteration where it returns outer-loop json result and not executing the inner-loop. Please find the function where am using nested do-while loop:
public function data_report($fromdate, $todate)
{
$lp = new log_report();
$mysqli = new mysqli("172.16.10.102", "aventador", "RND#ISO-3306", "eTrans");
if ($mysqli->connect_errno)
{
$response["success"] = 0;
}
if (!$mysqli->multi_query("call sp_Android_Online_Dashboard('$fromdate','$todate')"))
{
$response["success"] = 0;
}
else {
//1st Iteration
do {
if ($res = $mysqli->store_result())
{
$response["values"] = array();
while ($row = $res->fetch_assoc())
{
$i=1;
$value = array();
$value["location_name"] = $row["location_name"];
$value["location_id"] = $row["location_id"];
$value["Totfiles"] = $row["Totfiles"];
$arr = explode(":", $row["file_minutes"], 2);
$value["file_minutes"] = $arr[0];
$value["Total_Lines"] = round($row["Total_Lines"], 0, PHP_ROUND_HALF_UP);
array_push($response["values"], $value);
echo $i++;
}
if (!$mysqli->multi_query("call sp_get_Android_Online_minutes_Chart('$fromdate','$todate')"))
{
$response["success"] = 0;
}
//2nd Iteration
do {
if ($res = $mysqli->store_result())
{
$response["Chart_data"] = array();
while ($row = $res->fetch_assoc())
{
$j=5;
$value = array();
$value["File_Day"] = $row["FileDay"];
$value["File_Minutes"] = $row["Tot_minutes"];
array_push($response["Chart_data"], $value);
}
$response["success"] = 1;
echo json_encode($response, JSON_NUMERIC_CHECK);
$res->free();
}
echo $j++;
} while($mysqli->more_results() && $mysqli->next_result());
}
echo json_encode($response, JSON_NUMERIC_CHECK);
} while($mysqli->more_results() && $mysqli->next_result());
}
}
Please help me out in solving this issue, thanks in advance!!

Related

&& in Foreach loop (PHP)

Just stating learning PHP, I would like to know is there any way I can declare more than 1 $value in a foreach loop? I am trying to echo out my 8 different type of arrays which i declared ($line0 - $line8). Apologies if my codes are abit messy. I'm still new to PHP.
PHP Code
<?php
$handle = #fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
if ($handle) {
while (!feof($handle)) {
$store = fgets($handle, 4096);
if ($row == 9){
$row = 0;
$count++;
}
if ($row == 0)
{
$line0[] = strval($store);
}
else if($row == 1) {
$line1[] = strval($store);}
else if($row == 2) {
$line2[] = strval($store);}
else if($row == 3) {
$line3[] = strval($store);}
else if($row == 4) {
$line4[] = strval($store);}
$row++;
}
?>
<table>
<tr>
<?php
foreach($line2 as $value1)&&(line3 as $value2){
echo "<td><b>Product ID: $value1</b>"
echo "<td><b>Selection ID: $value2</b>
</td>";
echo '</tr>';
}
?>
</table>
listings.txt
Cedric
93482812
cedric#hotmail.com
Guitar
---------------------------------------------
Wendy
98238432
wendy#hotmail.com
Guitar
---------------------------------------------
No you can't do that, but there's something else you can use. All your arrays have the name keys, and you can get the key with foreach like this:
foreach ($line1 as $key => $value1) {
$value2 = $line2[$key];
echo "<tr>";
echo "<td><b>Product ID: $value1</b></td>";
echo "<td><b>Selection ID: $value2</b></td>";
echo '</tr>';
}
That is getting pretty close to what you want.

Echo array values of column not working

I want to display the values of my array. but instead of displaying:
1509 1510 1511 it display ArrayArrayArray. What does it mean?
include("db_PSIS.php");
$sql_dc = "SELECT Child, Datecode
FROM traveller_merging15
WHERE Parent='" . $_REQUEST["txt_traveller_no"] . "'
ORDER BY Merge_ID ASC";
$res_dc = mysql_query($sql_dc);
$dcode1 = $row_dc['Datecode'];
$storeArray = array();
if (!$res_dc) {
echo "No data fetched!";
} else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray} <br>";
}
//$str_dc=implode(",",$storeArray);
//echo $str_dc;
}
You are assign the value of $row_dc['Datecode']; before fetch data from database. You need to do fetch data inside while loop and echo it
$res_dc = mysql_query($sql_dc);
if (!$res_dc) {
echo "No data fetched!";
} else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
echo $row_dc['Datecode'];
}
}
Note:- mysql is deprecated instead use mysqli or PDO
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray} <br>";
}
instead try
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[] = $dcode1;
echo "{$storeArray[0]} <br>";
}
Try this,
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$child = $row_dc['Child'];
$Datecode = $row_dc['Datecode'];
echo "$child <br> $Datecode";
}
if you are getting more that 1 row, use for() loop
else {
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$count = count($row_dc);
for($i = 0; $i < $count; $i ++){
$child = $row_dc[$i]['Child'];
$Datecode = $row_dc[$i]['Datecode'];
echo "$child <br> $Datecode";
}
}
-first array don't print value using echo
-use print_r($array) to print key value pair(print a array)
your solution is
$i = 0;
while ($row_dc = mysql_fetch_array($res_dc, MYSQL_ASSOC)) {
$storeArray[$i] = $dcode1;
echo $storeArray[$i].'<br>';
$i++;
}
NOTE::-- Use single Quote instead of double Quote and mysql is deprecated instead use mysqli or PDO

Memcache results and while loop

I wrote this function for caching queries:
function get_cached($query, $type = 1) {
$key = md5(strtolower($query));
$cached = $memcache->get($key);
if($cached) {
$res = unserialize($cached);
} else {
$r = mysql_query($query);
if($mode == 1) {
$res = mysql_fetch_object($r);
} else {
$res = mysql_fetch_array($r);
}
$memcache->store($key, serialize($res));
}
return $res;
}
And it kinda works, i can cache simple queries. But i have problem with queries that operate within loops.
I really can't understand how to get around this, and "port" this to use my get_cached() function first.
$ref = mysql_query("query");
while($res = mysql_fetch_object($ref)) {
//do something with results
}
How i can do it through cache?
You cannot store the mysql result object but you can fetch all rows into an array and cache the array. like
function get_cached($query, $type = 1) {
$key = md5(strtolower($query) . $type);
$cached = $memcache->get($key);
if($cached) {
return unserialize($cached);
} else {
$r = mysql_query($query);
$data = array();
if($mode == 1) {
foreach($row = mysql_fetch_object($r)) {
$data[] = $row;
}
} else {
foreach($row = mysql_fetch_array($r)) {
$data[] = $row;
}
}
$memcache->store($key, serialize($data));
}
return $data;
}
don't forget to store the type of your functions as cache key too, otherwise you fetch a results set with type==1 and in return you get the elements as array

HTML select not inflating correctly from PHP array

I am trying to create a drop down menu of usernames or a <select> as it's known in HTML. However I am only getting the last value back from my array and I can't figure out why.
PHP
function getUserName($db) {
try {
$sql = 'SELECT members.name FROM members';
$query_an = $db->query($sql);
$count = $query_an->rowCount();
if ($count > 0) {
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names = array();
$names[] = $row['name'];
}
return $names;
}
} catch(PDOException $e) {
die($e->getMessage());
}
}
HTML
<select>
<?php $names = getUserName($db); foreach($names as $key => $value) { ?>
<option value="<?php echo $key ?>"><?php echo $value ?></option>
<?php }?>
</select>
I'm fairly sure the HTML section of my code is solid. I think the error lies in how I'm adding values to my $names array but after staring at it for a half an hour I can't see it. Thanks for any help/fresh eyes.
You need to declare your array outside the loop
if ($count > 0) {
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;
}
this is emptying your array every time : $names = array();
use this :
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
You are creating a new $names array every row then returning the last one. You need to declare the array outside the while loop. The following should work:
function getUserName($db) {
try {
$sql = 'SELECT members.name FROM members';
$query_an = $db->query($sql);
$count = $query_an->rowCount();
if ($count > 0) {
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;
}
} catch(PDOException $e) {
die($e->getMessage());
}
}
The problem is you're re-initializing the array on every loop:
See:
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names = array();
$names[] = $row['name'];
}
return $names;
Should be:
$names = array();
while ($row = $query_an->fetch(PDO::FETCH_ASSOC)) {
$names[] = $row['name'];
}
return $names;

How to add a <br/> after each result, but not last result?

This is my partial code:
while($row = $db->fetch_array($query))
{
echo $row['row_name'];
}
How can I make it so it will add a break tag after each result, but not the last result?
Put the output into an array, then join the array with implode:
$rows = array();
while($row = $db->fetch_array($query))
{
$rows[] = $row['row_name'];
}
echo implode('<br/>', $rows);
You could do this. No arrays or counters.
if($row = $db->fetch_array($query))
{
do {
echo $row['row_name']
} while($row = $db->fetch_array($query) && print("<br />"));
}
for ($idx = 0; $row = $db->fetch_array($query); $idx++)
{
if ($idx > 0) { echo "<br/>"; }
echo $row['row_name'];
}

Categories