1

I insert data with json_encode() in database, now i want get(select * from <table> ...) only name_unitsin of database? i want output this-> salam & mokhles & fadat

In database row units:

[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]},
 {"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},
 {"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]

.

$query_hotel_search = $this->db->query("SELECT * FROM hotel_submits WHERE name LIKE '%$hotel_search%' ORDER BY name asc");
$data = array();
foreach ($query_hotel_search->result() as $row)
{
   $units = json_decode($row->units);
   $data[] = array('name' => $row->name, 'units' =>$units['name_units']); // Line 24
}
echo json_encode($data);

This is output above code:

A PHP Error was encountered
Severity: Notice
Message: Undefined index: name_units
Line Number: 24

[{"name":"Jack","units":null}]

4
  • 1
    it seems that $units is numeric-indexed array with 3 values for name_units: salam, mokles and fadat. But what is the correct output when the $row->name is the same for all of the units? Maybe you have to return also array of unit names? Without expected output, we can't help Commented Sep 2, 2011 at 15:13
  • output $row->name => "name":"Jack" and output $row->units no "$row->units['name_units']" => [{"units":[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]},{"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]}] Commented Sep 2, 2011 at 15:18
  • what is your output when you print_r( $units ) before line 24? you might not be getting what you expect out of the database, or from json_decode Commented Sep 2, 2011 at 15:18
  • @Ryan print_r( $units ) => Array ( [0] => stdClass Object ( [name_units] => salam [price_units] => 74,554 [checkbox_units] => Array ( [0] => minibar [1] => mobleman ) ) [1] => stdClass Object ( [name_units] => mokhles [price_units] => 4,851,269 [checkbox_units] => Array ( [0] => mobleman [1] => tv ) ) [2] => stdClass Object ( [name_units] => fadat [price_units] => 85,642 [checkbox_units] => Array ( [0] => minibar [1] => mobleman [2] => tv ) ) ) 1 Commented Sep 2, 2011 at 15:20

1 Answer 1

1

You're getting an array of stdClass objects as a result from json_decode, rather than an associative array as you'd expect. It looks like you've got an array of JSON strings located in the same cell of your DB.

Assuming that's how your DB table is structure, if you want to output

salam & mokhles & fadat

then try this:

foreach( $query_hotel_search->result() as $row ) {
    $units = json_decode( $row->units );
    $names = '';
    foreach( $units as $unit ) {
        $names .= "{$unit->name_units} & ";
    }
}
echo substr( $names, 0, -2 );
Sign up to request clarification or add additional context in comments.

4 Comments

In output these these are just a name_units => salam . i want all value name_units of database, This means(Please note to the [In database row units] in first my post:): salam & mokhles & fadat . how is it?
is the JSON string you posted in your original question in one cell? or is there one name_units entry per cell? your solution really depends on how the information is stored in the database. the code i just posted will reproduce your expected output if they're all in the same cell; if they're all in different cells (which would be better), then i can modify my code to do that, too.
in your edit this line:echo $unit->name_unit; have this error: Message: Trying to get property of non-object. ?
based on your last comment, i think i'm missing some information about your database. what does the data look like while it's still in your table? that will determine how it needs to be extracted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.