Invalid argument supplied - php

In following code i has error in view, how can fix it?
My data in table is as:
CI_Controller:
$update = array('15'); // this is a example from my $_POST that are array.
if (is_array($update) && count($update) > 0) {
foreach($update as $val){
$data['query_hi'] = $this->db->get_where('hotel_image', array('relation' => $val))->row();
}
$this -> load -> view('admin/residence_update', $data);
}
View:
foreach($query_hi->images as $val){
echo $val;
}
Error:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: core/Loader.php(679) : eval()'d code
Line Number: 279

The problem is that it returns only one result for your query... and the array is overridden at each cycle. Try this:
$update = array('15'); // this is a example from my $_POST that are array.
if (is_array($update) && count($update) > 0) {
$data= array();
foreach($update as $val){
$tmp= $this->db->get_where('hotel_image', array('relation' => $val));
foreach($tmp->result() as $row){
$data['query_hi'][] = $row;
}
}
$this -> load -> view('admin/residence_update', $data);
}

Maybe you should try:
for($i=0;$i<count($query_hi);$i++)
{
echo $query_hi[$i]->images;
}

Related

Assigning values to associative array (multidimensional)

while($row = $champions_table_result->fetch_assoc()){
while($row3['Champion']==$row['Champion']){
$test[$row['Champion']]['General']['Change'][] =$row3['Stat_Change'];
$test[$row['Champion']]['General']['Type'][] = $row3['Stat_Change_Icon'];
}
foreach ($row as $column_name => $column) {
if ($column_name == 'Champion') {
continue; // These fields were already displayed above
}
if (!empty($column)) {
$test[$row['Champion']][$column_name] =$column;
while($row2['Champion']==$row['Champion']&&$row2['Spell_Type']
==$column_name&&!empty($row2['Spell_Change'])) {
$test[$row['Champion']][$column_name]['Change'][] = $spell_descriptions[ $champion_counter][$spell_counter][$change_counter];
$test[$row['Champion']][$column_name]['Type'][] = $spell_changes[
$champion_counter][$spell_counter][$change_counter];
$row2 = $spells_table_result->fetch_assoc();
}
}
}
}
To better showcase my problem here is an image with var_dump from $test - array http://i.imgur.com/xcox2df.png and next to Q W E it should show variable $column; I'm trying to achieve that in this line
$test[$row['Champion']][$column_name] =$column;
Without this line I get what I showed on imgur but with this line I get an error:
Warning: Illegal string offset 'Change' and Fatal error: Cannot use string offset as an array

Invalid argument supplied for foreach() in file php

I have the following error in the code below.
Warning: Invalid argument supplied for foreach() in
/admin/controller/module/megamenu.php on line 263 The list was updated
15-05-27 23:25:14!
Line 263 : foreach ($jsonArray as $subArray) {
I checked on another server and does not appear this error (php5.4) but my server with php5.3, php5.5 appear. What's missing?
if (isset($_GET['jsonstring'])) {
if($this->validate()){
$jsonstring = $_GET['jsonstring'];
$jsonDecoded = json_decode(html_entity_decode($jsonstring));
function parseJsonArray($jsonArray, $parentID = 0) {
$return = array();
foreach ($jsonArray as $subArray) {
$returnSubSubArray = array();
if (isset($subArray->children)) {
$returnSubSubArray = parseJsonArray($subArray->children, $subArray->id);
}
$return[] = array('id' => $subArray->id, 'parentID' => $parentID);
$return = array_merge($return, $returnSubSubArray);
}
return $return;
}
$readbleArray = parseJsonArray($jsonDecoded);
foreach ($readbleArray as $key => $value) {
if (is_array($value)) {
$this->model_menu_megamenu->save_rang($value['parentID'], $value['id'], $key, $data['active_module_id']);
}
}
die("The list was updated ".date("y-m-d H:i:s")."!");
} else {
die($this->language->get('error_permission'));
}
}
Change
$jsonDecoded = json_decode(html_entity_decode($jsonstring));
to
$jsonDecoded = json_decode(html_entity_decode($jsonstring),true);
and $jsonDecoded will be an array
i think i some cases we have to use implode and explode functions when we work with json data in php, try this after decoding your json data to convert an string to an array.

get COUNT(*) from mysql_magic

The following code retrieve the result from queries. Usually, I ask for SELECT... and I get a two-dimensional array but this time I need to get count from a table.
if(isset($_GET['query']))
{
$results = mysql_magic($_GET['query']);
$response = array();
//ERROR : Invalid argument supplied for foreach()
foreach($results as &$row)
{
$rowArray = array();
foreach($row as &$column)
{
$rowArray[] = $column;
}
$response[] = $rowArray;
}
$jsonData = json_encode($results);
}
This is a part of the function mysql_magic. In most cases, it returns mysql_fetch_all($req_result). In this case, it returns a row.
else if (startsWith($req_sql, 'select count(*)'))
{
$line = mysql_fetch_row($req_result);
return $line[0];
}
Why do I get an error "Invalid argument supplied for foreach()" since my result, a count, contains one row and one column?
I think that you are using mysql_magic($_GET['query']) should be mysql_query($_GET['query']). I see no reference in the code for a $_GET case. So $results is undefined due to php error?
if(isset($_GET['query']))
{
$results = mysql_query($_GET['query']);
$response = array();
//ERROR : Invalid argument supplied for foreach()
foreach($results as &$row)
{
$rowArray = array();
foreach($row as &$column)
{
$rowArray[] = $column;
}
$response[] = $rowArray;
}
$jsonData = json_encode($results);
}

Foreach error variable cannot identified

i have this function
function c_del()
{
$session_data = $this->session->userdata('logged_in');
$uname = $session_data['username'];
$query = $this->user_m->viewDetail($uname);
foreach($query as $row)
{
$username=$row->username;
}
$id_calon_reg=$_GET['a'];
$query1 = $this->candidate_m->del_calon($id_calon_reg);
$query3 = $this->candidate_m->search_calon($id_calon_reg);
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
}
unlink($foto_calon);
$query2 = $this->candidate_m->viewAll();
$data=array(
"query"=>$query2,
"username"=>$username
);
$this->load->helper(array('form'));
$this->load->view('candidate_view',$data);
}
i want to unlink the path stored in $foto_calon, but i get this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: foto_calon
Filename: controllers/candidate.php
Line Number: 67
line 67 is where i call unlink function.
but I already define the variable $foto_calon in foreach.
the first foreach when i want to store the username into $username its success but got error in the second foreach.
i can't find out what's the problem. can anybody tell me?
Try to replace
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
}
with
if(is_array($query3)) {
foreach($query3 as $row)
{
$foto_calon=$row['foto_calon'];
}
}
And if you want to unlink each row of foto_calon in the result array then put that unlink in this foreach loop like
if(is_array($query3)) {
foreach($query3 as $row)
{
$foto_calon=$row['foto_calon'];
unlink($foto_calon);
}
}
Try to put the unlink into the foreach, like so :
$id_calon_reg=$_GET['a'];
$query1 = $this->candidate_m->del_calon($id_calon_reg);
$query3 = $this->candidate_m->search_calon($id_calon_reg);
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
unlink($foto_calon);
}

foreach() error when using wordpress wp_get_sidebars_widgets()

function getWidgets($position = null) {
if (empty($this->widgets)) {
foreach (wp_get_sidebars_widgets() as $pos => $ids) {
$this->widgets[$pos] = array();
foreach ($ids as $id) { // error is here
$this->widgets[$pos][$id] = $this->getWidget($id);
}
}
}
}
These are lines 305-314.
I'm getting this error:
" Warning: Invalid argument supplied for foreach() in /home/content/73/9889573/html/wp-content/themes/yoo_spark_wp/warp/systems/wordpress.3.0/helpers/system.php on line 310 "
Can someone tell me how do i fix it
wp_get_sidebars_widgets() returns a 1-dimensional array.
Reference: http://codex.wordpress.org/Function_Reference/wp_get_sidebars_widgets
$ids is not an array. You cannot traverse it in a foreach loop.
Try this:
$widgets = array();
foreach (wp_get_sidebars_widgets() as $pos => $id) {
$widgets[$pos] = $this->getWidget($id);
}

Categories