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/
Related
Can you please help me out as to what I am doing wrong with this code:
<option value="">------------ Select ------------</option>
<?php while (($row = mysql_fetch_array($tech))) {
?>
<option value= <?php echo $row['id'] ?>
<?php echo $selectedtechname ?>
<?php echo "selected" ?>
>
<?php echo $row['technician_name'] ?></option>
<?php } ?>
All variables are working fine since I used Var_dump and they echo correctly so no errors is SQL etc. What I need to do is pass a php statement in the option tag of HTML to simply select the value of $selectedtechname from the list. The value to post is $row['id'] but the selected value to show should be $selectedtechname
Thanks
I see a similar post
populate a select box with php mysql
while($row = mysql_fetch_assoc($result)) {
if ($row['technician_name'] == $selectedtechname) {
echo '<option value=\"'.$row['id'].'" selected>'.$row['id'].'</option>';
} else {
echo '<option value=\"'.$row['id'].'">'.$row['id'].'</option>';
}
}
You can try the following:
$selected='';
<option value="">Select a value</option>
<?php
while ($row = mysql_fetch_array($tech)) {
?>
<?php
if($selectedtechname == $row['selectedtechname'])
{
$selected='selected';
}
else
{
$selected='';
}
?>
<option value="<?php echo $row['id']?>" <?php echo $selected;?> ><?php echo $row['value_to_display'];?></option>
<?php
}
?>
<?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
I'm trying to create a drop down menu that will select a value that is stored in the database. here's the code :
require 'koneksi.php';
$sql_select = "SELECT * FROM supplier";
$hasil = mysql_query($sql_select);
if(!$hasil) {
echo "data supplier not found ".mysql_error();
}
$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if($hasil2) {
$supplier = $data['nama_supplier'];
}
<select name="supplier">
<option value="">---pilih supplier---</option>
<?php
while($baris = mysql_fetch_array($hasil)){
?>
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
<?php }?>
</select>
the problem is my code creates a dropdown with nothing selected. here's the screenshot : link
i've tried all the solutions in the stackoverflow. but the dropdown value still nothing selected. i know that it has to be something simple that i am missing but seriously i cannot figure it out. please anyone help, thanks!
I think the problem lies in this line:
<option value="<?php $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) echo 'selected="selected"'; ?> > <?php echo $baris['nama_supplier']; ?> </option>;
You're missing an echo and it looks funny :/
Try instead:
<option <?php $val=$baris['nama_supplier']; echo "value='$val'"; if($supplier==$val) echo "selected='selected'>";echo $val;?> </option>;
Try this way..
$sel="select f.*,c.category from final f, category c where f.category_id=c.id and f.id='$id'";
$data=mysql_query($sel);
$res=mysql_fetch_assoc($data);
<select name="cat">
<?php
$sql = mysql_query("SELECT * FROM category");
while ($row = mysql_fetch_array($sql)){
if($row['id'] == $res['category_id']){
echo '<option value="'.$row['id'].'" selected="selected">'.$row['category'].'</option>';
}else{
echo '<option value="'.$row['id'].'">'.$row['category'].'</option>';
}
}
?>
</select>
Try this out
require 'koneksi.php';
$sql_select = "SELECT * FROM supplier";
$hasil = mysql_query($sql_select);
if(!$hasil) {
echo "data supplier not found ".mysql_error();
}
$id = $_GET["id"];
$hasil2 = mysql_query("SELECT * FROM brg_supplier WHERE id_brg=".$id);
$data = mysql_fetch_array($hasil2);
if(mysql_num_rows($hasil2) > 0) {
$supplier = $data['nama_supplier'];
}
<select name="supplier">
<option value="">---pilih supplier---</option>
<?php
while($baris = mysql_fetch_array($hasil)){
?>
<option value="<?php echo $baris['nama_supplier'] ?>" <?php if ($supplier==$baris['nama_supplier']) {echo 'selected="selected"';} ?> > <?php echo $baris['nama_supplier']; ?> </option>;
<?php }?>
</select>
<option value="<?php $baris['nama_supplier'] ?>
should be
<option value="<?php echo $baris['nama_supplier'] ?>
I spent some time trying to find the best solution for this, and came up with a tiny little jQuery code.
First of all you should be using PDO or mysqli instead of mysql, since it's deprecated. Let's assume you've fixed that.
Inside the <form> tag, add an <input type="hidden"/> so that it can storage your database value, for example:
HTML
<form>
<input id="valueFromDatabase" type="hidden" value="<?php echo $stringFromDB ?>"/>
</form>
Note: in this case, $stringFromDB is a variable that holds your query's return from DB.
So now we have the value of our database inside our HTML code. Now we just need to check if any of the options inside the <select> tag is equal to this value. We'll be doing it with jQuery:
jQuery
$( document ).ready(function() {
$('option').each(function(){
if (this.value == $('#valueFromDatabase').val()){
this.setAttribute('selected', 'selected');
}
});
});
What's happening here? We are telling to jQuery to analyze all the <option> tags in the HTML and compare its value with our value from database; if its equal, we add the selected attribute to the equivalent <option>.
You can it working here (used a calendar example).
Hope that helps!
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>';
}
I have an html dropdown box. Then I use an array to fill the items in it.
The keys in this for each loop is just a number from 0 - 9. My problem now is how can I control what shows up as the default choice:
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>" selected="<?php if($k==1){ echo "selected"; } ?>"><?php echo $k + 1; ?></option>
<?php } ?>
In this code, you can see that I'm attempting to make the 2nd item as the default choice.
But it seems like I'm always brought to the last array item whatever number I type as the condition.
Try this:
<?php foreach($cat_r as $k=>$c){ ?>
<option value="<?php echo $k + 1; ?>" <?php if($k==1){ echo 'selected="selected"'; } ?>><?php echo $k + 1; ?></option>
<?php } ?>
Or, this format works too
<option value="foo" selected />
<?php foreach($cat_r as $k=>$c){ ?>
<option name="<?php echo $k + 1; ?>" value="<?php echo $k + 1; ?>"
<?php if($k==1){ echo "selected=\"selected\""; } ?>>
<?php echo $k + 1; ?>
</option>
<?php } ?>
I came here for finding solution to set options selected by user before as selected , above question was in different context and I ended up in scratching my head to this thing...
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}
This code was functional for me, thanks..
foreach($options as $k){
echo "<option value=".$k." ";
foreach($selectedoptions as $m){
if($k==$m)
echo "selected='selected'";
}
echo ">".$k."</option>";
}