views:

42

answers:

4
<?php
$tab=1;/*$_GET['tab'];*/
$id=1111;/*$_GET['id'];*/
include_once('solve.php');
  $query="SELECT
  user_job.level1,
  user_job.tab_level,
  job.money_gain,
  job.exp_gain,
  job.energy_required,
  job.name,job.job_id
FROM user_job
RIGHT JOIN job USING(job_id) WHERE job.tab=".$tab." AND user_job.user_id=".$id." LIMIT 0,10";
     $result =solve1($query);
        while($data=mysql_fetch_array($result))
        {
            $i=0;
            $rows[]=array('name'=>array(
                                        'name_id'=>$data['name'],
                                        'tab_level'=>$data['tab_level']
                                        ),
                          'job_reward'=>array(
                                        'job_money'=>$data['money_gain'],
                                        'job_exp'=>$data['exp_gain'],
                                        'job_energy'=>$data['energy_required']
                                        ),
                          'job_required_items'=>array()

                        );
                    echo $data['job_id'];
                    $q="SELECT job_item.no,job_item.item_name FROM job_item WHERE job_id=".$data['job_id'];
                    $data_result=solve1($q);
                    $re=array();
                    $data_data="";
                    while($data_data=mysql_fetch_array($data_result))       
                    {
                        $r=array( 'filename'=>$data_data['item_name'],
                                  'no'=>$data_data['no']
                                  );

                        array_push($re,$r);
                    }
                    //print_r(json_encode($re));
                    $row['job_required_items']=array_merge($row['job_required_items'],$re);
                    //echo $data['job_id']."==>".var_dump($re);
                    //$rows['job_required_items']=$r;
                    //$rows['']
                    //$rows[$i]=$rows[$i]+$re;
                    //array_merge($rows[$i],$re['job_required_items']);
                    //array_push($rows[$i],re);
                    //print_r($re);
                    //$row[]=array('job_required_items'=>array());                  
                    //$rows[$i]=$rows[$i]+$re;
                    $r=array();
                    $re=array();
                    //unset($r);
                    //unset($re['job_required_items']);
                //print_r(json_encode($re));                                
                        //($rows);              
            $i++;
        }
        //print_r($rows);
        print_r(json_encode($rows));





?>

how to add one seperate array into associative array? like i hav ne array $re and i have associative array required_item i want to add $re to 'required_item';

A: 
$row['jon']=$re
Alexander
na this is nt workin
bhaskaragr29
A: 

array_merge maybe ?

$new_array = array_merge($jon,$re);
Puaka
+1  A: 

Assuming the array you've just added is the last one:

$row[count($row) - 1]['jon'] = array_merge($row[count($row) - 1]['jon'], $re);

If that assumption isn't correct, then you have to know the index of the array with 'jon' in it (which I'll call $index here):

$row[$index]['jon'] = array_merge($row[$index]['jon'], $re);

If you don't know the index, and you can't be sure it's the last entry in $row, then you'll have to figure out the index by searching the array somehow.

Ryan Kinal
A: 

Instead of $rows[]=array('name'=>array(... use something like $newrow = array('name'=>array(.... Then add all the stuff you want to $newrow and when you're done append it to $rows: $rows[] = $newrow;.

Btw: It would be possible to query all the information with only one query. Maybe the overhead for transferring all that redundant data isn't desirable. But please at least consider (server-side) prepared statements. This would also take care of the possible sql injections in your current code.

update:
With $rows[] you don't get the index/key you need to modify the newly appended element. That's why you'd have to use end()/current() or something like Ryan Kinal showed. On the other hand if you use a temporary variable you don't need the key, the temp. variable is more or less the key. And since php uses copy-on-write for arrays there's very little overhead in doing so.

Server-side prepared statements would reduce the amount of time it takes the mysql server to parse each query. Since your dependant query ( FROM job_items ) is quite simple this effect would most certainly be completely insignificant. But there's another benefit from using them: You also get named parameter queries. Right now you mix the parameters into the sql statement like e.g. in WHERE job.tab=".$tab.". If for some reason $tab is empty or contains a string you'll get an sql syntax error in the best case or a different, possibly harmful sql statement in the worst case. This doesn't only apply to parameters you get from user input (in this case $_GET['tab']) but any parameter you mix into the statement, like e.g. WHERE job_id=".$data['job_id']. With server-side prepared, parametrized queries the actual arguments are transferred apart from the sql statement and therefore the server has no problem differentiating both the statement and the arguments. Just search stackoverflow for the topics "sql injection" and "prepared statements", there must be hundreds, thousands of posts about this.

VolkerK
thanks i gt it.can u explain the reason behind your code
bhaskaragr29