Welcome Guest, Not a member yet? Register   Sign In
Controller variable not being passed to view
#1

[eluser]Unknown[/eluser]
Hi,

I keep receiving the following error in one of my views:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: urlError

Filename: views/gs_error.php

Line Number: 15

Here is the controller code:

Code:
foreach($urlArray as $url)
{
  $url = SnagUrlCleaner($url);//clean supplied url
  $feedResult = $this->Add_Feed($url, $user);//returns true or false
  
  if($feedResult == false)//the url could not be found
  {
   $urlError[] = $url;
  }
}
if(isset($urlError))
{
  
  $data['urlError'] = $urlError;
  $this->load->view('gs_error', $data);
}

And here is the view code to display the var being passed:

Code:
<?php

  foreach($urlError as $thisError)
  {
   echo '<p><em>' . $thisError . "</em></p>";
  }
  
  ?&gt;


Can anyone tell why the variable called $urlError is not being passed to the view? I have not encountered an issue like this before with CI.
#2

[eluser]Nanodeath[/eluser]
I'd recommend throwing in a var_dump($urlError) just inside your if(isset()) block and see what you get.
#3

[eluser]coolfactor[/eluser]
You've got it wrapped in a conditional. It'll only be set if the feed could not be found ($feedError == FALSE). Is there a valid feed during your test?
#4

[eluser]Nanodeath[/eluser]
Well, the view is in the conditional too, so the variable will only be requested by the view that gets called if it's supposedly already set. (Presumably some other view gets called otherwise)
#5

[eluser]coolfactor[/eluser]
Nanodeath has a good suggestion. Debug the data you're going to pass to the view. What does $urlError contain prior to loading the view?
#6

[eluser]Unknown[/eluser]
Here are the details from var_dump():

array(1) { [0]=> string(56) "http://www.dsfdsfdsffsfdsatechrepublic.com/5150-22-0.xml" }

The url in the array is obviously no good, and is set - the view is displayed. There are other valid urls in the OPML file that get loaded into the database as appropriate.

What's interesting is that the same code works in another function perfectly. The code shown above is for inserting feeds from an opml file. The code for inserting feeds manually works as intended when a bad url is provided. As such:

Code:
function Manual()
{
  //check credentials
  $creds = $this->check_creds();
  
   if($creds == TRUE)
   {
   $this->load->plugin(array('snagurls'));
   $user = $this->session->userdata('username');
   //Get each feed entered into the form - there will be a max of 5.
  
   $this->load->library('validation');
  
   $rules['feed1'] = "min_length[4]|max_length[255]|xss_clean";
   $rules['feed2'] = "min_length[4]|max_length[255]|xss_clean";
   $rules['feed3'] = "min_length[4]|max_length[255]|xss_clean";
   $rules['feed4'] = "min_length[4]|max_length[255]|xss_clean";
   $rules['feed5'] = "min_length[4]|max_length[255]|xss_clean";
  
   $this->validation->set_rules($rules);
   if($this->validation->run() == TRUE)
   {
//Create an array to hold each feed address.  Add the data from each input field (if it exists) to the array.
if(strlen($_POST['feed1']) > 4)
{
  $urlArray['newfeed1'] = @SnagUrlCleaner($_POST['feed1']);
  //echo $urlArray['newfeed1'];
}
if(strlen($_POST['feed2']) > 4)
{
  $urlArray['newfeed2'] = @SnagUrlCleaner($_POST['feed2']);
}
if(strlen($_POST['feed3']) > 4)
{
  $urlArray['newfeed3'] = @SnagUrlCleaner($_POST['feed3']);
}
if(strlen($_POST['feed4']) > 4)
{
  $urlArray['newfeed4'] = @SnagUrlCleaner($_POST['feed4']);
}
if(strlen($_POST['feed5']) > 4)
{
  $urlArray['newfeed5'] = @SnagUrlCleaner($_POST['feed5']);
}
foreach($urlArray as $url)
{
  $feedResult = @$this->Add_Feed($url, $user);
  
  if($feedResult == false)//the url has been returned due to an error
  {
   $urlError[] = $url;
  }
}
if(isset($urlError))
{
  
  $data['urlError'] = $urlError;
  $this->load->view('gs_error', $data);
}
else
{
  //redirect user to the feed settings page.
}
   }
   else
   {
//enter code for validation error
   }
  }
  else
  {
//this user is not validated...send him to the front page
   redirect('entry', 'location');
  }
}

Now here is the full code for the opml insertion:

Code:
function Opml()
{
  //check credentials
  $creds = $this->check_creds();
  $this->load->plugin(array('snagurls'));
  $user = $this->session->userdata('username');
  
   if($creds == TRUE)
   {
   //Upload file.
   $config['upload_path'] = './uploads/opml_files/';
   $config['allowed_types'] = 'xml';
   $config['max_size'] = '10000';
  
   $this->load->library('upload', $config);
  
   if( ! $this->upload->do_upload())
   {
$error = array('error' => $this->upload->display_errors());
$this->load->view('getstarted', $error);
   }
   else
   {
  //Read file contents.
  $file_info = array('upload_data' => $this->upload->data());
  
$opmlFile = './uploads/opml_files/' . $file_info['upload_data']['file_name'];
  
$fd = fopen($opmlFile, 'r');

$data = stream_get_contents($fd);

$thisXml = simplexml_load_string($data);
  
foreach($thisXml->body->outline as $item)
{
   foreach ($item->children() as $ol)//get the children of each main "outline" tag
   {
   $urlArray[] = "" . $ol['xmlUrl'] . "";
  }
}
if(file_exists($opmlFile)){unlink($opmlFile);}
foreach($urlArray as $url)
{
  $url = @SnagUrlCleaner($url);//clean supplied url
  $feedResult = $this->Add_Feed($url, $user);//returns true or false
  
  if($feedResult == false)//the url could not be found
  {
   $urlError[] = $url;
  }
}
if(isset($urlError))
{
  
  //var_dump($urlError);
  $data['urlError'] = $urlError;
  
  
  
  $this->load->view('gs_error', $data);
}
else
{
  //redirect user to the feed settings page.
}
  
   }

  
  
  
  
  }
  
}

Additionally I am getting another error in the OPML function when the view is shown:

Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: controllers/getstarted.php

Line Number: 375

Not sure why I don't have the same problems with the first function.




Theme © iAndrew 2016 - Forum software by © MyBB