controller
$data["controller"] = $table;
$data["rows"] = $this->base_model->get();//get table attributes and names
$write_add = $this->load->view("generate/add",$data,TRUE);//load file into var
$file_vadd = fopen($path["dirname"].'/generate/views/'.$data["controller"].'/add.php',"w+"); //write to file
generate/add.php (template)
<?php echo "<?php " ?>echo form_open('<?=$controller?>/add'); ?>
<fieldset>
<legend>New <?=$controller?></legend>
<?php foreach($fields as $field): ?>
<label for="<?php echo $field->name?>" ><?php echo $field->name?></label>
<?php if(!empty($rel)) : ?>
<?php if($rel["rel_type"]==1 && $field->name == $rel["rel_fk"] ):?>
<select name="<?php echo $field->name?>" id="<?php echo $field->name?>">
<?php echo "<?php " ?> foreach($<?=$rel["rel_table"]?>_option as $<?=$rel["rel_table"]?>): ?>
<option value="<?php echo "<?php " ?>echo $<?=$rel["rel_table"]?>-><?=$rel["rel_pk"]?>; ?>" ><?php echo "<?php " ?>echo $<?=$rel["rel_table"]?>-><?=$rel["rel_view"]?> ;?></option>
<?php echo "<?php " ?> endforeach; ?>
</select>
<?php else: ?>
<input type="text" name="<?php echo $field->name?>" id="<?php echo $field->name?>" />
<?php endif; ?>
<?php endif; ?>
<?php endforeach;?>
<input name="submit" type="submit" value="Add"/>
</fieldset>
</form>
i have a controller which will call this template to create views, controllers and models. i dont know a better way to do this and i dont like putting the php tag inside a php tag. suggestion?
This is like "bake" function in cakephp. But im creating a codeigniter version of bake. the script is running. But my code is messy.? looking for better way to manage the template file..
A simple way of doing this is enabling output buffering, including the php file (which lets it do its thing), end output buffering and saving the result in a file. Something along the lines of
ob_start();
include("foobar.php");
$temp .= ob_get_clean();
file_put_contents('foo.tpl', $temp);
Related
Iam a beginner in php and codeigniteran I have a problem concerning a survey vote form Iam creating in codeigniter. I have no idea how many answers the creater of a survey writes so I must code a dynamic page where the user sees as many radio buttons as answeres.
controller:
public function vote($frage_id){
if(!$this->ion_auth->logged_in()){
redirect('auth/login');
}
$antworten = $this->antwort_model->getbyFrageid($frage_id);
$frage = $this->fragen_model->get($frage_id);
foreach($antworten as $antwort){
$this->form_validation->set_rules('antworten',$antwort['inhalt']);
}
if($this->form_validation->run() === FALSE){
$data = array(
'frage' => $frage,
'antworten' => $antworten
);
$this->load->view('templates/header');
$this->load->view('antwort/vote', $data);
$this->load->view('templates/footer');
print_r($antworten);
}
else{
$antwort_id;
foreach($antworten as $antwort){
if($antwort['inhalt'] = $this->input->post('antworten')){
$antwort_id = $antwort['id'];
}
}
$user_id = $this->ion_auth->get_user_id();
$this->antwort_model->set($antwort_id,$user_id);
redirect('umfrage/display/'.$frage['umfrage_id']);
}
}
}
view;
<?php echo validation_errors(); ?>
<h1><?php echo $frage['inhalt'];?></h1>
<?php echo form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="antworten" ><?php echo $antwort['inhalt'];?></label>
<input type="radio" name="antworten" value = "<?php $antwort['inhalt'];?>"<?=set_radio('antworten',$antwort['inhalt'])?> /><br />
<?php } ?>
<input type="submit" name="submit" value="Submit" />
</form>
for some reason the form_validation() always returns false.
pls help
thx in advance
IF you look at your HTML Source on the browser to see what HTML is neing generated you should see you have to find a few more 'echo' commands.
<?php echo validation_errors(); ?>
<h1><?php echo $frage['inhalt'];?></h1>
<?php echo form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="<?php $antwort['inhalt'];?>" ><?php echo $antwort['inhalt'];?></label>
<input type="radio" name="<?php $antwort['inhalt'];?>" value = "<?php $antwort['inhalt'];?>" /><br />
<?php } ?>
<input type="submit" name="submit" value="Antwort abgeben" />
</form>
Needs a few more echo statements but its is better to use <?= than <?php echo ( as some folks forget the echo...)
<?= validation_errors(); ?>
<h1><?= $frage['inhalt'];?></h1>
<?= form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort){ ?>
<label for="<?= $antwort['inhalt'];?>" ><?= $antwort['inhalt'];?></label>
<input type="radio" name="<?= $antwort['inhalt'];?>" value = "<?= $antwort['inhalt'];?>" /><br />
<?php } ?>
<input type="submit" name="submit" value="Antwort abgeben" />
</form>
And we could add a little less confusion with curly braces in HTML by using https://www.php.net/manual/en/control-structures.alternative-syntax.php
<?= validation_errors(); ?>
<h1><?= $frage['inhalt'];?></h1>
<?= form_open('antwort/vote/'.$frage['id']); ?>
<?php foreach($antworten as $antwort): ?>
<label for="<?= $antwort['inhalt'];?>"><?= $antwort['inhalt'];?></label>
<input type="radio" name="<?= $antwort['inhalt'];?>" value = "<?= $antwort['inhalt'];?>"><br>
<?php endforeach; ?>
<input type="submit" name="submit" value="Antwort abgeben">
</form>
I would like to be able to dynamically change how many option values are available, so that there are always "$i" values, like in the following.
<form action="scheduled.php" method="post" id="fields">
<p>Teams Playing</p>
<SELECT NAME="Teams[]" MULTIPLE SIZE=<?php echo htmlspecialchars($i); ?>>
<OPTION value="<?php echo htmlspecialchars($team[0]); ?>"><?php echo htmlspecialchars($team[0]); ?>
<OPTION value="<?php echo htmlspecialchars($team[1]); ?>"><?php echo htmlspecialchars($team[1]); ?>
<OPTION value="<?php echo htmlspecialchars($team[2]); ?>"><?php echo htmlspecialchars($team[2]); ?>
<OPTION value="<?php echo htmlspecialchars($team[3]); ?>"><?php echo htmlspecialchars($team[3]); ?>
...
<OPTION value="<?php echo htmlspecialchars($team[$i]); ?>"><?php echo htmlspecialchars($team[$i]); ?>
</SELECT>
<input type="submit">
</form>
I already suspect my code is sloppy for using all of the "?php echo"s. Is there any way I can make a for loop so that there are always "$i" options displayed in this format?
(Posted on behalf of the OP).
Here was my solution:
<form action="scheduled.php" method="post" id="fields">
<p>Teams Playing</p>
<SELECT NAME="Teams[]" MULTIPLE SIZE=<?php echo htmlspecialchars($i); ?>>
<?php
for ($x=0; $x<$i; $x++){
echo "<OPTION value=".htmlspecialchars($team[$x]).";>";
echo $team[$x];
}
?>
</SELECT>
<input type="submit">
</form>
I'm developing a wordpress woocommerce website where I put a form where two drop down exists. where selecting a value from the first dropdown after page refresh it automatically goes the second dropdown respective to the value of the first dropdown value. then after submitting it goes to certain page. my code is given below
<ul><script>
$(function(){
$("#parent_cat").change(function(){
var val=$("#parent_cat").val();
$("#first").click();
//alert(val);
});
});
</script>
<li><label>Category</label>
<?php global $val;?>
<form method="post">
<?php echo $val;?>
<select name="category_name" id="parent_cat">
<?php $val2='* Please Select *';?>
<?php if(isset($_POST['hidden'])){?>
<option value="<?php echo $val; ?>" id=""><?php echo $val; ?></option>
<?php } else {?>
<option value="<?php echo $val; ?>"><?php echo $val2; ?></option>
<?php } ?>
<?php echo $post->id; ?><?php
global $wpdb;
$wp_posts = $wpdb->prefix . "posts";
$rsults=$wpdb->get_results
("select post_title,guid,ID from ".$wp_posts." where post_status='publish' and post_type='page' and post_parent='0' and ID IN(8,32) ");
//echo "select post_title from ".$wp_posts." where post_status='publish' and post_type='page' and post_parent='0' ";
global $post;
foreach($rsults as $post){
setup_postdata($post);
$id = get_the_ID();
?>
<option value="<?php echo $id; ?>">
<?php the_title(); }?></option>
</select>
<input type="submit"name="sub" id="first" style="display:none;"/>
</form>
<script>
$(function(){
var hid=$("#quot").val();
$("#parent_cat").val(hid);
$("#parent_cat").change(function(){
var val=$("#parent_cat").val();
$("#first").click();
//alert(val);
//$("#parent_cat > option[value=" + 32 + "]").prop("selected",true);
});
});
</script>
</li>
<li><label>Type</label>
<form method="post">
<select name="search_category" class="parent">
<option value="" selected="selected">Select</option>
<?php if(isset($_POST['category_name']))
$val=$_POST['category_name'];
echo $val;
?>
<?php
$rsults2=$wpdb->get_results("select ID,guid,post_title from ".$wp_posts." where post_status='publish' and post_type='page' and post_parent=".$val." ");
if($rsults2){
?>
<?php
foreach($rsults2 as $post_sub){
?><option value="<?php echo $post_sub->guid; ?>">
<a href="<?php echo $post_sub->guid; ?>" title="<?php echo $post_sub->post_title; ?>">
<?php echo $post_sub->post_title; ?></a></option>
<?php } ?>
<?php } ?>
</select>
<input type="text" value="<?php echo $val; ?>" id="quot" style="display:none;" name="hidden"/>
<button id="red" name="reff">submnit</button>
</form>
<?php if(isset($_POST['search_category']))
$vall=$_POST['search_category'];
echo $vall;
header('Location: '.$vall);
?>
<script>
$(function(){
$(".parent").change(function(){
var ref=$(".parent").val();
});
});
</script>
</ul>
It's working fine in all other pages instead of home page. when I select first value of the first dropdown it goes to 404 page. It's built in wordpress and woocommerce and it's home page is not the shop page. Any help would be appreciated. Thank you in advance.
I have created a html form that is populated by a mysql query. The data has spaces in it.
eg "Bob site", "Sarah home"
When the form is created in the browser I get this:
Bob
site
Sarah
home
Instead of
Bob site
Sarah home.
I know I need to format my data but can't find any examples. My form code is below.
<?php include("includes/header.php"); ?>
<?php
$site = mysql_query("select distinct `site_name` from sept_billing");
?>
<html>
<body>
<h2>Create a new install</h2>
<form method="post" action="actions/newmachine_action.php">
<table border = '0'>
<tr>
<td><b>Site name:</b></td>
<td><input id="site" type="text" name="site" size="40" list="sites" value = 'Choose site name' />
<datalist id="sites">
<?php
while ($row = mysql_fetch_array($site))
{
echo "<option value=".$row[0].">";
}
?>
</datalist></td>
</tr>
</table>
<input type="submit" value="Create" />
</form>
</body>
</html>
<?php include("includes/footer.php"); ?>
Please change this and let me know if this works
echo "<option value=".$row[0].">";
to
echo "<option value='".$row[0]."'>";
This will work
<?php
while ($row = mysql_fetch_array($site))
{ ?>
<option><?php echo $row['name']; ?></option>
<?php } ?>
try
"<option text='".$row[0]."' value='".preg_replace(' ', '-', $row[0])."'></option>"
or
"<option value='".preg_replace(' ', '-', $row[0])."'>".$row[0]."</option>"
i think that value cannot have spaces
i´m kind of a noob in php things and i got the following problem:
i got a plugin which helps me making a website with 2 languages. So i tell the plugin which languages i want to have in my contao-backend and then i can use them in the frontend. I can then chose wether i want to be able to switch the language by clicking on a flag or by clicking the language in a select box. But there is no option to just have the languages next to each other without a flag. Just a text like "German Spanish". its always a select box or flags.
I then opened the template for the select box and it tells me this:
<?php
/**
Menu for switching between languages of a page.
*/
?><form name="<?php echo $this->type;?>" method="post" style="display:inline"
><select name="language" onchange="this.form.submit();">
<?php foreach ($this->items as $item): ?>
<option value="<?php echo $item['language'];?>" <?php
if ($item['isActive']) {
echo ' class="active" selected="selected"';
} ?>><?php
echo $this->languages[$item['language']];
?></option>
<?php endforeach; ?></select><input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}" /></form>
(This is the one where i can switch by a select box)
and:
<form name="<?php echo $this->type;?>" method="post" style="display:inline"
><?php foreach ($this->items as $item): ?><input
class="language" type="radio" name="language"
id="language_<?php echo $item['language'];?>"
onchange="this.form.submit();"
value="<?php echo $item['language'];?>" <?php
if ($item['isActive']) {echo ' class="active" checked="checked"';} ?> />
<label for="language_<?php echo $item['language'];?>" <?php
if ($item['isActive']) {echo ' class="active"';} ?>><img src="<?php
echo 'system/modules/i18nl10n/html/flag_icons/png/'.$item['language'].'.png';?>"
title="<?php echo $this->languages[$item['language']];?>"
alt="<?php echo $this->languages[$item['language']];?>"
/></label><?php endforeach; ?><input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}"></form>
(this is the one which lets me change the language by clocking on the flag)
....
my question now is if someone knows how i can edit this code in a way that it simply shows me the languages i can chose in a text like "german spanish".
Sorry for my english :P
greetings
You can just remove the <img> tag and replace it with the language name:
(I have also improved the code formatting for better readability)
<form name="<?php echo $this->type;?>" method="post" style="display:inline">
<?php foreach ($this->items as $item): ?>
<input class="language" type="radio" name="language"
id="language_<?php echo $item['language'];?>"
onchange="this.form.submit();"
value="<?php echo $item['language'];?>"
<?php
if ($item['isActive']) {
echo ' class="active" checked="checked"';
}
?>
/>
<label for="language_<?php echo $item['language'];?>"
<?php if ($item['isActive']) { echo ' class="active"'; } ?>
>
<?php echo $this->languages[$item['language']];?>
</label>
<?php endforeach; ?>
<input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}">
</form>