I am using codeigniter and at the moment I have a bit of the problem. When I insert iframe (from the youtube) into db in the view file I get row code like this:

<iframe width="420" height="315" src="http://www.youtube.com/embed/aWvcNkRJhHw" frameborder="0" allowfullscreen></iframe>

How to insert iframe into db and display it properly?

EDITED PART:

Form for inserting:

  <h3>Create News</h3>
<?php echo form_open('news/create_news') ?>
<label for="title">Title</label><input type="text" name="title" class="inputBox" id="title"><br>
<label for="body">Text</label><textarea name="body" class="inputBox" id="body" rows="5"></textarea>
<label for="datepicker">Date</label><input type="text" name="date" class="inputBox" id="datepicker">
<input type="submit" name="submit" value="Create" class="submit" />
<input type="reset" class="button standard" value="Clear" />
<?php echo form_close() ?>

Function for inserting:

    function create_news()
    {
         $data = array(
   'title' => $_POST['title'],
   'body' => $_POST['body'],
    'date' => $_POST['date']         
);
$this->db->insert('news', $data); 
    }

I tried to insert iframe from youtube (embed video) but it does not work very well. When I load view file I just get text instead of video ().

Controller function:

function news() {
    $this->load->helper('text');
    if ($this->session->userdata('is_logged_in') == true) {
        $q = $this->news_model->get_all();
        if ($q == false) {
            $data['np'] = 'There is no news at the moment.';
        } else {
            $data['news'] = $q;
        }
        $this->pagg();                      
        $data['main_content'] = 'admin_news';
        $data['title'] = 'Admin News';
        $this->load->view('admin/template', $data);                     
    } else {
        $this->index();
    }
}

View file:

  <div id="news">
<?php if (isset($np)) { echo '<p>' . $np . '</p>'; } ?>
<?php if (isset($news)) : ?>    
    <?php foreach ($news as $row) : ?>
    <div class="borderBott col_12">
            <h3><a href="<?php echo base_url() ?>admin/news_update/<?php echo $row['title'] . "/" . $row['id_news'] ?>" ><?php echo $row['title'] ?></a></h3>
            <p class="<?php echo alternator('par', 'nepar') ?>"><?php echo word_limiter($row['body'], 50); ?></p>
            <p>Date written: <?php echo $row['date'] ?></p>
            <a href="#" class="delete" id="<?php echo $row['id_news'] ?>">Delete News</a>            
        </div>
    <?php endforeach; ?>  
<?php endif; ?>
有帮助吗?

解决方案

As NullUserException suggested, it's not a good idea to store the whole iframe tag in your database.

First and foremost: it's ugly. Your storing layout dependent data in your database. NEVER do this. What if you decide you want a different default width and height? You would need to edit all existing records in your database.

Secondly, if YouTube changes their embedded code, all your data would be rendered useless.

IMO the correct solution would be to extract the video id from the iframe and store this in your database.

However, if what I'm assuming is correct and you have a news post with an embedded video in it, you need to take a look at the BBCode mechanism. Your HTML is stored as plain text and hence not rendered correctly when simply outputting the value from your database. By making use of a mechanism such as BBCode, you define custom tags (generally enclosed by straight brackets '[]'), to be stored in your database.

Upon showing the data again all your tags are converted back to the correct HTML and you'll get your embedded video!

There are a few helpers for CodeIgniter: BBCode Helper or Another BBCode Helper. (Note: these are the actual names of the helpers, I'm not trying to be funny :) ).

In my CI applications I always made use of Smarty templating engine, which also provided BBCode helpers, so I never used any of the above.

HTH.

其他提示

<iframe width="560" height="315" src="https://www.youtube.com/embed/IwB4idmx7oE" frameborder="0" allowfullscreen></iframe>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top