Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to construct array
Old 08-21-2008, 11:57 AM how to construct array
Junior Talker

Posts: 4
Name: hasan
Trades: 0
I retrieve data from database, I think the result is array, how to construct the data-array in php?
My code is :

PHP Code:
$sql=mysql_query("select * from airline") or die(mysql_error());
   
      if (
$sql) {
          while(
$orsCat mysql_fetch_array($sql)) {
   
  
$aid[]=$orsCat['airline'];
  
$airline[]=$orsCat['airline'];
  echo 
" | airline ".$orsCat["airline"]."<br>";
   
                                  
$sql2=mysql_query("SELECT year(a.tanggal) as year, month(a.tanggal) as month, sum(b.jml) as jml_transaksi, a.airline as airline FROM `keuangan` as a, a_keuangan as b where year(a.tanggal)='2008' and a.id=b.id_transaksi and a.airline=b.id_airline and b.id_airline=".$orsCat["id"]." group by year(a.tanggal), month(a.tanggal), a.airline order by a.tanggal asc") or die(mysql_error());
   
                                                  while(
$ors mysql_fetch_array($sql2)){
                                                  echo 
" bulan ".$ors["month"]." | jumlah ".$ors["jml_transaksi"]."<br>"
                                                   }
  
mysql_free_result($sql2);
   
  }
   
  } 
In browser like this :

| airline Batavia airline
bulan 6 | jumlah 2
bulan 7 | jumlah 2
bulan 8 | jumlah 2
| airline Lion Air
bulan 6 | jumlah 1
bulan 7 | jumlah 1
bulan 8 | jumlah 1
| airline Sriwijaya Air
bulan 6 | jumlah 1
bulan 7 | jumlah 1
bulan 8 | jumlah 2
| airline Garuda
bulan 6 | jumlah 2
bulan 7 | jumlah 2
bulan 8 | jumlah 2

Airline[airline][bulan][jumlah]. Am I correct? How to coding it in php?
recosmic is offline
Reply With Quote
View Public Profile Visit recosmic's homepage!
 
 
Register now for full access!
Old 08-21-2008, 12:50 PM Re: how to construct array
Junior Talker

Posts: 3
Name: Joseph
Trades: 0
You currently have two arrays:
$orsCat which holds the airline and $ors which holds the other two values.
The problem with this is that each iteration of the loop recreates the arrays. If you want the data in only one array, you will need to create a new array in which to store the information.

Example:
PHP Code:
while($ors mysql_fetch_array($sql2)){
       echo 
" bulan ".$ors["month"]." | jumlah ".$ors["jml_transaksi"]."<br>"
       
$orsAll[$orsCat["airline"]][$orsCat["airline"]] = $ors["jml_transaksi"];

jthayne is offline
Reply With Quote
View Public Profile
 
Old 08-21-2008, 08:46 PM Re: how to construct array
Junior Talker

Posts: 4
Name: hasan
Trades: 0
PHP Code:
$orsAll[$orsCat["airline"]][$orsCat["airline"]] = $ors["jml_transaksi"
if i build array like this:

PHP Code:
$orsAll[$orsCat["airline"]][$ors["month"]] = $ors["jml_transaksi"
because i want collect data relative to airline + month and will show data in $ors["jml_transaksi"] ? its correct one?
recosmic is offline
Reply With Quote
View Public Profile Visit recosmic's homepage!
 
Reply     « Reply to how to construct array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.12171 seconds with 12 queries