Call html code in mysql

Below is my controller and code view. It stores a new blog post using the tinymce form and editor. The problem is in storing tinymce content in html format. How do you call all stored content in readable format.

thank

CONTROLLER CODE:

<?php

class Welcome extends CI_Controller {
               
	function __construct()
	{
 		parent::__construct();
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('html');
		$this->load->model('blogpost');
	}	
	function index()
	{			
		$this->form_validation->set_rules('post', 'post', 'required|trim|xss_clean|max_length[255]');			
		$this->form_validation->set_rules('content', 'content', 'max_length[255]');
			
		$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
	
		if ($this->form_validation->run() == FALSE) // validation hasn't been passed
		{
			$this->load->view('BlogForm_view');
		}
		else // passed validation proceed to post success logic
		{
		 	// build array for the model
			
			$form_data = array(
					       	'post' => set_value('post'),
					       	'content' => set_value('content')
						);
					
			// run insert model to write data to db
		
			if ($this->blogpost->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
			{
				redirect('welcome/success'); 
				  // or whatever logic needs to occur
			}
			else
			{
			echo 'An error occurred saving your information. Please try again later';
			// Or whatever error handling is necessary
			}
		}
	}
	function success()
	{
			echo 'this form has been successfully submitted with all validation being passed. All messages or logic here. Please note
			sessions have not been used and would need to be added in to suit your app';
	}

	function recall()
	{
		$query = $this->db->query('SELECT id, post, content FROM post');

		foreach ($query->result_array() as $row)
		{
		    echo $row['id'];
		    echo $row['post'];
		    echo $row['content'];
		    $this->load->view('BlogForm_view');
		}
	}
}
?>
      

Run codeHide result


VIEW CODE:

<html>
<head>
	<!-- TinyMCE -->
<script type="text/javascript" src="<?php echo base_url('tiny_mce/tiny_mce.js'); ?>"></script>
<script type="text/javascript">
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft,visualblocks",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
	});
</script>
<!-- /TinyMCE -->
</head>
<body>

<?php // Change the css classes to suit your needs    

$attributes = array('class' => '', 'id' => '');
echo form_open('welcome', $attributes); ?>

<p>
        <label for="post">post <span class="required">*</span></label>
        <?php echo form_error('post'); ?>
        <br />
        <input id="post" type="text" name="post" maxlength="255" value="<?php echo set_value('post'); ?>"  />
</p>
<p>
	<?php echo form_error('content'); ?>
<textarea name="content" class="tinyMCE"><?php echo set_value('content'); ?></textarea>
</p>

<p>
        <?php echo form_submit( 'submit', 'Submit'); ?>
</p>

<?php echo form_close(); ?>
</body>
</html>
      

Run codeHide result


+3


source to share


1 answer


You seem to be using codeigniter incorrectly ...

You shouldn't be making a database query from your controller, this is the point of the model. Set up a function in the controller that communicates with the model. It's also usually cleaner to do all of your data loops in a view instead of a controller (but that's my preference).



//Controller
function recall()
{
    $data['posts'] = $this->post_model->getPosts(); //Move to the model
    $this->load->view('BlogForm_view', $data); //Loop over data in the view.
}

//Model
function getPosts($post_id) {
    $this->db->select('id, post, content')->from('post');
    $r = $this->db->get();
    if ($r->num_rows() > 0) {
        return $r;
    } else {
        return false;
    }
}

      

Typically using tinyMCE is to store related html data. It looks like you are limiting the data to 255 characters. $this->form_validation->set_rules('content', 'content', 'max_length[255]');

If you want to store html, you almost always want to store that as a longtext value in your database. Your question is phrased like you don't need html. And if in this case you can just use the strip_tags () method that php provides to strip all the html (this will probably be in your model where you are dealing with the data $ this-> input-> post ())

0


source







All Articles