Echo array values of column not working - php

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

Related

How to save multiple results in the array with for loop php

I'm working with a CSV file. when the user uploads the file. I parse the CSV then select the data from the array that I need. after that, I'm running a for loop to validate that data and saving the results in the array. but the problem is when I print the results array there's only result for 1 email and there are 4 emails. any suggestions?
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$results['Email'] = $email;
if ($result) {
$results['Result'] = 'valid';
$valid_emails++;
} else {
$results['Result'] = 'invalid';
$invalid_emails++;
}
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
Use $results[] to add one or more elements :
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$result = validate_email($email);
$res['Email'] = $email;
if ($result) {
$res['Result'] = 'valid';
$valid_emails++;
} else {
$res['Result'] = 'invalid';
$invalid_emails++;
}
$results[] = $res;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
You are overriding results every time in your loop, try this
$results = [];
$valid_emails = 0;
$invalid_emails = 0;
for ($i = 0; $i < $csv_array['row_count']; $i++) {
$email = $csv_array['data'][$i][$email_column];
$rowResult=[];
$result = validate_email($email);
$rowResult['Email'] = $email;
if ($result) {
$rowResult['Result'] = 'valid';
$valid_emails++;
} else {
$rowResult['Result'] = 'invalid';
$invalid_emails++;
}
$results[]=$rowResult;
}
echo '<pre>';
print_r($results);
echo '</pre><br>';
echo $valid_emails . '<br>';
echo $invalid_emails . '<br>';
$results contains only the last email validation.
You should store multiple results, and not only the last ;-)
Something like :
$results[$i]['Email'] = $email;
if ($result) {
$results[$i]['Result'] = 'valid';
$valid_emails++;
} else {
$results[$i]['Result'] = 'invalid';
$invalid_emails++;
}
if ($result) {
$results[$i] = 'valid';
$valid_emails++;
} else {
$results[$i] = 'invalid';
$invalid_emails++;
}

encode php recordset in custom format

I'm currently have a recordset created with dreamweaver and have encode the results in json format which work fine.
Recordset
$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
$pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);
if (isset($_GET['totalRows_rs_feeds'])) {
$totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
$all_rs_feeds = mysql_query($query_rs_feeds);
$totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);
}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;
do {
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
mysql_free_result($rs_feeds);
Recordset Results
{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}
But i want the results to be encode like this rather
[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
Try
do {
$json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";
OR
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
You can change your loop to look like this:
echo "[";
$count = 0;
do {
if($count !== 0)
echo ",";
$count++;
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";
$count is there just to skip first "," so you don't get [,{...},{...}] or you can put it like false/true... I just can't remember right now what is better in case you have 10k steps loop...
Try:
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);

Fetch Two Arrays in 1 while loop (not working)

I'm trying to fetch arrays from two queries. The result only gets the first index.
while(($row2 = mysql_fetch_array($query1)) && ($index = mysql_fetch_array($query2)))
{
$leaveID = $row2['leaveID'];
$personID = $index['personID'];
$Person_Type = $index['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
Assuming both queries are OK I offer two ways to loop through them depending on your needs. Hopefully one of them will help.
<?php
$arrayRowA = array();
$arrayRowB = array();
while($row = mysql_fetch_array($query1)){$arrayRowA[] = $row;}
while($row = mysql_fetch_array($query2)){$arrayRowB[] = $row;}
// Loop through two arrays in a square way (every combination of both arrays)
foreach($arrayRowA as $keyA => $objectA){
foreach($arrayRowB as $keyB => $objectB){
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
// Loop through two arrays in a linear way (one to one)
foreach($arrayRowA as $keyA => $objectA){
if(isset($arrayRowB[$keyA])){
$objectB = $arrayRowB[$keyA];
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
?>
$query = mysql_query("select p*, l* from person as p left join leave as l on p.personID = l.leaveID");
while(($row = mysql_fetch_array($query)))
{
$Person_Type = $row['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo "<td>$Sick_Remaining_Days</td>";
echo "</tr>";
}
You should get array before while:
array f,s
while(first)
{
f = ...;
}
while(second)
{
s = ...;
}
And now use both array.
Or try use new http://php.net/manual/en/mysqli-result.fetch-array.php. Probably you losing connection to first query after use second query.
while($row = mysqli_fetch_assoc($result))
{
$row2 = mysqli_fetch_assoc($result2);
echo $row['id'];
echo $row2['id'];
/* your code */
} `
Use this... It is what do you search !
while($row2 = mysql_fetch_array($query1))
{
$index = mysql_fetch_array($query2);
echo $row['id'];
echo $index['id'];
/* add code */
}

static marker from a mysql array

I have a little problem I can't figure out how to solve.
I have an SQL result with 3 rows and I want to put the id from each row into a static marker ie.
MARKER_1 = 4
MARKER_2 = 5
MARKER_3 = 6
How can I do that so I get my static markers but with dynamic values?
I can't do it with a normal
while($row = mysql_fetch_array($result)) {
}
$i = 1;
while($row = mysql_fetch_array($result)) {
if($i == 1) {
$marker_1 = $row;
} elseif($i == 2) {
$marker_2 = $row;
} elseif($i == 3) {
$marker_3 = $row;
}
$i++;
}
i would recommend using an array like this
$results = array();
while($row = mysql_fetch_array($result)) {
$results[] = $row;
}
and then access it via:
$results[0] // or $results[1] and so on. you can even loop that :)
hope that helps
Hope this helps
while($row = mysql_fetch_array($result)) {
echo "MARKER_".$row['id'];
echo"=". $row['value'];
}

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