Welcome Guest, Not a member yet? Register   Sign In
Creating views, loading views
#1

[eluser]-Menno-[/eluser]
Well, I've got a problem with my views. I'm not very good with CI so please don't start bugging me about how bad I did (tips are welcome tho). So my problem goes like this:


Controller:
Code:
$path = 'lessen/file.php';
$content = '<? echo"test"; ?>';
if(!write_file($path, $content))
{
    echo"Cannot create file.";
}
else
{
    echo"File has been created successfully.";
}

I'm trying to generate views with my function, so the $content will be extended with HTML and so on. Now.. This system works, it writes files to this directory:

Code:
main
->system (the CI system)
->lessen (the directory in which the files will be created.)

So this works, now my next try is to load this view with the $this->load->view() function. Too bad it doesn't work. I've searched on CI and on Google, but couldn't find anything about loading views from another directory as the (sub)directory from the views. So I thought, well, could be that this is not possible.
But then again, how can I create my files in the views directory, or one of its subdirectories?
#2

[eluser]gon[/eluser]
Why do you want to do this?
#3

[eluser]-Menno-[/eluser]
Well, I'm making a website in which you can follow lessons. Now, the admin doesn't know anything about PHP or HTML so I'm making this little function for him to make new lessons. Because I don't like to put it in my database with like 10.000 characters, I was trying to generate these views and only place the links in the database.
But now I'm stuck..
#4

[eluser]-Menno-[/eluser]
Now,
a friend of mine told I could try to do this with the read_file() function and echo this string. I don't know whether this is a good solution or not, what do you think about it? I would prefer a more 'professional' solution.
#5

[eluser]gon[/eluser]
The solution for this is using a WYSIWYG javascript editor, like Tinymce or spaw2. Spaw2 is excellent. They allow the user to write text and format it, but the form sends html that you can store and render in your view. This editors also allow images uploading, so diagrams and such can be inserted.

So you would prepare a form to edit lessons, protecting it with an auth system. It's really a CMS. Lessons are stored in a text database field, in HTML format.

10Kb is a lot of text! If you think lessons will be really long, you should implement the option for creating pages, and store them separately.

good luck!
#6

[eluser]jedd[/eluser]
Hi Menno, and welcome to the CI forums.

A few things ..
Code:
$content = '<? echo"test"; ?>';

This is not guaranteed to work - always use the long PHP tags ( <?php ) and remember to put a space between commands and their parameters (ie. echo and ").

Quote:Because I don’t like to put it in my database with like 10.000 characters,

I'd recommend using a database.

For the rest of your code, I think you need to show us a bit more of the view and controller file content.

Finally, a comment on this bit:

Quote:Well, I’m making a website in which you can follow lessons.

Have you considered installing [url="http://moodle.org/"]Moodle[/url] instead? No point reinventing wheels, particularly when there are such beautiful wheels out there on offer.
#7

[eluser]-Menno-[/eluser]
[quote author="jedd" date="1237514956"]Hi Menno, and welcome to the CI forums.

A few things ..
Code:
$content = '<? echo"test"; ?>';

This is not guaranteed to work - always use the long PHP tags ( <?php ) and remember to put a space between commands and their parameters (ie. echo and ").
[/qoute]

Ok, thanks!

[quote author="jedd" date="1237514956"]
Quote:Because I don’t like to put it in my database with like 10.000 characters,

I'd recommend using a database.
[/quote]

Oh, so it is actually a good idea to put this in the database? Hmm, think this would make everything a lot easier too.

[quote author="jedd" date="1237514956"]
For the rest of your code, I think you need to show us a bit more of the view and controller file content.

Finally, a comment on this bit:

Quote:Well, I’m making a website in which you can follow lessons.

Have you considered installing [url="http://moodle.org/"]Moodle[/url] instead? No point reinventing wheels, particularly when there are such beautiful wheels out there on offer.[/quote]
[/quote]

Yeah, but like already posted, I think TinyMCE would be a better solution for my function.

Thanks anyway! I will put the lessons in my database instead of creating views!
#8

[eluser]cahva[/eluser]
[quote author="jedd" date="1237514956"]Hi Menno, and welcome to the CI forums.

A few things ..
Code:
$content = '<? echo"test"; ?>';

This is not guaranteed to work - always use the long PHP tags ( <?php ) and remember to put a space between commands and their parameters (ie. echo and ").
[/quote]

Short tags can be used. Even if short_open_tag is not on, it can be set in the config file to rewrite PHP Short Tags:
Code:
$config['rewrite_short_tags'] = TRUE;

I personally use it in view files as it makes it a little bit cleaner. But yes, not recommended to use in other situations..
#9

[eluser]jedd[/eluser]
Quote:Short tags can be used. Even if short_open_tag is not on, it can be set in the config file to rewrite PHP Short Tags:

Oh .. I'm not suggesting that they can't be made to work - merely that a) they're not guaranteed to work on any given hosted environment (so it begs the question of whether saving a couple of characters here and there warrants the potential pain later during a platform migration, say) and b) the CI Manual has a [url="http://ellislab.com/codeigniter/user-guide/general/styleguide.html#short_open_tags"]section in the Style Guide[/url] on the subject. While I certainly don't agree with everything in the CI Style Guide, when it agrees with my position I'll happily defer to it.

They also get excluded (for my part) by that style/readability principle of 'if there are two ways you can do something, pick one and use it consistently'.
#10

[eluser]tomcode[/eluser]
this
Code:
$this->load->vars($data);

$this->load->file(APPPATH .'views/prep_view' .EXT);

is the same than
Code:
$this->load->view('prep_view', $data);

so You can
Code:
$data = array ( 'content' => 'what so ever' );

$this->load->vars($data);

$this->load->file('lessen/file.php');

See the User Guide / Loader




Theme © iAndrew 2016 - Forum software by © MyBB