homepage Welcome to WebmasterWorld Guest from 54.197.65.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Arrays
LinusIT



 
Msg#: 4509773 posted 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.

 

omoutop

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4509773 posted 6:04 am on Oct 22, 2012 (gmt 0)

Do you mean something like this?
$myarray[] = array('id' => $id, 'ref' => $ref, 'inn' => $in, 'out' => $out, 'detail' => $detail, 'balance'=>$balance, 'colour'=>$colour);

LinusIT



 
Msg#: 4509773 posted 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

penders

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



 
Msg#: 4509773 posted 2:28 pm on Oct 24, 2012 (gmt 0)

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


Is that what you have tried?

LinusIT



 
Msg#: 4509773 posted 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.

penders

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



 
Msg#: 4509773 posted 9:28 am on Nov 8, 2012 (gmt 0)

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



 
Msg#: 4509773 posted 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.

penders

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



 
Msg#: 4509773 posted 10:10 am on Nov 10, 2012 (gmt 0)

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.

LinusIT



 
Msg#: 4509773 posted 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 :)

ericlewis107



 
Msg#: 4509773 posted 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?

penders

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



 
Msg#: 4509773 posted 12:18 pm on Dec 7, 2012 (gmt 0)

@ericlewis107 wrong thread?!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved