5

Similar to Facebook, I am building an app that allows users to post a link.

The user fills out the link in an input field, and the controller returns

Title
Link
Meta description
Images (as thumbnails)

to the view.

Here is the controller code:

    $url = $this->input->post('posts_link');

    if (!empty($url)) {

        $html = file_get_html($url);

        foreach ($html->find('img') as $element) {

          $src = "";

          $src = $element->src;

            if (preg_match("/\.jp[e]?g$/i", $src)) {

            $images[] = $src;

            }
        }

        $data['posts_link'] = $url;
        $data['images']     = $images;
        $data['title']          = $html->find('title', 0)->plaintext;
        $data['meta']           = get_meta_tags($url);

The problem I'm having is when there are no images, no title, or no description (alone or in combination).

I am using codeigniter and it throws several errors on the view, which I would rather have suppressed.

Is there a best practice to suppress these errors or place empty variables in case no title/images/descriptions are returned by the DOM parser?

For example I've tried

$data['images'] = $images ? $images : '';

but it doesn't resolve my problem.

Any suggestions?

Thanks.

pepe
  • 9,799
  • 25
  • 110
  • 188
  • And what is the *actual* problem? – zerkms Jun 21 '11 at 03:04
  • The problem I'm having is when there are no images, no title, or no description (alone or in combination). I am using codeigniter and it throws several errors on the view, which I would rather have suppressed. – pepe Jun 21 '11 at 03:06
  • @torr: **what errors**? We need to guess? – zerkms Jun 21 '11 at 03:06
  • :0P - sorry - Undefined index: description, Undefined variable: images, are a few examples – pepe Jun 21 '11 at 03:08
  • @torr: so predefine them `$images = array(); $description = '';` – zerkms Jun 21 '11 at 03:09
  • @zerkms, I've tried `$data = array()` and it didn't work - should I predefine each array key as in `$data['images'] = array()`? – pepe Jun 21 '11 at 03:12
  • @torr: no, you need to predefine **used variables**. Added separate answer below. – zerkms Jun 21 '11 at 03:14

3 Answers3

1

You need to predefine the variables before you use them. For $images it would be

$images = array();

right after if (!empty($url)) { etc

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • this works of $images, but how to resolve the undefined index error coming from `description`? since `$meta` is an array in itself, I retrieve description via `$meta['description']` in the view, but how to initialize this one in the controller? – pepe Jun 21 '11 at 03:24
  • @torr: `if (isset($meta['description'])) { // work with description }` – zerkms Jun 21 '11 at 03:36
  • what would you suggest to suppress an error like this -- `Message: file_get_contents(http://cia.gov) [function.file-get-contents]: failed to open stream: HTTP request failed!` -- certain websites that don't have `` seem to break `file_get_contents`, regardless if I run the function directly or via Simple Dom Parser – pepe Jun 22 '11 at 01:25
  • @torr: `failed to open stream` has nothing to do with non existent title – zerkms Jun 22 '11 at 01:42
  • OK thanks - found this on SO that may help other with same issue -- [link](http://stackoverflow.com/questions/3431169/good-error-handling-with-file-get-contents) – pepe Jun 22 '11 at 01:45
0
if (array_key_exists('images', $data)) {
    // display the image
    }
else
{

    //set a default image, etc
}

or something like isset

Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
-1

you can suppress errors by using the @ symbol. i.e.

 @     $data['images']     = $images;
dgamma3
  • 2,333
  • 4
  • 26
  • 47