PHP implode function in if and foreach condition [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my HTML Code
<input type="checkbox" value="fotoğraf" name="materyal[]"> Fotoğraf
<input type="checkbox" value="resim" name="materyal[]">Resim
<input type="checkbox" value="çizelge" name="materyal[]">Çizelge
<input type="checkbox" value="katlanabilir harita" name="materyal[]"> Katlanabilir Harita
There are checkbox and i'll take values with $_POST['materyal'] and then i want to write screen values with comma if values more than one. if checkbox values empty i don't write anything screen.
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
$materyal = $_POST['materyal'];
echo "İçerdiği extra materyaller ; ";
foreach ($materyal as $materyallist) {
foreach ($materyallist as $yenimateryal){
array_push($sonmateryal, $yenimateryal);
}
}
echo implode(", ", $sonmateryal);
}
This is my code. when i want to use implode in if contiditon, I take mistake.
How can i do

It seems like you just want to show the contents?
$sonmateryal = array();
if (isset($_POST['materyal']) && !empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ";
foreach ($_POST['materyal'] as $materyallist) {
foreach ($materyallist as $yenimateryal) {
$sonmateryal[] = $yenimateryal;
}
}
}
echo implode(", ", $sonmateryal);
update according to given HTML
if (!empty($_POST['materyal'])) {
echo "İçerdiği extra materyaller ; ".htmlentities(implode(',', $_POST['materyal']));
}
Since you're posting the inputs as simple array's this will implode the results with a comma if the post-ed value isn't empty..

Related

Print PHP array results into HTML selectable list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have the following code printing out an array. I'd like to print this out into a selectable list.
foreach ($local as $record)
{
print($record->friendlyName);
}
Output example:
[checkbox] Option 1
[checkbox] Option 2
[checkbox] Option 3
[checkbox] Option 4
[checkbox] Option 5
Ideally I could take action on the selected option. Thank you!
I would do it like this. Depends, what value you want to get. Whether it's id of $record or it's friendlyName.
Its friendlyName:
<?php foreach ($local as $record) { ?>
<input type="checkbox" name="checkboxes[]" value="<?=$record->friendlyName?>">
<a><?=$record->friendlyName?></a>
<br>
<?php } ?>
Its ID:
<?php foreach ($local as $recordId => $record) { ?>
<input type="checkbox" name="checkboxes[]" value="<?=$recordId?>">
<a><?=$record->friendlyName?></a>
<br>
<?php } ?>

Getting data from hundreds of fields [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is more of a hypothetical, since I just want to use the most efficient way to go about this.
Say you have a page with over 100 textboxes, and a submit button that bring you to a next page and displays what you've written.
How would I go about only printing the textboxes that have been filled in?
I would know how to do this with a lot of if statements, or a very long switch statement, but perhaps there's a simpler way?
Thank you!
yes, there is.
simply call them numbering them.
for example you'll have textbox1, textbox2, textbox3...
in this way, on the next page you just have to build a for loop.
for ($index = 1; $index < 100; $index++) {
if (!empty($_REQUEST["textbox" . $index]))
{
echo "textbox number {$index} isn't empty!";
}
}
in this way you will get each box is full or not.
Something like
<?php
for($i=0; $i<100; $i++){
if(!empty($_POST['textbox'.$i]))
echo $_POST['textbox'.$i].'<br>';
}
?>
should work I guess.
I did not test it
if the name itself is not important i would give them the name txtbox[] (name="txtbox[]")
This way you can choose one name for all the boxes that belong together, have a specific array to loop through $_POST['txtbox'] . Then just echo them all
foreach($_POST['txtbox'] as $key => $tb) {
echo "<br>Box " . $key . ": " . $tb;
}
Give them names like txtbox_1 to txtbox_100
foreach ($_POST as $key => $value) {
if (is_integer(strpos($key, 'txtbox_')) && $value != '') {
echo "<br />$key = $value ";
}
}

Dropdown variable script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following code:
echo "<td><textarea wrap='soft' class=tarea3 name='inston[]'>".$r['inston']."</textarea></td>\n";
I'd like to use that inston variable to associate with a dropdown list. I have working code for the dropdown list:
<tr><td class="tdt">
<?php te('Hosting Server');?>:</td> <td title='Select server'>
<select validate='required:true' class='mandatory' name='manufacturerid'>
<option value=''>Select</option>
<?php
foreach ($agents5 as $a) {
$dbid=$a['id'];
$atype=$a['label']; $s="";
if (isset($manufacturerid) && $manufacturerid==$a['id']) $s=" SELECTED ";
echo "<option $s value='$dbid' title='$dbid'>$atype</option>\n";
}
echo "</select>\n";
?>
</td></tr>
and a sql query for the above mentioned dropdown script,
$sql="SELECT * FROM items where itemtypeid=32 OR itemtypeid=44 order by label";
$sth=db_execute($dbh,$sql);
while ($r=$sth->fetch(PDO::FETCH_ASSOC)) $agents5[$r['id']]=$r;
How can we use the code above to associate inston variable?
Your question isn't clear and from what i understood, is suppose the answer to be this
First of all , use an id for the select tag and textarea and use an onChange function like this:
<textarea wrap='soft' class=tarea3 name='inston[]' id='inston'>".$r['inston']."</textarea>
<select validate='required:true' class='mandatory' name='manufacturerid' id='manufacturerid' onChange='setInston()'>
and the use this javascript function :
function setInston() {
var x = document.getElementById("manufacturerid").value;
document.getElementById("inston").innerHTML= x+"
";
}

Trying to echo a variable ( $i) into another variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}

What is the correct syntax for the code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
<input type="radio" name="rdbtnques1">
<input type="radio" name="rdbtnques2">
//php code
for($i=0;$i<count($_POST['txt']);$i++)
{
echo $chk = $_POST['rdbtnques.$i'];
}
I want to know the correct syntax for the above code
change this
echo $chk = $_POST['chkques.$i'];
to
echo $chk = $_POST['chkques'.$i];
just put the $_POST['txt'] in a variable
Like: $text = $_POST['txt'];
in that way it is much more easier and readable.
Your question could do with being made a bit clearer, but I think this is what you want:
for( $i=0 ; $i<count( $_POST['txt'] ) ; $i++ )
{
echo $chk = $_POST['rdbtnques'.$i];
}

Categories