php-general Digest 17 Dec 2011 05:57:43 -0000 Issue 7613

Topics (messages 316036 through 316049):

Re: Working on a Subsummary Report
        316036 by: Mike Mackintosh
        316037 by: DealTek
        316038 by: Bastien Koert
        316039 by: DealTek
        316040 by: Jim Lucas
        316041 by: Jim Lucas

Problems with CURL using an HTTP Proxy on PHP5
        316042 by: Francisco M. Marzoa Alonso

call_user_func_array and bind_result
        316043 by: Mari Masuda
        316047 by: David Harkness
        316049 by: Mari Masuda

dealing with this code $_POST['custom´]
        316044 by: Carlos Sura
        316045 by: Marc Guay
        316046 by: Govinda

Re: Preferred Syntax
        316048 by: Eric Butera

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

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


----------------------------------------------------------------------
--- Begin Message ---

On Dec 16, 2011, at 12:04, Dave <deal...@gmail.com> wrote:

> Hi all,
> 
> I need to create a year to date report with individual  Subsummary
> Report (ala filemaker / others?) headings for each month. So, I’m
> curious the best way to approach this for performance speed and
> flexibility etc.
> 
> -  I can do 1 sql query for the whole year’s data - or 12 individual
> queries... probably best to do 1 big year sql query - but I’m not sure
> how to split things up with loops etc.
> 
> each summary month heading will need to show:
> - total month count of records
> - special counts within each month
> - more stuff
> - also the record data
> 
> like:
> 
> JAN - total record count = 50 special count = 3
> 
> data record 1
> data record 2
> data record 3
> 
> FEB - total record count = 47 special count = 4
> 
> data record 1
> data record 2
> data record 3
> etc.
> 
> 
> Q: is it better to do 1 year find or 12 month queries - is 1 big find faster?
> 
> Q: with one year find - how do I setup 12 loops to simulate summaries?
> do i just loop through the full set and only display if record is in
> month 1 or 2 or whatever?
> 
> If there are any links describing this process - that would be great
> 
> any help would be appreciated...
> 
> 
> -- 
> Thanks - Dave
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

I would give consideration to the 'group by' function for your query, and loop 
through that.

--- End Message ---
--- Begin Message ---
> 
> 
> I would give consideration to the 'group by' function for your query, and 
> loop through that.

Thanks Mike - will do...

----------

*beginner question* - what would we call this type of query/display:

reporting or summary or ? I'm not sure what terms look up?

it would be similar to query all categories and their products sold in a year 
and display like:

summary heading1
CAT 1
 sub head 1------- Prod 1

----------- sales prod data 1
----------- sales prod data 2 etc

 sub head 2------- Prod 2

----------- sales prod data 3
----------- sales prod data 4 etc

summary heading2
CAT 2
 sub head 3------- Prod 3

----------- sales prod data 5
----------- sales prod data 6 etc


any other hints will help a lot...

--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-11]




--- End Message ---
--- Begin Message ---
On Fri, Dec 16, 2011 at 1:49 PM, DealTek <deal...@gmail.com> wrote:
>>
>>
>> I would give consideration to the 'group by' function for your query, and 
>> loop through that.
>
> Thanks Mike - will do...
>
> ----------
>
> *beginner question* - what would we call this type of query/display:
>
> reporting or summary or ? I'm not sure what terms look up?
>
> it would be similar to query all categories and their products sold in a year 
> and display like:
>
> summary heading1
> CAT 1
>  sub head 1------- Prod 1
>
> ----------- sales prod data 1
> ----------- sales prod data 2 etc
>
>  sub head 2------- Prod 2
>
> ----------- sales prod data 3
> ----------- sales prod data 4 etc
>
> summary heading2
> CAT 2
>  sub head 3------- Prod 3
>
> ----------- sales prod data 5
> ----------- sales prod data 6 etc
>
>
> any other hints will help a lot...
>
> --
> Thanks,
> Dave - DealTek
> deal...@gmail.com
> [db-11]
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

what about creating a master report that has the total summarized by
category and then offering a drill thru type structure to dig deeper
in certain areas?


-- 

Bastien

Cat, the other other white meat

--- End Message ---
--- Begin Message ---
> 
> 
> what about creating a master report that has the total summarized by
> category and then offering a drill thru type structure to dig deeper
> in certain areas?



- Hi Bastien,

That's a cool idea. In this case they want it on a longer single page so they 
may save or print all at once.

A while back - I remember doing a query for let's say just the unique product 
names - then as I looped though them in a table - I did a sub query for all 
records with that product name.... and did a 2nd loop in a second table to show 
them....  It ended up working like I wanted but I'm not sure if this was the 
best way to do it....


---------

also

also while I am testing I am trying to loop through the query 2 times

table 1

i tell it to show records if they meet criteria 1

then do again in table 2

table 2

i tell it to show records if they meet criteria 2

but the table 2 only shows 1 record

Q: ... like the query number row number? needs to be reset - but how?




----- like ...

like---- 2x ...

  <?php do { ?>
  
 <?php if ($row_get1['loc'] = 'back') { ?>
  
    <tr>
      <td><?php echo $row_get1['id']; ?></td>
      <td><?php echo $row_get1['loc']; ?></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    
   <?php  }; ?>
    
    <?php } while ($row_get1 = mysql_fetch_assoc($get1)); ?>



> 
> 
> -- 

--
Thanks,
Dave - DealTek
deal...@gmail.com
[db-11]




--- End Message ---
--- Begin Message ---
On 12/16/2011 9:04 AM, Dave wrote:
> Hi all,
> 
> I need to create a year to date report with individual  Subsummary
> Report (ala filemaker / others?) headings for each month. So, I’m
> curious the best way to approach this for performance speed and
> flexibility etc.
> 
> -  I can do 1 sql query for the whole year’s data - or 12 individual
> queries... probably best to do 1 big year sql query - but I’m not sure
> how to split things up with loops etc.
> 
> each summary month heading will need to show:
> - total month count of records
> - special counts within each month
> - more stuff
> - also the record data
> 
> like:
> 
> JAN - total record count = 50 special count = 3
> 
> data record 1
> data record 2
> data record 3
> 
> FEB - total record count = 47 special count = 4
> 
> data record 1
> data record 2
> data record 3
> etc.
> 
> 
> Q: is it better to do 1 year find or 12 month queries - is 1 big find faster?
> 
> Q: with one year find - how do I setup 12 loops to simulate summaries?
> do i just loop through the full set and only display if record is in
> month 1 or 2 or whatever?
> 
> If there are any links describing this process - that would be great
> 
> any help would be appreciated...
> 
> 

1) What does your db schema look like?
2) What SQL do you currently use?
3) What criteria do you want to use to sort the data?
4) Will the output be plaintext, html, etc?
5) Is this going to be used to import into another app, or display & printing?

-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

--- End Message ---
--- Begin Message ---
On 12/16/2011 12:56 PM, DealTek wrote:
>>
>>
>> what about creating a master report that has the total summarized by
>> category and then offering a drill thru type structure to dig deeper
>> in certain areas?
> 
> 
> 
> - Hi Bastien,
> 
> That's a cool idea. In this case they want it on a longer single page so they 
> may save or print all at once.
> 
> A while back - I remember doing a query for let's say just the unique product 
> names - then as I looped though them in a table - I did a sub query for all 
> records with that product name.... and did a 2nd loop in a second table to 
> show them....  It ended up working like I wanted but I'm not sure if this was 
> the best way to do it....
> 
> 
> ---------
> 
> also
> 
> also while I am testing I am trying to loop through the query 2 times
> 
> table 1
> 
> i tell it to show records if they meet criteria 1
> 
> then do again in table 2
> 
> table 2
> 
> i tell it to show records if they meet criteria 2
> 
> but the table 2 only shows 1 record
> 
> Q: ... like the query number row number? needs to be reset - but how?
> 
> 
> 
> 
> ----- like ...
> 
> like---- 2x ...
> 
>   <?php do { ?>
>   
>  <?php if ($row_get1['loc'] = 'back') { ?>
>   
>     <tr>
>       <td><?php echo $row_get1['id']; ?></td>
>       <td><?php echo $row_get1['loc']; ?></td>
>       <td>&nbsp;</td>
>       <td>&nbsp;</td>
>       <td>&nbsp;</td>
>       <td>&nbsp;</td>
>     </tr>
>     
>    <?php  }; ?>
>     
>     <?php } while ($row_get1 = mysql_fetch_assoc($get1)); ?>

for the above to work right, you will need to loop through the mysql result set
one time.  Placing all the results in to one large array.  Then you can loop
through the array as many times as needed.

What you will probably find is that you can sort all the data into the proper
order withing your SQL statement.  Then display the data looping through the
mysql result set once without having to create the additional array mentioned in
my first statement above.

> 
> 
> 
>>
>>
>> -- 
> 
> --
> Thanks,
> Dave - DealTek
> deal...@gmail.com
> [db-11]


-- 
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
http://www.bendsource.com/

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

The following code is failing and I do not find the cause (please, note
that checkurl value and CURLOPT_PROXY are NOT the real values I'm using,
of course):

        $ch = curl_init();
        $checkurl = 'http://mycheckhost.com/';
        curl_setopt ($ch, CURLOPT_TIMEOUT, 6000);
        curl_setopt ($ch, CURLOPT_URL, $checkurl);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt ($ch, CURLOPT_HEADER, false);
        curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
        curl_setopt ($ch, CURLOPT_PROXY, '1.2.3.4:8888' );

        $data = curl_exec($ch);
        $curl_info = curl_getinfo ($ch);

        if ( $data === false ) {
            $rtn = false;
        } else {
            $rtn = true;
        }

        return $rtn;

Every curl_exec returns false, and a var_dump of $curl_info looks as
follows:

array(21) {
  ["url"]=>
  string(25) "http://mycheckhost.com/";
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(99)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(1.843925)
  ["namelookup_time"]=>
  float(0.00018)
  ["connect_time"]=>
  float(0.333015)
  ["pretransfer_time"]=>
  float(0)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(-1)
  ["upload_content_length"]=>
  float(-1)
  ["starttransfer_time"]=>
  float(0)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
}


I know it is NOT a problem with the website, because if I comment out
the line curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, 1) disabling the use
of a proxy, it works fine.

I know the proxy is working fine, because if I use curl from command
line like:

curl -x 1.2.3.4:8888 http://mycheckhost.com/

It also WORKS fine.

So the problem may be or within the PHP5 curl implementation, or in my
own code, but I have not been able to find the problem with my code.

Any hints?

BTW:

$ php --version
PHP 5.3.6-13ubuntu3.3 with Suhosin-Patch (cli) (built: Dec 13 2011
18:37:10)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies



Thanks a lot in advance,

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

I am having trouble figuring out how to properly bind the results of a mysqli 
prepared statement using call_user_func_array.

I have an "AbstractModel" abstract class and within the class is a method 
called "load" that takes the primary key of the desired item and retrieves and 
loads the data from the database into the object.

abstract class AbstractModel {
        // lots of stuff omitted

        // this is just a rough draft so no error checking is implemented
        // $db is a subclass of mysqli
        // $primaryKeyAsTypedItem is an object that holds a type (int, float, 
string, etc.) and value
        public function load($db, $primaryKeyAsTypedItem) {
                $query = "SELECT * FROM " . $this->tableName . " WHERE " . 
$this->primaryKeyName . "=?"; 
                $ps = $db->prepare($query); 
                $type_string = $primaryKeyAsTypedItem->getTypeAbbreviation(); 
                $value = $db->escapeSql($primaryKeyAsTypedItem->getValue()); 
                $ps->bind_param($type_string, $value); 
                $ps->execute(); 
                $ps->store_result(); 
                $metadata = $ps->result_metadata()->fetch_fields(); 
                $params = array(); 
                foreach ($metadata as $object) { 
                        $params[$object->orgname] = null;
                } 
                call_user_func_array(array($ps, 'bind_result'), $params); 
                $ps->fetch(); 

                // see what is going on
                print "<pre>"; 
                var_dump($params); 
                print "</pre>";

                // more stuff omitted
        }
}

The problem I am having is that mysqli_stmt::bind_result expects a list of 
individual variables to which the results are bound, not just an array with the 
proper number of available indices.  I have done a lot of Googling and tried 
following the advice I found at [1], [2], [3], etc. but I was not successful in 
getting this to work.  I don't know how I can generically "expand" $params so 
that this will work for different tables with different fields.  Any pointers 
greatly appreciated.

[1] 
http://forums.devshed.com/php-development-5/mysqli-bind-result-to-return-array-568982.html
[2] 
http://www.charles-reace.com/blog/2009/04/28/mysqli-avoiding-bind_result-for-every-column/
[3] http://us.php.net/manual/en/mysqli-stmt.bind-result.php#102179

--- End Message ---
--- Begin Message ---
Each *value* in the array must be a reference to an existing variable--they
cannot be null or direct values. I didn't try this with bind_param(), but I
create a function that takes reference arguments and got it to work with
call_user_func_array():

    function foo(&$x, &$y) { $x *= 2; $y *= 3; }
    $x = 2;
    $y = 1;
    $params = array(&$x, &$y);
    call_user_func_array('foo', $params);
    var_dump($p);

    array(2) {
      [0]=>
      &int(4)
      [1]=>
      &int(3)
    }

Try changing the loop where you package up the $params array. I'm not
exactly sure what that part in your code is supposed to do, but I believe
that you have a result set where each column name is the name of a property
in the AbstractModel subclass.

    foreach ($metadata as $object) {
        $field = $object->orgname;
        $params[] = &$this->$field;
    }

If that's not the case, you can create a $values array to receive the
actual values from the result set and have the $params array hold
references to those values.

    $values = array();
    foreach ($metadata as $object) {
        $field = $object->orgname;
        $params[$field] = null;
        $params[] = &$values[$field];
    }

Peace,
David

--- End Message ---
--- Begin Message ---
On Dec 16, 2011, at 4:51 PM, David Harkness wrote:

> Each *value* in the array must be a reference to an existing variable--they 
> cannot be null or direct values. 

[snip]

Thank you very much for your explanation and example code.  I was missing the 
fact that the *values* in the array must be references.  I was thinking that 
call_user_func_array wanted a reference to the array itself where I wanted the 
values stored, but when I tried doing 

call_user_func_array(array($ps, 'bind_result'), &$params);

I got the following deprecation notice so I knew I was on the wrong track but 
didn't know where I was going wrong:
Warning: Call-time pass-by-reference has been deprecated in 
/Applications/apache/htdocs/hila/includes/class.AbstractModel.php on line 176

In the course of studying your example and trying to get it to work I also 
discovered a problem elsewhere in my code where the value of the primary key 
field in my MySQL database, defined as 

`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT

was being returned to PHP as a string instead of an int, and this was causing a 
type mismatch that ultimately resulted in all fields of the results array being 
populated with NULLs, which is why all of the examples on the pages I Googled 
did not appear to be working for me.  

In the end I changed the relevant part of my code to the following and thanks 
to your help it is now working:

$params = array();
$values = array();
foreach ($metadata as $object) {
        $params[] = &$values[$object->orgname];
}
call_user_func_array(array($ps, 'bind_result'), $params);
$ps->fetch();
   
print "<pre>";
var_dump($values);
print "</pre>";

Thanks again and happy holidays!

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

My problem is very simple,  I want to save the forwards emails in some
contact list so I'm saying what list in the following code:


} else {
$saving_list = $_POST['custom'] == 'FR' ? 148 : 147;


So, what I'm actually doing is:

} else {
$saving_list = $_POST['custom'] == 'FR' ? 152 : 148 : 147;


But, it doest make anything, also in Windows with Dreamweaver it says:
"syntax error" I'm not an advanced user of PHP, but I really don't get why
adding just another id (of a contact list) would be a "syntax error" if I
only use 148:147 it works fine!

Any help would be appreciated.

Regards
-- 
Carlos Sura.-
www.carlossura.com

--- End Message ---
--- Begin Message ---
> $saving_list = $_POST['custom'] == 'FR' ? 148 : 147;

Hi there, here's a quick translatation of this code that might help
you understand it better:

if ($_POST['custom'] == 'FR'){
  $saving_list = 148;
}
else{
  $saving_list = 147;
}

--- End Message ---
--- Begin Message ---
> "syntax error" I'm not an advanced user of PHP, but I really don't get why
> adding just another id (of a contact list) would be a "syntax error" if I
> only use 148:147 it works fine!

Hi Carlos, 

it is called "ternary operator".

Search for "ternary operator" in this page:

http://www.php.net/manual/en/language.operators.comparison.php

-Govinda

--- End Message ---
--- Begin Message ---
On Thu, Dec 15, 2011 at 5:46 PM, Ross McKay <ro...@zeta.org.au> wrote:
> Jim Lucas wrote:
>
>>I second this example, with one minor change, I would add '{' and '}' around
>>variables.
>>
>>echo <<<HTML
>><a style="text-align:left;size:14;font-weight:bold"
>>   href="/mypage.php/{$page_id}">{$page_name}</a><br>
>>HTML;
>>
>>This works for $variables, $objects, and variable functions calls.  But 
>>doesn't
>>work if you try to call functions directly (bummer).
>
> In fact, we are in agreement here :) I was just simplifying for the
> example at hand. And as for calling functions directly, also add
> constants :(
>
> However, it's easy enough to assign a constant to a variable and embed
> it in a HEREDOC, and also easy to wrap a function in a method,
> especially when your HEREDOC is within a method itself:
>
> define('MSG', 'My name is');
>
> class X {
>  function html($text) {
>    return htmlspecialchars($text);
>  }
>
>  function output($name) {
>    $msg = MSG;
>    echo <<<HTML
> <p>$msg {$this->html($name)}</p>
> HTML;
>  }
> }
>
> $x = new X();
> $x->output('silly "rockstar" name like <&>');
>
>>[...]
>>This is why I like heredoc syntax over pretty much everything else.
>
> Concur!
> --
> Ross McKay, Toronto, NSW Australia
> "Pay no attention to that man behind the curtain" - Wizard of Oz
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

To all the people who responded to this thread:
It is 2011 - please stop writing code like this.

To the OP:
I'm glad you're asking questions and realizing you're not happy with
your current abilities and suspect there's a better way.  I've read
the replies in this thread and feel a bit let down.  Use a templating
language - yes, I understand that is what php is for - but I won't go
into it.  You should not be echoing, printing, or any other method of
concatenating html dealing with escaping quotes inside your php logic
code.  Please separate your concerns.

Not sure what that means?  That's OK!  If you want to move forward,
look up how modern frameworks deal with this issue using their views
or template views.  You don't have to use a framework if you do not
want to, that's perfectly fine.  If it works, it works.  But in the
end, it the separation of logic and html is essential to code
maintenance.

--- End Message ---

Reply via email to