Why preg_all_match returning two results? Where is the problem? Thanks for your helps.
<?php
$site=file_get_contents("http://www.site.gen.tr");
preg_match_all('#<div class=\'search\'>(.*?)<\/div>#si',$site,$baslik); ?>
<select name="sub_category" id="sub_category_id">
<option value="" selected="selected">Bir Sey Seçin</option>
<?php for($i=0; $i < count($baslik); $i++) { ?>
<option value="<?php echo $baslik[0][$i];?>"><?php echo $baslik[0][$i]; ?></option>
<?php } ?>
</select>
Change your for loop to:
for($i=0; $i < count($baslik[0]); $i++) {
Related
I have this loop coded to create a dropdown with the numbers 0-30.
<td style="text-align:left;">
<select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i=0; $i<=30; $i++) {
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
I cannot seem to figure out how to get the value of I to work in my calculations on an order processing form.
I assign the variable using
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
but if I try to output $iPhone12 * 15 for example the answer is always zero.
Make sure you select is wrapped in a form element with method="POST". You should also use isset to make sure it exists.
<form action="" method="post">
<td style="text-align:left;"><select id="iPhone12Case" name="iPhone12Case">
<?php
for ($i = 0; $i <= 30; $i++) {
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select></td>
<button>submit</button>
</form>
<?php
if (isset($_POST['iPhone12Case'])) {
$iPhone12 = htmlspecialchars($_POST['iPhone12Case']);
printf($iPhone12 * 15);
}
I'm having an stupid issue here; I'm trying to make in a form, a select with the time the user starts, and the time they finish, so logically, I want to display the time as:
00
01
02.. I mean, 2 digit
But although I put 00 for the initial value of $i, it displays only 1 digit anyway!
Is it any way I can set it to two digits?
Thanks!!
<div class="interval">
<input type="checkbox" id="interval_check"/>
<div id="desde">
De: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div> <div id="fins">
fins: <select class="hores" name="hores" disabled>
<?php for ($i = 00; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>:<select class="minuts" name="minuts" disabled>
<?php for ($i = 00; $i <= 45; $i+=15) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
just use sprintf to create the string for you.
<?php for ($i = 0; $i <= 23; $i++) : ?>
<option value="<?php echo $i; ?>">
<?php echo sprintf("%02d", $i); ?>
</option>
<?php endfor; ?>
this will add a 0 if the string is less than 2 characters. I'm presuming that you don't care about the value.
My Drop down menu to choose a date does not work unless I hold down left click on my mouse.
Only the year drop down works correctly but the month and days only work if you hold your mouse button down.
Here is the website it is on: www.cipslimoshuttle.com/tickets
Here is the code:
<label>
Date of travel:
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
</label>
The problem is with your label tag You need to end before the select box.Check this it will work
<label>
Date of travel: </label>
<select name="ticketYear">
<?php
$i = date('Y');
while($i <= date('Y')+5){
?>
<option <?php if(date('Y') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketDay">
<?php
$i = 1;
while($i <= 31){
?>
<option <?php if(date('j') == $i) echo 'selected' ?> value="<?php echo $i ?>"><?php echo $i ?></option>
<?php
$i++;
}
?>
</select>
<select name="ticketMonth">
<?php
$months = array(
'01-January',
'02-February',
'03-March',
'04-April',
'05-May',
'06-June',
'07-July',
'08-August',
'09-September',
'10-October',
'11-November',
'12-December'
);
foreach($months as $month) {
$month = explode('-',$month);
?>
<option <?php if(date('m') == $month[0]) echo 'selected' ?> value="<?php echo $month[0] ?>"><?php echo $month[1] ?></option>
<?php
}
?>
</select>
How to make the current month selected by default in option select using php
Here is what i have tried so far.
$curmonth = date("F");
And to display the entire month
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
And according to the above code, I am assigning the current month as $curmonth, and inside loop assigning the $allmonth for entire, month.
And inside the Value
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
for checking if the current month equals all month and displaying the selected to make it select. But i am not getting result.. What i am getting is all the items are being displayed in the option select.
What i am missing ?
What you have missed is,
You are trying to display the selected inside the value
What you need to do is
<option value="<?php
echo $i;
?>"
<?php
if($allmonth==$curmonth)
{
echo ' selected';
}
?>
>
<?php
echo $allmonth;
}
?>
</option>
So, the result will be
<select >
<option value="1">
January<option value="2">
February<option value="3">
March<option value="4">
April<option value="5">
May<option value="6">
June<option value="7">
July<option value="8">
August<option value="9" selected>
September<option value="10">
October<option value="11">
November<option value="12">
December</option>
You're not correctly closing the <option> tags you are creating. Indenting you're code makes these issues more apparent:
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
//Close tag inside loop
?>
</option>
<?php
}
Hope you are wrong here
<option value="<?php
echo $i; ?>"
<?php
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
You have missed closing quotes for the value
Take a look on this example:
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo $month . " - Selected \r\n";
else
echo $month . "\r\n";
}
IN HTML FORMAT:
echo "<select>";
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo "<option value='$month' selected='selected'>" $month . "</option>";
else
echo "<option value='$month'>" $month . "</option>";
}
echo "</select>";
DEMO
You have error with closing quotes in the value of options in the line
<option value="<?php
echo $i; if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
^ // This is the quotes opened for value and is not properly closed
>
Here you have to close the quotes. As per your code it will display as:
<option value="9 selected" >
Also you are not closing <option> properly. Change your code to :
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i;?>" <?php if($curmonth==$allmonth){echo 'selected';}?> >
^ // close quotes here
<?php
echo $allmonth;?>
</option>
}
</select>
I am trying to create a drop down menu with the options of 1,2,3 and 4.
The below code is what I am using just now and the dropdown is empty.
Any idea what I am doing wrong?
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
"<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You are not outputting the option tags.
Try it like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
echo "<option value=".$i.">".$i."</option>";
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
You basically use html without closing the php syntax.Your code should look like this:
<select name="years">
<?php
for($i=1; $i<=4; $i++)
{
?>
<option value="<?php echo $i;?>"><?php echo $i;?></option>
<?php
}
?>
<option name="years"> </option>
</select>
<input type="submit" name="submitYears" value="Year" />
Or are you trying to echo the option? In that case you forgot the echo statement:
echo "<option value= ".$i.">".$i."</option>";
This worked for me. It populates years as integers from the current year down to 1901:
<select Name='ddlSelectYear'>
<option value="">--- Select ---</option>
<?php
for ($x=date("Y"); $x>1900; $x--)
{
echo'<option value="'.$x.'">'.$x.'</option>';
}
?>
</select>
You forgot something..
Add print / echo before "<option value=".$i.">".$i."</option>";
place an echo in your loop to output your options.
echo "<option value=".$i.">".$i."</option>";
Just echo the <option> tag
echo "<option value=".$i.">".$i."</option>";
<select name="year">
<?php for ($i = 0; $i <= 9; $i++) : ?>
<option value='<?= $i; ?>' <?= $fetchData->year == $i ? 'selected' : '' ?>><?= $i; ?></option>
<?php endfor; ?>
</select>