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>
Related
I have datalist like this :
<input type="text" list="colours" id="txt">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
The option that datalist has was populated from a foreach loop using php.
How do I disable the datalist input, if the data that foreach loop's result is null/none using jquery ?
like for example
Have Option
<input type="text" list="colours">
No Option
<input type="text" list="colours" disabled="disabled">
Here is an example of keeping your PHP and HTML separate:
<?php
$option="";
foreach ($kondisi as $kondisi) {
if($kondisi != 'null' || $kondisi != 'none'){
$option .= '<option data-value="'.$kondisi'.->nama_kondisi" value="'.$kondisi.'->id_kondisi">'.$kondisi.'->nama_kondisi</option>';
}else{
$option .= 'your disabled option code here...';
}
}
?>
<input type="text" list="colours">
<datalist id="colours">
<?php echo $option; ?>
</datalist>
If you want to disable it then do like below:-
<?php if(count($kondisi)>0){?>
<input type="text" list="colours">
<datalist id="colours">
<?php foreach ($kondisi as $kondisi) { ?>
<option data-value="<?php echo $kondisi->nama_kondisi ?>" value="<?php echo $kondisi->id_kondisi ;?>"><?php echo $kondisi->nama_kondisi ?></option>
<?php } ?>
</datalist>
<?php }else{?>
<input type="text" list="colours" disabled="disabled"/><!-- or use autocomplete="off"-->
<?php } ?>
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 have saved multiple courses id's in course field for student in Mysql database..
Ex : course : 1,5,7,9
if i edit the student how can i activate selected courses..?
enter image description here
<?php
$courses_query=mysql_query("select * from `course` where status='1'");
while($c_fetch=mysql_fetch_array($courses_query)){
?>
<input type="checkbox" name="course[]" value="<?php echo $c_fetch['id']; ?>" id="courseid" <?php if($re['course']==$c_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $c_fetch['course'];
?>
<?php
$cids=$c_fetch['id'];
$softw_qry=mysql_query("select * from `softwares` where course='$cids' and status='1'");
while($softw_fetch=mysql_fetch_array($softw_qry)){
?>
<input type="checkbox" name="software[]" value="<?php echo $softw_fetch['id']; ?>" <?php if($re['software']==$softw_fetch['id']){ echo "checked='checked'"; } ?> /> <?php echo $softw_fetch['software']; ?>
<?php
$soft_ids=$softw_fetch['id'];
$topicsn_qry=mysql_query("select * from `topics` where course='$cids' and software='$soft_ids' and status='1'");
while($topicn_fetch=mysql_fetch_array($topicsn_qry)){
?>
<input type="checkbox" name="topics[]" value="<?php echo $topicn_fetch['id']; ?>" /> <?php echo $topicn_fetch['topic']; ?>
<?php } }} ?>
I'm drawing data from a MySQL database that dynamically places a question with 4-5 radio button choices for the answer. These radio buttons all belong to the same group, $quest_name. The first pass of the while statement will create 4 radio buttons belonging to radio group "question_1".
It creates 30-40 of these questions on a page, each with 4 radio buttons. I want the user to fill in all there answers and the page to post back to itself with the users answers still selected and then display if they were correct or not (functionality I still have to add).
I'm trying to follow http://www.w3schools.com/php/php_form_complete.asp as an example, but use a dynamically created radio button name instead.
This is what I have thus far:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$quest_num = $row["id"];
$question = $row["question"];
$option_1 = $row["option_1"];
$option_2 = $row["option_2"];
$option_3 = $row["option_3"];
$option_4 = $row["option_4"];
$option_5 = $row["option_5"];
$answer = $row["answer"];
$quest_name = "question_" . $row["id"];
echo "(" . $quest_num . ") " . $question . "<br>";
?>
<label>
<input type="radio" name=<?php echo $quest_name ?>
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
value=<?php echo $option_1 ?>><?php echo $option_1 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_2 ?>><?php echo $option_2 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_3 ?>><?php echo $option_3 ?>
</label>
<br>
<label>
<input type="radio" name=<?php echo $quest_name ?>
value=<?php echo $option_4 ?>><?php echo $option_4 ?>
</label>
<br>
<br>
<?php
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit">
</form>
The part causing me grief so far is:
<?php if (isset(echo $quest_name) && echo $quest_name == echo $option_1) echo "checked"; ?>
I have also tried:
<?php if (isset($quest_name) && $quest_name == $option_1) echo "checked"; ?>
and:
<?php echo (isset($quest_name) && $quest_name == $option_1) ? "checked" : ""; ?>
How do you post back to the same page what they've selected? Like in this case I'm trying to say if "question_1" is set and "question_1" is equal to "converter" (the first radio button option) then have it checked when submit button is clicked.
I'm not that good at web development, but I'm trying to create a website to help my fellow electrical technician classmates.
Thanks for any help.
EDIT :
Using this line of code fixed the issue:
<?php if(isset($_POST[$quest_name]) && $_POST[$quest_name]==$option_1) { echo 'checked="checked"'; } ?>
What you need is called Radio Group. In HTML layer it is created with same name for all and different values for each like this:
<p>
<label>
<input type="radio" name="RadioGroup1" value="Value1" id="RadioGroup1_0">
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="Value2" id="RadioGroup1_1">
Radio</label>
<br>
</p>
And when you want to get the user input in php layer you go like this:
<?php
//check if Radio Group 1 is set
if(isset($_POST['RadioGroup1'])) {
// print the value of Radio Group 1 choice
echo $_POST['RadioGroup1'];
}
?>
When you want to create a Selected Radio in HTML layer you go like this:
<input name="RadioGroup1" type="radio" id="RadioGroup1_1" value="radio" checked="checked">
So you have to check if user inputs the value of which radio like this:
<label>
<input type="radio" name="RadioGroup1" value="value1" id="RadioGroup1_0" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value1') { echo ' checked="checked"'; } ?>>
Radio</label>
<br>
<label>
<input type="radio" name="RadioGroup1" value="value2" id="RadioGroup1_1" <?php if(isset($_POST['RadioGroup1']) && $_POST['RadioGroup1']=='value2') { echo ' checked="checked"'; } ?> >
Radio</label>
You could use the following:
<input type="radio" name="<?php echo $quest_name ?>" value="<?php echo $option_1 ?>"
<?php if (isset($quest_name) && ($quest_name == $option_1)) echo "checked"; ?> />
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);