<?php
$tab=$_GET['liste'];
$max=sizeof($tab);
echo$max;
echo$_GET['type'];
if ($_GET['type']='multiple')
{
?>
<form name='form'>
<select size=<?php $max ?> multiple>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
else{
?>
<form name='form'>
<select size=<?php $max ?>>
<?php
for($i=0;$i<$max;$i++){
echo"<option value=$i>$tab[$i]</option>";
}
echo"</select>";
echo"</form>";
}
?>
in two cases it's showing just the dropdown list width single choice even if $_GET['type']='multiple' .
I tried
<select size=<?php $max ?> <?php echo$_GET['type']; ?>>
instead of
<select size=<?php $max ?> multiple>
But still not working !!
Any suggestions please !
Change
<select name='ville' size="$max" multiple>
to
<select name='ville' size="<?=$max?>" multiple>
There is a lot that needs fixing here (no offense), so let's start with
if($_GET['type']='multiple')
All this is doing is assigning the value 'multiple' the $_GET['type'] variable, so this will always hit. Next, as mentioned before, you need to change
<?php $max ?>
as it currently does nothing. Next up is
echo"<option value=$i>$tab[$i]</option>";
When using an array element in a string, must be wrapped with { and }
Let's take a look at some cleaned up code because there are too many errors here to pinpoint the real issue:
<?php
$tab = $_GET['liste'];
$max = sizeof($tab);
#echo $max;
#echo $_GET['type'];
$multiple = '';
if($_GET['type'] == 'multiple')
{
$multiple = ' multiple="multiple"';
}
?>
<form name='form'>
<select size="<?php echo $max ?>" <?php echo $multiple; ?>>
<?php
foreach($tab as $key => $value)
echo "<option value='{$key}'>{$value}</option>";
?>
</select>
</form>
Edit: Just verified - variables inside a string must always be wrapped in { and } if it's referencing an array element (this does not apply to objects after all):
$a = array('foo' => 'bar');
$o = new stdclass;
$o->foo = 'bar';
$o->baz = array('foo' => 'bar');
echo "$a['foo']"; // Syntax error
echo "$o->foo"; // echos "bar"
echo "{$a['foo']}"; // echos "bar"
echo "$o->baz['foo']"; // Syntax error
In general, it's a Good Idea to always wrap variables in strings with { and }, even if it's legally allowed:
echo "Something {$here}"; // Obvious variable is obvious
Related
I am currently working with XML files in PHP and would like to be able to load in a specific XML file based on a selection from a dropdown list.
This is code I have been trying so far with help from some other stackoverflow posts about this method:
<html>
<?php
$submittedValue = "";
$value0 = "Route 7602";
$value1 = "Route 7603";
if (isset($_POST["busroutes"])) {
$submittedValue = $_POST["busroutes"];
}
?>
<form action="" name="busroutes" method="post">
<select name="busroutes" onchange="this.form.submit()">
<option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
<option value = "<?php echo $value1; ?>"<?php echo ($value1 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
<?php
if($submittedValue = $value0){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7602&format=xml");
}
elseif($submittedValue = $value1){
$urlbus = ("https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=7603&format=xml");
}
else{
echo "No XML file loaded";
}
$dublinbus_array = simplexml_load_file($urlbus);
echo '<pre>';
print_r($dublinbus_array);
echo '</pre>';
?>
</html>
The first XML file with the stopid of 7602 is displaying correctly but the XML file with stopid of 7603 isn't. I have a feeling im close to something but just can't get over the line.
Any help would be greatly appreciated.
Use "===" for strict comparison not just "=" .
Learn more details here http://www.programmerinterview.com/index.php/php-questions/difference-between-and-in-php/
I successfully echoed out the values that I wanted between the tags but value isn't being recognized which I don't understand, I did a test elsewhere and the value is stored.
This is what I am trying to do where $row[1] displays in the drop down but when selected, no value is stored.
echo '<option value="'.$row[1].'">'."$row[1]".'</option>';
alternatively
$val = $row[1];
// or
$val = "$row[1]";
echo '<option value="'.$val.'">'.
$row[1].
'</option>';
This is my test which works
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$something = $_POST['soption'];
$hey = "hey";
}
?>
<html>
<form method="post">
<select name="soption">
<?php
$you = "somevalue";
$some = '<option value="'.$you.'">'.
"something".
'</option>';
echo $some;
?>
<option value="else">real</option>
</select>
<input type="submit" name="submit" value="test">
</form>
<?php echo isset($something)? $something:""; ?>
<?php echo isset($hey)? $hey:""; ?>
</html>
There doesn't seem to be a problem here, I tested your code in my local server and it displayed both $something and $hey in the echo.
The proper syntax for '."$row[1]".' is '.$row[1].' though if your original enclosing quotes under echo were single quotes.
I have a this html:
<option value="yes">Yes</option>
<option value="no">No</option>
and my php:
<?php
exec('uci get sms_gateway.setting.filter',$filt);
echo '<form action='.$_SERVER['PHP_SELF'].' method="post">
<select name="filter">';
foreach ($filt as $value){
if ($filt = "yes"){
echo '<option value="'.$value.'" selected>Yes</option><br>
<option value="no">No</option><br>ini yes';}
else {
echo '<option value="yes">Yes</option><br>
<option value="'.$value.'" selected>No</option><br> ini no';
}
}
echo '
</select>
<input type="submit" name="submit" value="Submit">';
if(isset($_POST['submit'])){
{
$data = $_POST['filter'];
echo "<br>halo ". $data;
}
}
?>
the $filt only has one string it's either yes or no
when it's yes I want the yes part on the dropdown menu selected, but when it's no I want the no part on the dropdown selected. How should I do that?
This bit of code:
foreach ($filt as $value) you're doing as $value so use:
foreach ($filt as $value){
if ($value == "yes"){
Then you have 2 sets of braces which are not needed; they're just extra keystrokes for nothing:
if(isset($_POST['submit'])){
{
$data = $_POST['filter'];
echo "<br>halo ". $data;
}
}
Change it to:
if(isset($_POST['submit'])){
$data = $_POST['filter'];
echo "<br>halo ". $data;
}
Another thing I spotted, a missing closing </form> tag.
Just for the record, you were also assigning using a single = sign, instead of comparing using two == for:
if ($filt = "yes")
which theoretically should have read as
if ($filt == "yes")
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
There's many things wrong with your code, you're treating "$filt" as both an array and a value, you're also assigning (=) instead of comparing. I'll give you a working example instead of trying to fix your code:
<?php $possibleValues = array('yes', 'no'); // All possible options ?>
<?php $valueYouWantSelected = 'yes'; // 'yes' will be selected ?>
<select>
<?php foreach ($possibleValues as $value): ?>
<?php $selected = ($valueYouWantSelected == $value) ? 'selected' : null; // Select this option if it equals $valueYouWantSelected ?>
<option value="<?php echo $value;?>" <?php echo $selected; ?>>
<?php echo $value; ?>
</option>
<?php endforeach ?>
</select>
If you're confused about how I assign $selected, check out ternary operators.
Also, I'm using alternative syntax, it makes it easier to organize PHP code within HTML (and colors code properly in your editor).
i am selecting an entry from the database to be edited on php/html. i can call the values of a table from the database and put it in a multiple tag.
$job_exp_tags = $row[16];//value from another table
$job_tags = explode(',',$job_exp_tags);//array to be put "selected" on the multiple select tag
$sql_2 = "SELECT _id, job_name from job_tags";
$sel_2 = mysql_query($sql_2);
$array = array();
while($row_new = mysql_fetch_assoc($sel_2)){
$array[] = $row_new;
}
<tr>
<td>Job Experience:<br>
(Hold ctrl to select multiple)</td>
<td><select name = 'job_tags[]' multiple>
<?php
foreach($array as $value){ ?>
<option value ='<?php echo $value['_id']; ?>'> <?php echo $value['job_name']; ?> </option>
<?php
}
?>
</select> </td>
</tr>
my problem is how can i put selected values to it from the explode statement
edit:
i've solved the problem, thanks anyway guys!
<option value ="<?php echo $value['_id']; ?>" <?php echo in_array($value['_id'], $job_tags) ? 'selected="true"' : null; ?>><?php echo $value['job_name']; ?></option>
Just check if its in your array, if so, set it selected:
foreach($array as $value){ ?>
$selected = in_array($value, $job_tags) ? ' selected ' : '';
/* Or [selected="selected"] if you dont use html5 yet (which you should) */
<option value ='<?php echo $value['_id']; ?>' <?php echo $selected; ?>> <?php echo $value['job_name']; ?> </option>
<?php
}
Your code can be simplified though:
foreach($array as $value){
$selected = in_array($value, $job_tags) ? ' selected="selected" ' : '';
?>
<option value="<?=$value['_id']?>" <?=$selected?> > <?=$value['job_name']?> </option>
<?php
}
I changed the quotes arround the value to doubles, not really a rule, but it is a good practice to do so. The other change is the short echo. Small demo, both do the same:
<php $var = 'foorbar'; ?> <!-- A bit weird, but this is demo-purpose -->
<span><?php echo $var; ?></span>
<span><?=$var?></span>
You could look for the value in the string $job_exp_tags and if found set the selected property.
foreach($array as $value){
$selected = strpos($job_exp_tags, $value) ? 'selected' : '';
echo '<option value="'.$value['_id'].'" '.$selected.'>'.$value['job_name'].'</option>';
}
Im a newbie to this forum and have just started coding in php. Need some help. I have the following code
<?php error_reporting(-1);
require_once('/mysql/sql_connect.php');
$q = "SELECT pty.pty_profile_name AS profile FROM pty, users WHERE users.username = 'testaccount' ";
$r = #mysqli_query ($dbc, $q);
$results_array = array();
while($row = mysqli_fetch_assoc($r)) {
array_push($results_array, $row);
echo "<pre>"; print_r($results_array); echo "</pre>"; }
?>
<p>
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
<input type="submit" name="Submit" />
</form>
<?php
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
The output I get is correct, but it displays the key, eg, 0 or 1 or 2, based on what I select in the form. I need the value output. Eg 0 = Emerton, it outputs "0" instead of Emerton.
If I echo $value['profile'] instead of pty_select, I get the last result of the query all the time. Which in this example would be 2, which is Ambarvale as I believe it just chooses the last row output of the query.
I hope I've made sense. Thanks in advance.
It will obviously echo the key, as you assigned the value of options as $key
if you need the options in the $_POST['pty_select'] use this:
<select name="pty_select" >
<?php foreach($results_array as $key => $value){ ?>
<option value="<?php echo $value['profile'];?>"><?php echo $value['profile']; ?></option>
<?php } ?>
</select>
You mean you need the value what you have used to display it.
Then,
Change to :
<option value="<?php echo $value['profile']; ?>">
<?php echo $value['profile']; ?>
</option>
And now let's go to ideal world :)
Build data pairs database_id => name for options:
$q = "SELECT pty.id, pty.pty_profile_name AS profile FROM pty, users
WHERE users.username = 'testaccount'";
$r = mysqli_query($dbc, $q);
$values = array();
while($r = mysqli_fetch_row($r)) {
$values[$r[0]] = $r[1];
}
Never use # when working with database, why do you want to suppress errors instead of preventing/handling them?
Now you have real database IDs and respective values (in general, using unique IDs are better... if nothing else they have greater entropy - more efficient search). And sice displaying select box is really common in webs, lets:
function selectbox( $values = array(), $attributes = array(), $selected_value = null)
{
// Header
echo '<select';
foreach( $attributes as $key => $val){
echo ' ' . htmlspecialchars($key) . '="' . htmlspecialchars( $val) . '"';
}
echo '>';
// Values
foreach( $values as $key => $val){
echo '<option value="' . htmlspecialchars( $key) .'"';
if( $key === $selected_value){
echo ' selected="selected"';
}
echo '>' . htmlspecialchars( $val) . '</option>';
}
echo '</select>';
}
And now usage :)
<form method="post" action="foreach2.php">
<label for="Property Select" class="title">Select Property</label>
<?php selectbox( $values, array( 'name' => 'pty_select')); ?>
<input type="submit" name="Submit" />
</form>
And what to do with it then?
$id = (int)(isset( $_POST['pty_select']) ? $_POST['pty_select'] : 0);
$name = null;
if( $id && isset( $values[$id])){
$name = $values[$id];
}
Give
<option value="<?php echo $value['profile']; ?>"><?php echo $value['profile']; ?></option>
instead of
<option value="<?php echo $key; ?>"><?php echo $value['profile']; ?></option>
if (isset($_POST['Submit'])) {
echo "<pre>"; echo ($_POST['pty_select']); echo "</pre>"; } ?>
Change it to something like
if(isset($_POST['Submit'])) {
echo $results_array[$_POST['pty_select']]['profile'];
}
Or alternatively use profile option value.