Jump to content

some CRUD automatic code creation snippets


gunter
 Share

Recommended Posts

I want to know your opinion, I wrote this small script, it´s not perfect, but it´s very helpful, it outputs php code.

I have included it globally at the footer when the develper is logged in, so I can view the code output at all pages...

the first snippet outputs code to echo all the fields of the actual page, from the actual page or another one...

so if you need somewhere the value of a field you can quickly copy&paste the code...

$text="";
    foreach($page->fields as $field)
    {   
    $text .=    '<b>'.$field->name.' = '. substr($page->get($field->name), 0, 50).'</b>    //    name & value
    &lt?php echo $pages->get("/'.$page->name.'/")->'.$field->name.'; ?>            echo from another page
    &lt?php echo $page->'.$field->name.'; ?>                            echo from the same page
';
    }
echo "output the fields ";
echo "<pre>";
echo $text;
echo "</pre>";
 

 example - in my case it outputs this:

title = title	//	name & value
	<?php echo $pages->get("/hhhhhh/")->title; ?>            echo from another page
	<?php echo $page->title; ?>            				echo from the same page
email = margit.werner@tralala.at	//	name & value
	<?php echo $pages->get("/hhhhhh/")->email; ?>            echo from another page
	<?php echo $page->email; ?>            				echo from the same page
kunde_vorname = MargariteMargarite	//	name & value
	<?php echo $pages->get("/hhhhhh/")->kunde_vorname; ?>            echo from another page
	<?php echo $page->kunde_vorname; ?>            				echo from the same page
kunde_nachname = Name	//	name & value
	<?php echo $pages->get("/hhhhhh/")->kunde_nachname; ?>            echo from another page
	<?php echo $page->kunde_nachname; ?>            				echo from the same page
locations = 1020|1018	//	name & value
	<?php echo $pages->get("/hhhhhh/")->locations; ?>            echo from another page
	<?php echo $page->locations; ?>            				echo from the same page
checkbox = 1	//	name & value
	<?php echo $pages->get("/hhhhhh/")->checkbox; ?>            echo from another page
	<?php echo $page->checkbox; ?> 

the next snippet echoes the code for the form. it´s simple and it takes no care of the different fieldtypes (and it generates css markup for bootstrap...)

Form:
<?php echo "<pre>";
$text ='<form action="<?php echo $page->url; ?>/" method="post" accept-charset="UTF-8">
';
foreach($page->fields as $field) {

	$text.='<div class="form-group">
	';
	$text.='<label for="'. $field->name.'">'.$field->name.'</label>      
	';
	$text.='<input name="'. $field->name.'"  class="form-control" value="<?php echo $page->get("'.$field->name.'");?>" />
</div>
';

}
$text.='
    <button type="submit" value="submit" class="btn btn-default" name="submit">Submit</button>

</form>
</div>';
echo "<pre>";
echo htmlspecialchars($text);
echo "</pre>";

in my example page it generates this:

<form action="http://localhost/kalender/kunden/<?php echo $page->name; ?>/" method="post" accept-charset="UTF-8">

<div class="form-group">
	<label for="title">title</label>      
	<input name="title"  class="form-control" value="<?php echo $page->get("title");?>" />
</div>
<div class="form-group">
	<label for="email">email</label>      
	<input name="email"  class="form-control" value="<?php echo $page->get("email");?>" />
</div>
<div class="form-group">
	<label for="kunde_vorname">kunde_vorname</label>      
	<input name="kunde_vorname"  class="form-control" value="<?php echo $page->get("kunde_vorname");?>" />
</div>
<div class="form-group">
	<label for="kunde_nachname">kunde_nachname</label>      
	<input name="kunde_nachname"  class="form-control" value="<?php echo $page->get("kunde_nachname");?>" />
</div>
<div class="form-group">
	<label for="locations">locations</label>      
	<input name="locations"  class="form-control" value="<?php echo $page->get("locations");?>" />
</div>
<div class="form-group">
	<label for="checkbox">checkbox</label>      
	<input name="checkbox"  class="form-control" value="<?php echo $page->get("checkbox");?>" />
</div>

    <button type="submit" value="submit" class="btn btn-default" name="submit">Submit</button>

</form>
</div>

the third snipped generates code for the form handling...

echo 'Form Processing:';
$text ='&lt?php
if($input->post->submit) 
{
	$page->of(false);
';
	foreach($page->fields as $field)
	{
	
	$text .='	$page->set("'.$field->name.'", $sanitizer->text($input->post->'.$field->name.'));
';
	}
	$text .='	$page->save();
}
?>';

echo "<pre>";
echo $text;
echo "</pre>";

at my example page it generates this:

<?php
if($input->post->submit) 
{
	$page->of(false);
	$page->set("title", $sanitizer->text($input->post->title));
	$page->set("email", $sanitizer->text($input->post->email));
	$page->set("kunde_vorname", $sanitizer->text($input->post->kunde_vorname));
	$page->set("kunde_nachname", $sanitizer->text($input->post->kunde_nachname));
	$page->set("locations", $sanitizer->text($input->post->locations));
	$page->set("checkbox", $sanitizer->text($input->post->checkbox));
	$page->save();
}
?>

this is my first code posting, I hope you understand what I am doing and my code isn´t too messy.





			
		
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...