Foreach error variable cannot identified - php

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

Related

Message: Invalid argument supplied for foreach() and Message: Array to string conversion [duplicate]

Why am I getting this PHP Warning?
Invalid argument supplied for foreach()
Here is my code:
// look for text file for this keyword
if (empty($site["textdirectory"])) {
$site["textdirectory"] = "text";
}
if (file_exists(ROOT_DIR.$site["textdirectory"].'/'.urlencode($q).'.txt')) {
$keywordtext =
file_get_contents(ROOT_DIR.$site["textdirectory"].'/'.urlencode($q).'.txt');
}
else {
$keywordtext = null;
}
$keywordsXML = getEbayKeywords($q);
foreach($keywordsXML->PopularSearchResult as $item) {
$topicsString = $item->AlternativeSearches;
$relatedString = $item->RelatedSearches;
if (!empty($topicsString)) {
$topics = split(";",$topicsString);
}
if (!empty($relatedString)) {
$related = split(";",$relatedString);
}
}
$node = array();
$node['keywords'] = $q;
2
$xml = ebay_rss($node);
$ebayItems = array();
$totalItems = count($xml->channel->item);
$totalPages = $totalItems / $pageSize;
$i = 0;
foreach ($xml->channel->item as $item) {
$ebayRss =
$item->children('http://www.ebay.com/marketplace/search/v1/services');
if ($i>=($pageSize*($page-1)) && $i<($pageSize*$page)) {
$newItem = array();
$newItem['title'] = $item->title;
$newItem['link'] = buyLink($item->link, $q);
$newItem['image'] = ebay_stripImage($item->description);
$newItem['currentbid'] = ebay_convertPrice($item->description);
$newItem['bidcount'] = $ebayRss->BidCount;
$newItem['endtime'] = ebay_convertTime($ebayRss->ListingEndTime);
$newItem['type'] = $ebayRss->ListingType;
if (!empty($ebayRss->BuyItNowPrice)) {
$newItem['bin'] = ebay_convertPrice($item->description);
}
array_push($ebayItems, $newItem);
}
$i++;
}
$pageNumbers = array();
for ($i=1; $i<=$totalPages; $i++) {
array_push($pageNumbers, $i);
}
3
// get user guides
$guidesXML = getEbayGuides($q);
$guides = array();
foreach ($guidesXML->guide as $guideXML) {
$guide = array();
$guide['url'] = makeguideLink($guideXML->url, $q);
$guide['title'] = $guideXML->title;
$guide['desc'] = $guideXML->desc;
array_push($guides,$guide);
}
What causes this warning?
You should check that what you are passing to foreach is an array by using the is_array function
If you are not sure it's going to be an array you can always check using the following PHP example code:
if (is_array($variable)) {
foreach ($variable as $item) {
//do something
}
}
This means that you are doing a foreach on something that is not an array.
Check out all your foreach statements, and look if the thing before the as, to make sure it is actually an array. Use var_dump to dump it.
Then fix the one where it isn't an array.
How to reproduce this error:
<?php
$skipper = "abcd";
foreach ($skipper as $item){ //the warning happens on this line.
print "ok";
}
?>
Make sure $skipper is an array.
Because, on whatever line the error is occurring at (you didn't tell us which that is), you're passing something to foreach that is not an array.
Look at what you're passing into foreach, determine what it is (with var_export), find out why it's not an array... and fix it.
Basic, basic debugging.
Try this.
if(is_array($value) || is_object($value)){
foreach($value as $item){
//somecode
}
}

Echo/Print Query Result at Controller in Codeigniter

I am trying to display my Model query return result in my controller, but I don't know how to do it. could you please show me? Thanks in advance
Controller:
function updateJobsheetCDC()
{
$prnID = $this->input->post('prnID');
$this->load->model('Ipss_model');
$data['prnEmail'] = $this->Ipss_model->prnEmailList($prnID);
echo $data['prnEmail']['name'];
}
Model:
function prnEmailList($prnID)
{
$q = $this->db->query("SELECT pm.* , prm.*,e.* from principal_master pm ,principal prm,email e where pm.prnID=e.prnID and prm.prID= pm.prID and e.prnID='" .$prnID."'");
if($q->num_rows()>0) {
foreach($q ->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
I have tried
echo $data['prnEmail']['name']; but it do not work. It shows Severity: Notice
Message: Undefined index: name.
You have multi-dimentinal array returning from model. So you have to loop over them then print data
function updateJobsheetCDC()
{
$prnID = $this->input->post('prnID');
$this->load->model('Ipss_model');
$data = $this->Ipss_model->prnEmailList($prnID);
foreach ($data as $key => $value) {
echo $value->name;
}
}

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

Function for querying Oracle database - 'Undefined index' notice

To give some context, a little while ago with the help from VolkerK on this very site I created a function that queries an Oracle database and places the content into a neatly formatted PHP array, depending on the index. Here's the original question.
The problem is, in some cases I just want to return a single value from a quick query, and when I do I get the the following error: Notice: Undefined index:
Here's the code I'm using:
function oracleGetData($query, $id = null) {
global $conn;
$results = array();
$sql = OCI_Parse($conn, $query);
OCI_Execute($sql);
while ( false!==($row=oci_fetch_assoc($sql)) ) {
$results[ $row[$id] ] = $row;
}
// remove one layer if there's only one record
if(count($results) == 1 and is_null($id)) {
$results = array_pop($results);
}
return $results;
}
I have tried changing the line that populates the array like so:
if(is_null($id)) {
while ( false!==($row=oci_fetch_assoc($sql)) ) {
$results[ $row ] = $row;
}
} else {
while ( false!==($row=oci_fetch_assoc($sql)) ) {
$results[ $row[$id] ] = $row;
}
}
Basically if the $id variable is null, remove all references to it, but then I get a 'Warning: Illegal offset type' error.
Any help would be much appreciated, I've tried passing in the single field I require into the function but get the same error.
Thank you
Perhaps if $id is NULL you could just use sequential numbers for the indexes, as in:
function oracleGetData($query, $id = null)
{
global $conn;
$results = array();
$sql = OCI_Parse($conn, $query);
OCI_Execute($sql);
if(is_null($id)) {
$idx = 1;
while (false!==($row=oci_fetch_assoc($sql))) {
$results[ $idx ] = $row;
$idx = $idx + 1;
}
} else {
while ( false!==($row=oci_fetch_assoc($sql))) {
$results[ $row[$id] ] = $row;
}
}
// remove one layer if there's only one record
if(count($results) == 1 and is_null($id)) {
$results = array_pop($results);
}
return $results;
}
Share and enjoy.

Invalid argument supplied

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;
}

Categories