Welcome to WebmasterWorld Guest from 54.147.217.76

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Arrays

   
11:01 am on Oct 19, 2012 (gmt 0)



Hi all

I've got some working code that returns all rows to a JSON request from a MySQL database. I would like to know if it's possible to add some more data in the array.

Here's the code I'm using now

if(isset($_POST['report'])){
$date = clean($_POST['date']);
$sql = "SELECT * FROM log WHERE date = '$date' ORDER BY id ASC";
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
$balanceArray=get_balance();
$balance = $balanceArray['balance'];
$colour = $balanceArray['colour'];

$myarray = array();
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$ref = $row['ref'];
$in = $row['in'];
$out = $row['out'];
$detail = $row['detail'];
$myarray[] = array('id' => $id, 'ref' => $ref, 'inn' => $in, 'out' => $out, 'detail' => $detail);
}
echo json_encode($myarray);
} else {
$arr = array("result" => "error");
echo json_encode($arr);
}}


The array works fine as it is but I need to add $balance and $colour into it so I can use them on the page. As the array is built by each row returned I have no idea how to do this.

Any help would be great, thanks.
6:04 am on Oct 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean something like this?
$myarray[] = array('id' => $id, 'ref' => $ref, 'inn' => $in, 'out' => $out, 'detail' => $detail, 'balance'=>$balance, 'colour'=>$colour);
11:04 am on Oct 24, 2012 (gmt 0)



I've tried that but it's returned undefined on the page. I think it's because I'm putting $balance into the array and I'm using a for each method on the main page. I'll show the code for displaying the results, hopefully this will help.

$.each(response, function(key, val) {
$("#report").append('<tr><td>' + val.ref + '</td><td>' + val.inn + '</td><td>' + val.out + '</td><td>' + val.detail + '</td></tr>');
});


I've tried accessing the balance by using

$("#report").append('<p>Total:' + response.balance + '</p>');


This returns - Total:undefined
2:28 pm on Oct 24, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



....<td>' + val.balance + '</td><td>' + val.colour + '</td></tr>');


Is that what you have tried?
8:56 am on Nov 8, 2012 (gmt 0)



I have tried that but as I'm using it outside of the "each (function)" it is coming back with val is undefined, which is correct. If I show the code hopefully this will help.

$(".main-content").html("<table id=\"till\"><thead><tr><th>Reference</th><th>In</th><th>Out</th><th>Details</th></tr></thead><tbody>");
$.each(response, function(key, val) {
$("#till tbody").append('<tr><td id="' + key + '">' + val.ref + '</td><td>' + val.inn + '</td><td>' + val.out + '</td><td>' + val.detail + '</td></tr>');
});
$("#till tbody").append('<tr><td colspan=\"4\">Total Spent:' + val.balance + '</td></tr>');
$("#till tbody").append('</tbody></table>');


I have used alert (JSON.stringify(response)); and can see that the balance is in the alert.
9:28 am on Nov 8, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



You appear to be applying the same "balance" and "colour" to every record that is returned, these should not really be part of this array, but anyway... have you tried:

alert(response[0].balance);
8:50 am on Nov 10, 2012 (gmt 0)



That's worked however I do realise your correct that the balance shouldn't be part of that array. Now I know how to access the data could you please help me ammended the PHP side of it in regard the array.
10:10 am on Nov 10, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



What you could do, is store just the 'balance' and 'colour' in the first element of the array and omit it from all the others. Then skip the first record (that contains just 'balance' and 'colour') in your JavaScript loop...

$myarray = array(); 
$myarray[] = array('balance' => $balance, 'colour' => $colour);
while (...) {


$.each(response, function(key, val) { 
if (key == 0) return;
$("#till tbody").append('<tr><td id="' + key + '">' + val.ref + '</td><td>' + val.inn
+ '</td><td>' + val.out + '</td><td>' + val.detail + '</td></tr>');
});


Then you still use
response[0].balance


This is assuming you don't need your keys to start at 0. The valid keys will now start at 1.
10:44 am on Nov 11, 2012 (gmt 0)



This is great, thank you very much.

I have implement the changes and it's works perfectly.

I've learnt something new once again :)
11:31 am on Dec 7, 2012 (gmt 0)



I don't see anything wrong in the function.

Can you post the code you used to call the function and display the result?

Is it possible that you accidently used "print($result_array)" instead of "print_r($result_array)" when checking the return from the function?
12:18 pm on Dec 7, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



@ericlewis107 wrong thread?!