Jump to content

HOW TO SEARCH A FILE IN PHP


Bala

Recommended Posts

hi all iam new beginner to php just 20 days old iam basically a unix admin. As my role depend on report generation and performance analysis i had written a unix shell script which convert in to html and saves in particular directory sample

 

/var/www/html/backup/mar/29-03-2008.html

 

like above the consalidated output for all days are manintained

 

now i used a html form/post method to get input like date month and year all are sepeate drop down menu the script is given below

 

<html><title><head></head></title>

<body>

<form action="search.php" method="POST">

PLEASE ENTER THE DATE MONTH AND YEAR <select name="days">

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

<option>5</option>

<option>6</option>

<option>7</option>

<option>8</option>

<option>9</option>

</select>

<select name="month">

<option>1</option>

<option>2</option>

<option>3</option>

<option>4</option>

<option>5</option>

<option>6</option>

<option>7</option>

<option>8</option>

<option>9</option>

</select>

<select name="year">

<option>2008</option>

<option>2009</option>

</select>

<input type=submit name=search value=search>

</form>

</body>

</html>

 

 

and php script is as below

<?php

$dd=$_POST[days];

$mm=$_POST[month];

$yy=$_POST[year];

$j=$dd."-".$mm."-".$yy.".".html;

echo $j;

$def_dir="/var/www/html/backup/$mm";

chdir ($def_dir);

print_r(glob($j));

?>

 

if a person search for particular day the script should display the report of that particular day plz i need a help

 

Link to comment
Share on other sites

Just to get you started:

<HTML>
<form action="search.php" method="POST">
<select name='days'>
  <?php
  for ($x=1;$x<=31;$x++){ // basic FOR loop to easily create the html
    echo "<option value='$x'>$x</option>";
  }
  ?>
<select name='month'>
  <?php
  for ($x=1;$x<=12;$x++){ 
    echo "<option value='$x'>$x</option>";
  }
  ?>
<select name='year'>
  <?php
  for ($y=2007; $y <= date('Y') ;$y++){ 
  // date('Y') returns the current 4-digit year. This will show all years from 2007 to the current year.
    echo "<option value='$y'>$y</option>";
  }
  ?>
<input type='submit' name='search' value='search'>
</form>
</HTML>

<?php
// search.php
$dd=$_POST[days];
$dd = str_pad($dd, 2, '0', 1); // If it is less than 2 characters long, add a 0 on the left side
$mm=$_POST[month];
$mm = str_pad($mm, 2, '0', 1); // If it is less than 2 characters long, add a 0 on the left side
$yy=$_POST[year];
$j=$dd . "-" . $mm . "-" . $yy . ".html";
echo $j;
$month = strtolower(date('M', strtotime("$yy$mm$dd 12:00:00"))); // A lot going on here
// strtolower makes the results all lowercase to match your 'mar' for march
// date('M', ...) returns the 3-letter form of month
// strtotime() is returning the unix timestamp for date() for the submitted date, at 12 noon.
$def_dir="/var/www/html/backup/$month";
?>

I've used some simpler functions here because if we used sprintf() you'd be in a corner crying (I say that because after a year, I finally figured out how to use it, otherwise I was crying). So yeah, there you go. I didn't test it, I just wrote it, but it should get you going in the right direction.

Link to comment
Share on other sites

hi ucffool

 

Thanks for your reply man its really helping me out instead of writing for loops in php ive used html for date ,month and year option its really helping me out here

very much thanks to you

 

Regards

Balasubramanian.N

Link to comment
Share on other sites

Just to get you started:

<HTML>
<form action="search.php" method="POST">
<select name='days'>
  <?php
  for ($x=1;$x<=31;$x++){ // basic FOR loop to easily create the html
    echo "<option value='$x'>$x</option>";
  }
  ?>
<select name='month'>
  <?php
  for ($x=1;$x<=12;$x++){ 
    echo "<option value='$x'>$x</option>";
  }
  ?>
<select name='year'>
  <?php
  for ($y=2007; $y <= date('Y') ;$y++){ 
  // date('Y') returns the current 4-digit year. This will show all years from 2007 to the current year.
    echo "<option value='$y'>$y</option>";
  }
  ?>
<input type='submit' name='search' value='search'>
</form>
</HTML>

<?php
// search.php
$dd=$_POST[days];
$dd = str_pad($dd, 2, '0', 1); // If it is less than 2 characters long, add a 0 on the left side
$mm=$_POST[month];
$mm = str_pad($mm, 2, '0', 1); // If it is less than 2 characters long, add a 0 on the left side
$yy=$_POST[year];
$j=$dd . "-" . $mm . "-" . $yy . ".html";
echo $j;
$month = strtolower(date('M', strtotime("$yy$mm$dd 12:00:00"))); // A lot going on here
// strtolower makes the results all lowercase to match your 'mar' for march
// date('M', ...) returns the 3-letter form of month
// strtotime() is returning the unix timestamp for date() for the submitted date, at 12 noon.
$def_dir="/var/www/html/backup/$month";
?>

I've used some simpler functions here because if we used sprintf() you'd be in a corner crying (I say that because after a year, I finally figured out how to use it, otherwise I was crying). So yeah, there you go. I didn't test it, I just wrote it, but it should get you going in the right direction.

 

 

hi ucffool

 

Thanks for your reply man its really helping me out instead of writing for loops in php ive used html for date ,month and year option its really helping me out here

very much thanks to you

 

Regards

Balasubramanian.N

Link to comment
Share on other sites

had a little fidle to learn str_pad

 

 

my long example

<?php

if($_POST['submit']){

$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];

$res=$_POST['res'];

$res=$day.$month.$year;

if($res=='01112008'){

echo"<center>Hi there m8 correct date!</center>";

}else{

echo "<center>Incorrect Date!</center>";
} 
}

$year=range(2008,2020);
$day=range(1,31);
$month=range(1,12);

echo"<center>";

echo"<form method='post' action=''>

<p></p>
Please enter a search via date!
<p></p>

<select name='day'>";
foreach($day as $days){
$days=str_pad($days,2,'0',STR_PAD_LEFT);
echo"<option value='$days'>$days</option>";
}

echo "</select>";

echo"<select name='month'>";
foreach($month as $months){
$months=str_pad($months,2,'0',STR_PAD_LEFT);
echo"<option value='$months'>$months</option>";
}

echo "</select>";

echo"<select name='year'>";
foreach($year as $years){
echo"<option value='$years'>$years</option>";
}


echo "</select>";

echo"<p></p><input type='submit' name='submit' value='Send'>";

echo"</form></center>";

?>

 

Link to comment
Share on other sites

$months=str_pad($months,2,'0',STR_PAD_LEFT);

While that works for str_pad(), luckily, php.net states it is expecting an integer for STR_PAD_LEFT.

The only reason I mention this is that some other functions, as I learned putting a book together (see my sig in a few days, once reviews are back), they state they take an integer and they only accept the string version of the flag, and other functions accept only an integer that represents one of the flags.

 

On a sidebar, I always forget about range, I wonder which loop is faster, the for() or foreach() with range.

Oh heck, ran a test:

<?php
$time = microtime(1);
$month = range(1,1000);
foreach($month as $months){
  $months=str_pad($months,2,'0',1);
}
$time = number_format(microtime(1)-$time, 6);
echo "$time seconds to complete FOREACH with RANGE.";

$time = microtime(1);
for($month=1;$month<=1000;$month++){
  $months=str_pad($month,2,'0',1);
}
$time = number_format(microtime(1)-$time, 6);
echo "$time seconds to complete FOR loop.";
?>

Results:

0.002322 seconds to complete FOREACH with RANGE.

0.002290 seconds to complete FOR loop.

 

I ran it a bunch of times, and while the values varied, FOR was 95% of the time faster, about 2%-25% faster.

Of course, this is less than 1/1000th of a second, and we are doing a loop of 12 values, not 1000... so it's a moot point. Just thought I would share.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.