Jump to content

killerb

Members
  • Posts

    48
  • Joined

  • Last visited

    Never

Contact Methods

  • Website URL
    http://freewebs.com/good-code

Profile Information

  • Gender
    Not Telling

killerb's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. <?php $query = "SELECT * FROM tablename WHERE colname LIKE '{$letter}%'";
  2. When you have data scattered all around different tables, the only way to optimize a heavy join is to index periodically and select your *whatever_id* from the index table. This is common practice for example on searches where the best efficiency is required on select whereas a few seconds delay on update/insert is tolerable.
  3. No can't help without getting into debugging (process of identifying cause of problem), therefore chargeable work. Sorry, not taking on small jobs presently.
  4. Parse errors mean the code is invalid PHP format, something is missing and it invalidates the PHP code so it cannot be interpreted: Needs a semicolon after , dtaken ASC" Needs a right square bracket after $week_stats[$row['wtaken']
  5. Dunno if this is the easiest or most elegant but it will achieve your requirements. You may need to tweak for performance on large data sets. This is untested, written quick for demonstration of how I have solved this sort of problem before: <?php $query = "select * from .. where dtaken >= :start_date and dtaken <= :end_date order by wtaken ASC, dtaken ASC" $bind = array('start_date'=>$start_date, 'end_date'=>$end_date); $rows = $DB->fetchAll($query, $bind); $week_stats = array(); foreach($rows as $row){ if(!array_key_exists($row['wtaken'], $week_stats)){ $week_stats[$row['wtaken'] = array( 'total' => 0, 'num' => 0 ); } // store the total of all averages for calculating averages $week_stats[$row['wtaken']]['total'] += $row['ans1']; // count number of answers for calculating averages $week_stats[$row['wtaken']]['num']++; } // calculate the averages foreach($week_stats as $stat){ $week_stats['average'] = $stat['total']/$stat['num']; } var_dump($week_stats);
  6. Another PHP'er is asking this too: http://www.phpfreaks.com/forums/index.php/topic,280683.msg1329657.html#msg1329657 You'll want to add the following method to one of your classes for converting mysql date formats to unix timestamp, or you can use the mysql function UNIX_TIMESTAMP() in your query: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp <?php public static function make_tstamp($date, $default_return_value=0, $default_time='00:00:00'){ if(!is_string($date)) return $default_return_value; $date_parts = explode(' ', $date); if(count($date_parts)==1){ // date only list($year, $month, $day) = explode('-', $date_parts[0]); list($hour, $min, $sec) = explode(':', $default_time); if(intval($hour+$min+$sec+$year+$month+$day)===0){ return 0; } return mktime(intval($hour), intval($min), intval($sec), intval($month), intval($day), intval($year)); }elseif(count($date_parts)==2){ // date and time list($year, $month, $day) = explode('-', $date_parts[0]); list($hour, $min, $sec) = explode(':', $date_parts[1]); if(intval($hour+$min+$sec+$year+$month+$day)===0){ return 0; } return mktime(intval($hour), intval($min), intval($sec), intval($month), intval($day), intval($year)); }else{ // invalid date format - return default tstamp now return $default_return_value; } } Once you have the datetimes as unix timestamps and the two functions from link above, you can write something like this: <?php $row = mysql_fetch_row($resource); $start_tstamp = your_object::make_tstamp($row['start_date']); $end_tstamp = your_object::make_tstamp($row['end_date']); $duration = timestamp_difference_units_to_string(timestamp_difference_to_units($start_tstamp, $end_tstamp));
  7. I used this function to display time remaining until an online auction finishes, which is typically days, hours, mins and secs. It does not handle leap years or months, but it may be sufficient for what you need: <?php $from_tstamp = mktime(0, 0, 0, 2, 1, 2009); $to_tstamp = mktime(0, 0, 2, 2, 1, 2009); var_dump(timestamp_difference_units_to_string(timestamp_difference_to_units($from_tstamp, $to_tstamp))); function timestamp_difference_to_units($from_tstamp, $to_tstamp){ $from_tstamp = intval($from_tstamp); $to_tstamp = intval($to_tstamp); $seconds_remaining = $to_tstamp-$from_tstamp; if($seconds_remaining < 0){ $retval = timestamp_difference_to_units($to_tstamp, $from_tstamp); foreach($retval as $k=>$v){ $retval[$k] = -$v; } return $retval; } $num_years = 0; $num_days = 0; $num_hours = 0; $num_mins = 0; $num_secs = 0; while($seconds_remaining >= 31536000){ $num_years++; $seconds_remaining -= 31536000; } while($seconds_remaining >= 86400){ $num_days++; $seconds_remaining -= 86400; } while($seconds_remaining >= 3600){ $num_hours++; $seconds_remaining -= 3600; } while($seconds_remaining >= 60){ $num_mins++; $seconds_remaining -= 60; } $num_secs = $seconds_remaining; $seconds_remaining = 0; return array( 'years' => $num_years, 'days' => $num_days, 'hours' => $num_hours, 'minutes' => $num_mins, 'seconds' => $num_secs ); } function timestamp_difference_units_to_string($diff){ $parts = array(); if($diff['years']>0){ $parts[] = $diff['years'].' years'; } if($diff['days']>0){ $parts[] = $diff['days'].' days'; } if($diff['hours']>0){ $parts[] = $diff['hours'].' hours'; } if($diff['minutes']>0){ $parts[] = $diff['minutes'].' mins'; } if($diff['seconds']>0){ $parts[] = $diff['seconds'].' secs'; } $last_part = array_pop($parts); $retval = implode(', ', $parts); if(count($parts)>0){ $retval .= ' and '.$last_part; }else{ $retval .= $last_part; } return $retval; }
  8. Interested to see this architecture, particularly "to get the data put wherever it needs to be put", is the code available?
  9. Reminds me of a problem I had with exec and mysqldump, the -p parameter was fine in shell as such: mysqldump -u root -p mypass but when running through PHP's exec() function it had to have no space after the -p argument: mysqldump -u root -pmypass Took a bit of stabbing in dark to discover this, hope the info might be of some help.
  10. It is common practice to put PHP processing at top of the file and have the form posting to the same file, this way PHP attempts to process a submitted form if possible and displays the form afterwards, whether or not the form was processed. Your task to list files and upload files is better to do with two files: upload.php list.php Begin with upload.php, place your html form into that file and above your form, put your processing logic within PHP tags. eg: <?php if(count($_POST)>0){ // processing goes here, validate and store the uploaded files } ?> <html> <body> <?php // output any errors that were encountered during form processing ?> <form method="post"> <!-- ommitting action attribute forces form submission to same uri --> </form> </body> </html> On your list page you will want to get all files into an array and then loop them: <?php $dh = opendir(dirname(__FILE__)); $files = array(); while($file = readdir($dh)){ if($file=='..' || $file=='.') continue; $files[] = $file; } ?> <html> <body> <h1>List of files:</h1> <ul> <?php foreach($files as $file){ echo '<li>'.$file.'</li>'; } ?> </ul> </body> </html> This way the PHP logic is separated from the HTML markup, and the PHP logic within HTML is for display only. It is generally advised that all PHP data retrieval and preparation is done before HTML begins, as you can see in 2nd example that all files are prepared into an array before the template begins. Its a personal preference of course, but you will find such conventions assist the separation of roles according to MVC principles, even without applying strict MVC rules. Also using the same file (or identical file path and file name in /controllers and /templates) for processing and template assists in tracking down the right file when it comes to maintaining a code base in future.
  11. If both servers run PHP, you will need one server to open a connection, request/push the xml from/to the other server. If pushing, you should use HTTP post to send the xml data, otherwise you can use curl, fread or file_get_contents for pulling an xml download into PHP and then store it as required. Note if you are able to see the xml on your server already, half the work is done you just need a PHP script on the recipient server to download the xml and store it, and of course a cron or something to invoke the script when required.
  12. killerb

    PDFLib

    There's FPDF which I've used with no problems and there's Zend_Pdf, which I only played with a year ago or so.
  13. Hi, yes it is an option to install the system on localhost and set the client dns to my IP so peer can perform tests, however was wanting to know if it is possible to set PHP's time() value at runtime because that would be much easier and preferable, guess from answers here its not the easiest solution..
  14. Hi, thanks for help, yes it is a development server but I am a programmer and not sysadmin, wanting to know if I can do it with PHP without having to open a ticket.
  15. Try wrapping the test in count($_POST) to be sure the form has been submitted, your condition is evaluating false because a) isset($_POST['go']) is false until form is submitted b) !empty($_POST['name']) is false until form submitted c) preg_match($pattern, $_POST['name']) is false because form not submitted EG: <?php class error { var $error = "You must put in a name"; var $good = "good work, name has been entered"; function myError(){ $pattern = '/[A-Za-z]+/'; if(count($_POST)>0){ // form submitted if(isset($_POST['go']) && !empty($_POST['name']) && preg_match($pattern, $_POST['name'])) { return $this->good; } else { return $this->error; } }else{ // form not submitted return ''; } } }
×
×
  • 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.