php-general Digest 19 Jan 2005 11:08:01 -0000 Issue 3236

Topics (messages 206719 through 206735):

$_SERVER['HTTP_USER_AGENT'] in html email
        206719 by: Graham Anderson
        206722 by: Graham Anderson

Re: Recursive Array Iterator
        206720 by: Gerard Samuel

Re: searching and sorting
        206721 by: Brian A. Anderson
        206723 by: Michael Sims

How to access remote files with php?
        206724 by: Sephiroth

Re: Writing static file from dynamic PHP page
        206725 by: Hugh Danaher

Nested SQL Statements
        206726 by: Greg Cullen

Re: Pagination Optimization
        206727 by: yangshiqi

debugging
        206728 by: William Stokes
        206732 by: Justin French
        206733 by: William Stokes

my for loop's gone bananas.. has yours?
        206729 by: Tim Burgan
        206731 by: Binoy AV
        206735 by: Ford, Mike

SPL Exceptions
        206730 by: Gerard Samuel
        206734 by: M. Sokolewicz

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        php-general@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
is it possible to use $_SERVER['HTTP_USER_AGENT'] in an html email ?

something like:
//What should we download based on user's platform ?
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Macintosh') !== FALSE) {
$download  = 'Fonovisa.dmg';
echo 'Mac';
}
else
{
echo 'PC';
$download = '!Fonovisa.exe';
}

--- End Message ---
--- Begin Message ---
What would be the standard/accepted  way to do this?

is it better to spell it out in the html email ?
Download:
Mac             PC              Linux ?

many thanks :)
On Jan 18, 2005, at 4:06 PM, [EMAIL PROTECTED] wrote:


php is server-side, so no. you might be able to do what you're after
with javascript, but any sensible person has that turned off in
general, and especially for html e-mail.

[remember, there are more operating systems/platforms than pc and mac
(with OS variations within those), so defaulting to "download" if it's
not mac would not be a nice thing to do. the download should be on the
positive match, not the fall-through.]

---------- Original Message ----------
From: Graham Anderson <[EMAIL PROTECTED]>
To: "<php-general@lists.php.net> <php-general@lists.php.net>"
<php-general@lists.php.net>
Date: Tuesday, January 18, 2005 03:08:16 PM -0800
Subject: [PHP] $_SERVER['HTTP_USER_AGENT'] in html email

is it possible to use $_SERVER['HTTP_USER_AGENT'] in an html email ?

something like:
//What should we download based on user's platform ?
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Macintosh') !== FALSE) {
$download  = 'Fonovisa.dmg';
echo 'Mac';
}
else
{
echo 'PC';
$download = '!Fonovisa.exe';
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

---------- End Original Message ----------


--- End Message ---
--- Begin Message ---
Gerard Samuel wrote:

Im trying to figure out how to create this.
But Im stuck in the foreach loop.
For some reason, $value in the foreach loop is not an object (Isn't is supposed to be?)
Any hints to get me in the right direction.
Thanks


$array = array(1 => array(2 => array(3 => array(4))));

class recursiveArrayIterator extends ArrayIterator implements RecursiveIterator
{
function __construct($array)
{
parent::__construct( $array );
}


   function hasChildren()
   {
   }

   function getChildren()
   {
   }
}

$obj = new recursiveArrayIterator( $array );
//reflectionObject::export($obj);
foreach($obj as $value)
{
   var_dump($value);
}


Never mind. I figured it out...
--- End Message ---
--- Begin Message ---
Quoting: "Richard Lynch" <[EMAIL PROTECTED]>




> Unless you KNOW your site will get loads of traffic from the get-go, I'd
> focus more on the SIMPLE solutions that you can maintain as a beginner.

Quoting: "Richard Lynch"

> Unless you KNOW your site will get loads of traffic from the get-go, I'd
> focus more on the SIMPLE solutions that you can maintain as a beginner.

well, I have implemented a simple solution, and it is working, but I am
thinking in terms of in the coming year or two of developing more
sophisticated strategies.


> Also depending on the size and scale and scope of your data, I'm not sure
> that two separate lists is a Good Idea.

Why not? I have arount 1000 products and many searches bring up more than a
few pages worth of data. I would think that it might be good to present the
links to test files along the side.

Here is a link to the actual site:
http://www.apsistl.com/

> Amazon may need it for, their zillion items they sell, but do you, really?
>
> I've spent *DAYS* writing special search engines for data-sets that only
> had 100 items in them, and would never grow significantly larger than
> that.  I told my client that was pretty wasteful of their money, but
> that's what they wanted. [shrug]  Pays the bills.  But that doesn't mean
> you want to waste your money/time that way.

Right now the people visiting my website are complaining that when they
search for "sports cars" for instance, that they get many sports items
sorted first before they even get to see the "sports cars". I realize that
this is a separate issue, but it illustrates that even though my search is
working, it could be improved. I look forward to figuring out how to do it
better. Regardless, my data sets may not always be limited to 1000 products
(not to mention the text pages).

> One Axiom: Keep as much of the scoring/sorting in your SQL as possible --
> That's what SQL engines are best at.

I agree. That is a wise statement, and I have tried to do this with regular
expressions but this doesn't deal with relevance at all, or is there
something that I am missing? I suppose I could do multiple searches on my
data to look for "sports cars" first, "cars" second, and "sports" last. Then
I could deal with the separate issue of the text files.


-Brian

--- End Message ---
--- Begin Message ---
Richard Lynch wrote:
> Brian A. Anderson wrote:
[...]
>> I am thinking of incrementally adding the resultant hits into two
>> associative arrays with the link to the data and a calculated
>> relevance value, and sorting this array by these relevences.
[...]
> One Axiom: Keep as much of the scoring/sorting in your SQL as
> possible -- That's what SQL engines are best at.

I suggest the original poster look into some of the full text indexing
capabilities of his SQL server.  A lot of these (for PostgreSQL and MySQL
anyway) will automatically return a relevance value and handle sorting based
on that.  As you said, that'll be a heck of a lot more efficient and easier
to implement that doing it in PHP.

--- End Message ---
--- Begin Message ---
Hi all,

How to access remote files with php?
For ex:
$sFile = "http://www.php.net/123.txt";;
if (file_exists($sFile)) {
  $hFile = fopen($sFile);
  ...
  fclose($hFile);
}


Regards,
Sephiroth

--- End Message ---
--- Begin Message --- Chris,
I've done something like this to update website's static pages. Basically, in a php script I've replaced all of the "print" statements with "$line.= " statements to accumulate all the items you want printed into one long string. I've then ftp'ed this string, along with the name of the html page, to the website where the page is to be viewed.
You could also set up an html page with links to a php page that generates the above stuff, and then use a header() command as the last line of the php page to go to the .html page you just generated.
Hope this helps.
Hugh


----- Original Message ----- From: "Chris Bruce" <[EMAIL PROTECTED]>
To: "PHP-General" <php-general@lists.php.net>
Sent: Monday, January 17, 2005 7:16 PM
Subject: [PHP] Writing static file from dynamic PHP page



Hi,

I am looking for a way to write to a file what the browser would see (raw html) when viewing a dynamic PHP page. I have numerous include files, MySQL db queries, loops etc. and I want to generate the static result of that file and save it as an html page. I have toyed a little with output buffering to no avail.

Is there an easy way to do this?

Thanks,

Chris

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 1/16/2005





-- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.300 / Virus Database: 265.6.13 - Release Date: 1/16/2005

--- End Message ---
--- Begin Message ---
Relatively new to PHP.  Having an issue trying to nest sql statements. 
Basically I am trying to pull a variable from SQL1, Pass it as a 
Variable/Bind or Parm to SQL2 and then Go back to SQL1 and pull the next 
value and pass to SQL2 again for processing.

It seems like the SQL2 is getting stuck on the first value passed by SQL1. 
Like SQL2 does not rebuild with the new variable.

In my example I am testing reading all my tables and reporting their 
definitions.  I have other uses for this technique if PHP and MySQL support.


<?

require_once ('mysql_connect.php');


   $result1 = mysql_query('show tables',$dbc);


   if ($myrow1 = mysql_fetch_array($result1))
    {

      // display list if there are records to display

  $tmptablename = sprintf("describe {$myrow1[0]}");


       do {


  $result2 = mysql_query($tmptablename,$dbc);

  echo "Table: {$myrow1[0]}";

        // Create page headers

   echo "<table border=\"1\" cellspacing=\"1\" width=\"80%\" 
id=\"{$myrow1[0]}\">";
   echo "<tr>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Field</td>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Type</td>";
   echo "<td width=\"5%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Null</td>";
   echo "<td width=\"10%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Key</td>";
   echo "<td width=\"20%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Default</td>";
   echo "<td width=\"25%\" bgcolor=\"#006600\"><b><font 
color=\"#FFFFFF\">Extra</td>";
   echo "</tr>";

       if ($myrow2 = mysql_fetch_array($result2))
           {

          // display list if there are records to display

           do {

          echo "<tr>";
          echo "<td width=\"20%\">";
       if ("{$myrow2['Field']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Field']}";
       }

      echo "</td>\n";

          echo "<td width=\"20%\">";
       if ("{$myrow2['Type']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Type']}";
       }

      echo "</td>\n";


          echo "<td width=\"5%\">";
       if ("{$myrow2['Null']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Null']}";
       }

      echo "</td>\n";

          echo "<td width=\"10%\">";

       if ("{$myrow2['Key']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Key']}";
       }

      echo "</td>\n";

          echo "<td width=\"20%\">";

       if ("{$myrow2['Default']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Default']}";
       }

      echo "</td>\n";

          echo "<td width=\"25%\">";
       if ("{$myrow2['Extra']}"==null)
       {
        echo "&nbsp";
       }
       else
       {
        echo "{$myrow2['Extra']}";
       }
      echo "</td>\n";
          echo "</td>\n";
          echo "</tr>\n";

               } while ($myrow2 = mysql_fetch_array($result2));

             echo "</table>";
         echo "<br>";

     $myrow2 = Null;

      mysql_free_result(result2);

          } else {

           // no records to display

            echo "Sorry, no records were found!";}

    } while ($myrow1 = mysql_fetch_array($result1));

 }

     else {

           echo "No Tables";

  }
    mysql_close();

include ('footer.php');
?> 

--- End Message ---
--- Begin Message ---
Yes. I just use the same method which Curt mentioned below and seem well.
But I met the problem a little bit complex that the sql used "group by".
Any idea?

 
Best regards,
Yang Shiqi
 
 
 

-----Original Message-----
From: Curt Zirzow [mailto:[EMAIL PROTECTED] 
Sent: Sunday, January 09, 2005 2:37 AM
To: php-general@lists.php.net
Subject: Re: [PHP] Re: Pagination Optimization

* Thus wrote Bruno B B Magalhes:
> Thanks for your help, in fact helped a lot... Now my function only 
> performs 2 queries, here it is:
> 
> =======================================
>       function fetch_paginated($query='',$page=1,$itens=20)
>       {
>               $this->query($query);

This here is going to be your big bottle neck. You're requiring
your database to fetch all the results. You're goal with
pagination is to know basically how many rows there are from the
result with minimal effort there are two ways i know that can
accomplish this rather efficiently:

  1. use the SQL_CALC_FOUND_ROWS option (i'm assuming this is
     mysql from your LIMIT statement).

     This way you can always use the LIMIT based on the page and
     items you want, and still know how many items would have
     been in the results. This will result with simply one query
     to the database.

  2. Modify your select statement to issue a count(*) of the
     query, to accomplish this automatically you can do something
     like:


     /* find the coulumns to replace with count(*) */
     $match = '/(select)\s+(.*?)\s+(from\s+.*)/i';

     /* row to start on; calculating what page they are
      * on to the actual row number */
     $start = (($page-1) * $items);

     /* replace fieldnames with count(*) */
     $replace = '$1 count(*) as qty $3';

     /* now replace the sqlqty and make the limit query  */
     $sql_qty = preg_replace($match, $replace, $query);
     $sql_qty = preg_replace('/(order|group) by.*/', '', $sql_qty);
     $sql_limit = $query . " limit $start, $items";


     And now you have $sql_qty that returns the number of total
     rows are available and $sql_limit which give you the actual
     results.


The first usage, is probably the fastest approach (i havn't done
any benchmarks), but it does limit to you with *only* mysql >= 4.0 and
and is not a common thing in other dbms (iirc).  

The second option seems like a lot of work but, i can guarantee  you
that it will be much faster than selecting all rows in a resultset
and figururing out what to do, expecially on the later pages.


Here is some code that uses method 2, keep in mind that the does
several things, like generating the navigation (which makes it more
complex). I should probably seperate the pagination method a bit
more.

http://zirzow.dyndns.org/html/php/code/paginate.php

And an example usage of it:
http://www.bigstockphoto.com/search.php


HTH,

Curt
-- 
Quoth the Raven, "Nevermore."

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
--- Begin Message ---
Hello,

I would like to add some debugging/info code to my pages. In test 
environment of course. Any ideas how to do this? I mean for example to print 
to a web page the line number when the script fails or something like that. 
It's a pain on the **s to hunt typo's by just reading the source over and 
over again.

Thaks a lot
-Will 

--- End Message ---
--- Begin Message ---
On 19/01/2005, at 5:36 PM, William Stokes wrote:

I would like to add some debugging/info code to my pages. In test
environment of course. Any ideas how to do this? I mean for example to print
to a web page the line number when the script fails or something like that.
It's a pain on the **s to hunt typo's by just reading the source over and
over again.

William,

I start by trying to programatically find out if I'm in a development or production environment and setting a constant DEV to true or false. For me, I development things on my desktop Mac, so the server and client are the same machine, and share the same IP address. So my check for DEV is if the client IP and server IP match.

<? define('DEV',($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR'])); ?>

So, this then gives a constant DEV to test to decide if I'm in the development or production environment, eg:

Next, I set-up PHP's built-in error reporting for both environments:

<?
if(DEV)
        {
        ini_set("error_reporting",E_ALL);
        ini_set("display_errors",1);
        ini_set("log_errors",0);
        }
else
        {
        ini_set("error_reporting",E_ALL ^ E_NOTICE);
        ini_set("display_errors",0);
        ini_set("log_errors",1);
        }
?>

In short, this logs most errors in production (not notices), and dumps them to the screen if we're in development. You'll already see that you get quite a lot of information from the errors (line numbers, reason for the error, etc), and I think this is what you're looking for.

But so far this only caters to PHP errors triggered by built-in functions and source. Smarter programmers will build in their own debugging lines, custom errors and notices to make the tracking down of bugs and quirks much much easier. For this, you can use trigger_error() <http://au2.php.net/trigger_error>.


That should be more than enough for the average PHP hack, but there is of course the option to write your own custom error handler to customise the look and feel of the error messages, send emails, log things to a database, etc.


It's all pretty powerful stuff, so read up!


--- Justin French, Indent.com.au [EMAIL PROTECTED] Web Application Development & Graphic Design

--- End Message ---
--- Begin Message ---
I'm trying to get the ini_set("error_reporting",E_ALL); work. No matter what 
kind of errors I write I just get blank screen when the script fails. Do I 
need to also echo the errors to screen? Or can the error reporting be 
disabled by the server admin (my adsl operator)? so that no errors are 
printed to screen.

I just set the error reporting at the beginning of the code like:

<?php
ini_set("error_reporting",E_ALL);
etc...

thanks
-Will

"Justin French" <[EMAIL PROTECTED]> kirjoitti 
viestissä:[EMAIL PROTECTED]
> On 19/01/2005, at 5:36 PM, William Stokes wrote:
>
>> I would like to add some debugging/info code to my pages. In test
>> environment of course. Any ideas how to do this? I mean for example to 
>> print
>> to a web page the line number when the script fails or something like 
>> that.
>> It's a pain on the **s to hunt typo's by just reading the source over and
>> over again.
>
> William,
>
> I start by trying to programatically find out if I'm in a development or 
> production environment and setting a constant DEV to true or false.  For 
> me, I development things on my desktop Mac, so the server and client are 
> the same machine, and share the same IP address.  So my check for DEV is 
> if the client IP and server IP match.
>
> <? define('DEV',($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR'])); ?>
>
> So, this then gives a constant DEV to test to decide if I'm in the 
> development or production environment, eg:
>
> Next, I set-up PHP's built-in error reporting for both environments:
>
> <?
> if(DEV)
> {
> ini_set("error_reporting",E_ALL);
> ini_set("display_errors",1);
> ini_set("log_errors",0);
> }
> else
> {
> ini_set("error_reporting",E_ALL ^ E_NOTICE);
> ini_set("display_errors",0);
> ini_set("log_errors",1);
> }
> ?>
>
> In short, this logs most errors in production (not notices), and dumps 
> them to the screen if we're in development.  You'll already see that you 
> get quite a lot of information from the errors (line numbers, reason for 
> the error, etc), and I think this is what you're looking for.
>
> But so far this only caters to PHP errors triggered by built-in functions 
> and source.  Smarter programmers will build in their own debugging lines, 
> custom errors and notices to make the tracking down of bugs and quirks 
> much much easier.  For this, you can use trigger_error() 
> <http://au2.php.net/trigger_error>.
>
>
> That should be more than enough for the average PHP hack, but there is of 
> course the option to write your own custom error handler to customise the 
> look and feel of the error messages, send emails, log things to a 
> database, etc.
>
> It's all pretty powerful stuff, so read up!
>
>
> ---
> Justin French, Indent.com.au
> [EMAIL PROTECTED]
> Web Application Development & Graphic Design 

--- End Message ---
--- Begin Message ---
Hello,


I have a for loop to create a HTML combo box that displays the 10 year values, starting from today's year and incrementing until 10 years it reached.


The output of the loop to the browser is weird.
If anyone has time, can you please let me know where I screwed up?


Here's my loop:

echo '<select name="expire_year" size="1">';
$numbOfYears = 10;
for ( $i = 0; $i < $numbOfYears; $i++ )
{
echo '<option value="'. $today_year + $i .'"';
if ( $_SESSION['createStudent_expireYear'] == $today_year + $i )
{
echo ' selected="selected"';
}
echo '>'. $today_year + $i .'</option>';
}
echo '</select>';



And here's the bung output:

  <select name="expire_year" size="1">
     0"0</option>
     1" selected="selected"1</option>
     2"2</option>
     3"3</option>
     4"4</option>
     5"5</option>
     6"6</option>
     7"7</option>
     8"8</option>
     9"9</option>
  </select>

And here's what I was expecting:

  <select name="expire_year" size="1">
     <option value="2005">2005</option>
     <option value="2006" selected="selected">2006</option>
     <option value="2007">2007</option>
     <option value="2008">2008</option>
     <option value="2009">2009</option>
     <option value="2010">2010</option>
     <option value="2011">2011</option>
     <option value="2012">2012</option>
     <option value="2013">2013</option>
     <option value="2014">2014</option>
  </select>


Thanks for your time and assistance

Tim
--- End Message ---
--- Begin Message ---
Hi,

   Store the sum into a variable (inside the loop) and print.I hope this will 
work.

Binoy
 

 
______________ ______________ ______________ ______________
Sent via the WebMail system at softwareassociates.co.uk


 
                   
---
Scanned by MessageExchange.net (07:10:44 SPITFIRE)

--- End Message ---
--- Begin Message ---
To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



> -----Original Message-----
> From: Tim Burgan [mailto:[EMAIL PROTECTED] 
> Sent: 19 January 2005 06:19
> 
> I have a for loop to create a HTML combo box that displays 
> the 10 year 
> values, starting from today's year and incrementing until 10 years it 
> reached.
> 
> The output of the loop to the browser is weird.
> If anyone has time, can you please let me know where I screwed up?
> 
> 
> Here's my loop:
> 
>    echo '<select name="expire_year" size="1">';
>   
>    $numbOfYears = 10;
>    for ( $i = 0; $i < $numbOfYears; $i++ )
>    {
>       echo '<option value="'. $today_year + $i .'"';

. And + have the same precedence, so their order of evaluation is decided by
their associativity; according to the table at
http://php.net/operators#language.operators.precedence, they are left
associative, so the above is interpreted as:

        echo (('<option value="'. $today_year) + $i) .'"';

>   
>       if ( $_SESSION['createStudent_expireYear'] == $today_year + $i )
>       {
>          echo ' selected="selected"';
>       }
>                               
>       echo '>'. $today_year + $i .'</option>';

And this, similarly, will be:

        echo (('>'. $today_year) + $i) .'</option>';

Some ways of dealing with this are:

(1) explicitly parenthesize the addition:

        echo '<option value="'. ($today_year + $i) .'"';

(2) use the multiple parameter feature of echo:

        echo '<option value="', $today_year + $i, '"';

(3) recast the loop to calculate the end year in advance, which avoids
having to repeatedly do the addition:

     $numbOfYears = 10;
     for ( $y = $today_year, $end_year = $today_year+$numbOfYears;
              $y < $endYear; $y++ )
     {
        echo '<option value="', $y, '"';

     ... etc.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services, JG125, James
Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS,
LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211

--- End Message ---
--- Begin Message ---
Does anyone know when the exception objects listed in
item #6 at http://www.php.net/~helly/php/ext/spl/ will be available
in base php5??

Thanks
--- End Message ---
--- Begin Message --- Gerard Samuel wrote:
Does anyone know when the exception objects listed in
item #6 at http://www.php.net/~helly/php/ext/spl/ will be available
in base php5??

Thanks
when you install the SPL extension :) I don't think it's planned for php5-source though
--- End Message ---

Reply via email to