php-general Digest 9 Sep 2006 19:12:49 -0000 Issue 4338

Topics (messages 241523 through 241544):

Re: Using a variable to call another variable
        241523 by: tedd
        241529 by: Christopher Weldon
        241533 by: Robert Cummings

Newbie question about <?= ?>
        241524 by: Mike Borrelli
        241525 by: Dave Goodchild
        241526 by: Christopher Weldon
        241527 by: Satyam
        241528 by: Satyam

Re: if statement with or comparison (newbie)
        241530 by: Mark Charette
        241532 by: Stut
        241534 by: Robert Cummings
        241535 by: Mark Charette
        241537 by: Robert Cummings
        241539 by: Mark Charette
        241541 by: Stut
        241542 by: Robert Cummings
        241543 by: Robert Cummings

loop structure
        241531 by: Reinhart Viane
        241536 by: Robert Cummings

Re: PHP Access Violations
        241538 by: Christopher Watson
        241544 by: Jürgen Wind

Re: loop structure(SOLVED)
        241540 by: Reinhart Viane

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 ---
At 12:10 AM -0400 9/9/06, Robert Cummings wrote:
On Sat, 2006-09-09 at 12:57 +0900, Dave M G wrote:
 PHP List,

 I have a list of variables:

 $001
 $002
 $003
 $004

 And what I'd like to do is have a function which will select and return
 one of them. Something like:

 public function returnVar($n)
 {
 return $(somehow n is made to reference the name of the variable);
 }

 And then in later scripts I can call anyone of the variables by saying

 returnVar(001)

 > Or something like that.

 I've been scratching my head on how to do this for a while. I thought
 the answer might lie somewhere in call_user_func(), but even if it is I
 can't determine how.

 Any advice would be much appreciated.

<?php

function easy_peasy( $name )
{
    $foo1 = 1;
    $foo2 = 2;
    $foo3 = 3;

    return $$name;
}

echo easy_peasy( 'foo2' )."\n";

?>

Cheers,
Rob.


 Or something like that.  <----  :-)

$easy_peasyier = array("foo1" => 1, "foo2" => 2, "foo3" => 3);

echo($easy_peasyier['foo1']);

tedd
--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--- End Message ---
--- Begin Message ---
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tedd wrote:
> At 12:10 AM -0400 9/9/06, Robert Cummings wrote:
>> On Sat, 2006-09-09 at 12:57 +0900, Dave M G wrote:
>>>  PHP List,
>>>
>>>  I have a list of variables:
>>>
>>>  $001
>>>  $002
>>>  $003
>>>  $004
>>>
>>>  And what I'd like to do is have a function which will select and return
>>>  one of them. Something like:
>>>
>>>  public function returnVar($n)
>>>  {
>>>  return $(somehow n is made to reference the name of the variable);
>>>  }
>>>
>>>  And then in later scripts I can call anyone of the variables by saying
>>>
>>>  returnVar(001)
>>>
>>  > Or something like that.
>>>
>>>  I've been scratching my head on how to do this for a while. I thought
>>>  the answer might lie somewhere in call_user_func(), but even if it is I
>>>  can't determine how.
>>>
>>>  Any advice would be much appreciated.
>>
>> <?php
>>
>> function easy_peasy( $name )
>> {
>>     $foo1 = 1;
>>     $foo2 = 2;
>>     $foo3 = 3;
>>
>>     return $$name;
>> }
>>
>> echo easy_peasy( 'foo2' )."\n";
>>
>> ?>
>>
>> Cheers,
>> Rob.
> 
> 
>>  Or something like that.  <----  :-)
> 
> $easy_peasyier = array("foo1" => 1, "foo2" => 2, "foo3" => 3);
> 
> echo($easy_peasyier['foo1']);
> 
> tedd

class myClass {
        private var $_001;
        private var $_002;
        private var $_003;

        public function access_var($var) {
                return $this->$$var;
        }
}

$cs = new myClass;
$cs->access_var('_001');

// Done

BTW, please make certain that you aren't really naming your variables as
$001, $002 and $003. Those are bad variable names, as PHP only allows
for variables beginning with letters and '_' characters (what I did above).

- --
Christopher Weldon, ZCE
President & CEO
Cerberus Interactive, Inc.
[EMAIL PROTECTED]
979.739.5874
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFAsfXZxvk7JEXkbERAiYkAJ9misO/pDJYEpJM3iPFF5T3GVdKGwCgpwFB
ae17qOdSZL2DJj+VA6rUqDc=
=dRAJ
-----END PGP SIGNATURE-----

--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 08:44 -0400, tedd wrote:
> At 12:10 AM -0400 9/9/06, Robert Cummings wrote:
> >On Sat, 2006-09-09 at 12:57 +0900, Dave M G wrote:
> >>  PHP List,
> >>
> >>  I have a list of variables:
> >>
> >>  $001
> >>  $002
> >>  $003
> >>  $004
> >>
> >>  And what I'd like to do is have a function which will select and return
> >>  one of them. Something like:
> >>
> >>  public function returnVar($n)
> >>  {
> >>  return $(somehow n is made to reference the name of the variable);
> >>  }
> >>
> >>  And then in later scripts I can call anyone of the variables by saying
> >>
> >>  returnVar(001)
> >>
> >  > Or something like that.
> >>
> >>  I've been scratching my head on how to do this for a while. I thought
> >>  the answer might lie somewhere in call_user_func(), but even if it is I
> >>  can't determine how.
> >>
> >>  Any advice would be much appreciated.
> >
> ><?php
> >
> >function easy_peasy( $name )
> >{
> >     $foo1 = 1;
> >     $foo2 = 2;
> >     $foo3 = 3;
> >
> >     return $$name;
> >}
> >
> >echo easy_peasy( 'foo2' )."\n";
> >
> >?>
> >
> >Cheers,
> >Rob.
> 
> 
> >  Or something like that.  <----  :-)
> 
> $easy_peasyier = array("foo1" => 1, "foo2" => 2, "foo3" => 3);
> 
> echo($easy_peasyier['foo1']);

That's zero marks on any exam I ever wrote. You didn't properly read the
business requirements that specified the need for a function ;) Also,
the version you showed isn't shared in any way, it has local scope, so
unless it's defined in global scope and you're working in global scope
(since I don't see a global declaration) then it's not very
accessible :))

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Good day,

While I've been using php for more than a little while now, I've never
understood why the use of the "<?= ...?>" short tag is noted "to be
avoided".

Or rather, I understand that there's an option to disable it, and that's
why it's noted in this way, but I don't understand why it's disabled? 
What's gained by writing <?php echo some_function(); ?> over <?=
some_function(); ?>

Thanks in advance.

Cheers,
Mike

--- End Message ---
--- Begin Message ---
To be gained: less typing
To be lost: short_open_tag may be disabled in some environments, making your
code less portable.

If you are only ever going to run your code in one environment and can
enable short_open_tag (or if it is already on), there's no issue.






--
http://www.web-buddha.co.uk
http://www.projectkarma.co.uk

--- End Message ---
--- Begin Message ---
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mike Borrelli wrote:
> Good day,
> 
> While I've been using php for more than a little while now, I've never
> understood why the use of the "<?= ...?>" short tag is noted "to be
> avoided".
> 
> Or rather, I understand that there's an option to disable it, and that's
> why it's noted in this way, but I don't understand why it's disabled? 
> What's gained by writing <?php echo some_function(); ?> over <?=
> some_function(); ?>
> 
> Thanks in advance.
> 
> Cheers,
> Mike
> 

- From my understanding, there are multiple reasons. One is that depending
on where your application is being hosted, the server it is on may have
turned off the short tags option (and you can't get your hosting
provider to change this). Thus, <?= ...?> would simply be written to the
page as that.

Additionally, through some of my training courses, another reason behind
it is that if you use PHP to generate any XML documents, XML uses <? ?>
syntax, and it's better to turn off the short tags in your PHP config so
that PHP doesn't attempt to interpret those tags as PHP code.

- --
Christopher Weldon
President, Lead Systems Administrator
Cerberus Interactive, Inc.
[EMAIL PROTECTED]
979.739.5874
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFAsRnZxvk7JEXkbERAiW+AJ9POOf3U1K6TiRVhSFC6ok7VjDm2ACfel8U
/6gFKYPPFYa5iyEtWdksZ0w=
=C1fK
-----END PGP SIGNATURE-----

--- End Message ---
--- Begin Message ---
All forms of short tags make your php file not XML compliant.

Notice that the <?php tag which starts every script is a valid XML construct, it is what is called a Processing Instruction (PI) and admits within it absolutely anything except for a ?> which ends the PI. A PI you will be familiar with is the <?xml PI. XML expects something to identify who is to process that PI, thus, there can be no 'anonymous' PI such as a plain <?. Actually, I have heard that it is possible (and have been done) to use more than one interpreter at once on the same document, using the proper processing tags for each language. How you configure that, don't ask me, I wouldn't know.

Any short tag then, whether a <? or a <?=,  is not valid XML.

Whether that is important or not, I can't tell, but it is the only reason I know why short tags might be considered inapropriate and disabled.

Satyam


----- Original Message ----- From: "Mike Borrelli" <[EMAIL PROTECTED]>
To: <php-general@lists.php.net>
Sent: Saturday, September 09, 2006 3:19 PM
Subject: [PHP] Newbie question about <?= ?>


Good day,

While I've been using php for more than a little while now, I've never
understood why the use of the "<?= ...?>" short tag is noted "to be
avoided".

Or rather, I understand that there's an option to disable it, and that's
why it's noted in this way, but I don't understand why it's disabled?
What's gained by writing <?php echo some_function(); ?> over <?=
some_function(); ?>

Thanks in advance.

Cheers,
Mike

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




--- End Message ---
--- Begin Message ---
All forms of short tags make your php file not XML compliant.

Notice that the <?php tag which starts every script is a valid XML construct, it is what is called a Processing Instruction (PI) and admits within it absolutely anything except for a ?> which ends the PI. A PI you will be familiar with is the <?xml PI. XML expects something to identify who is to process that PI, thus, there can be no 'anonymous' PI such as a plain <?. Actually, I have heard that it is possible (and have been done) to use more than one interpreter at once on the same document, using the proper processing tags for each language. How you configure that, don't ask me, I wouldn't know.

Any short tag then, whether a <? or a <?=,  is not valid XML.

Whether that is important or not, I can't tell, but it is the only reason I know why short tags might be considered inapropriate and disabled.

Satyam


----- Original Message ----- From: "Mike Borrelli" <[EMAIL PROTECTED]>
To: <php-general@lists.php.net>
Sent: Saturday, September 09, 2006 3:19 PM
Subject: [PHP] Newbie question about <?= ?>


Good day,

While I've been using php for more than a little while now, I've never
understood why the use of the "<?= ...?>" short tag is noted "to be
avoided".

Or rather, I understand that there's an option to disable it, and that's
why it's noted in this way, but I don't understand why it's disabled?
What's gained by writing <?php echo some_function(); ?> over <?=
some_function(); ?>

Thanks in advance.

Cheers,
Mike

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




--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
On Fri, 2006-09-08 at 18:38 -0400, tedd wrote:
At 5:03 PM -0400 9/8/06, JD wrote:
In all of the answers given thus far, no one mentioned that the use of $_REQUEST has a security issue with regard to where the $_REQUEST originated.

$_REQUEST is an array consisting of $_GET, $_POST and $_COOKIE values and as such, you don't know where the data came from and that might be important.

So, wouldn't it be better to recommend that the poster use $_GET, $_POST, or $_COOKIE instead of $_REQUEST?

Nope, not inherently less secure. If you are properly cleaning and
validating your data (as every good program should) then it doesn't
matter whether you pull from $_GET, $_POST, or $_REQUEST. The only time
it's bad is if you make assumptions about the value received -- AND YOU
SHOULD NEVER ASSUME YOU HAVE CLEAN DATA FROM AN OUTSIDE SOURCE!!
However, looking at it from a 'knowing early the data is tainted' perspective, not from a 'validating and cleaning perspective', if you have coded that (for instance) a variable is set via COOKIE, then only looking for that variable set via COOKIE will eliminate its being tainted by being set via GET or REQUEST. It doesn't eliminate any need for validation or cleaning, but reduces (naive) attempts to set via incorrect means. That is not possible via REQUEST. Personally, I like to toss out possibilities of bad data via simple means as early in the chain as possible.
--- End Message ---
--- Begin Message ---
Mark Charette wrote:
However, looking at it from a 'knowing early the data is tainted' perspective, not from a 'validating and cleaning perspective', if you have coded that (for instance) a variable is set via COOKIE, then only looking for that variable set via COOKIE will eliminate its being tainted by being set via GET or REQUEST. It doesn't eliminate any need for validation or cleaning, but reduces (naive) attempts to set via incorrect means. That is not possible via REQUEST. Personally, I like to toss out possibilities of bad data via simple means as early in the chain as possible.

If I understood that right it's a shocking naive statement for any developer to make. While I agree with what you're saying, you're implying a bad attitude to handling data from untrusted sources.

It shouldn't matter how difficult it is for the user to send you dodgy data... if its source is outside your control you need to handle it accordingly no matter how unlikely you think it is that it will be changed between you setting it and getting it back.

Data from any of the superglobals should be treated the same - trust none of them!!

-Stut

--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 10:21 -0400, Mark Charette wrote:
> Robert Cummings wrote:
> > On Fri, 2006-09-08 at 18:38 -0400, tedd wrote:
> >   
> >> At 5:03 PM -0400 9/8/06, JD wrote:
> >>     
> >> In all of the answers given thus far, no one mentioned that the use 
> >> of $_REQUEST has a security issue with regard to where the $_REQUEST 
> >> originated.
> >>
> >> $_REQUEST is an array consisting of $_GET, $_POST and $_COOKIE values 
> >> and as such, you don't know where the data came from and that might 
> >> be important.
> >>
> >> So, wouldn't it be better to recommend that the poster use $_GET, 
> >> $_POST, or $_COOKIE instead of $_REQUEST?
> >>     
> >
> > Nope, not inherently less secure. If you are properly cleaning and
> > validating your data (as every good program should) then it doesn't
> > matter whether you pull from $_GET, $_POST, or $_REQUEST. The only time
> > it's bad is if you make assumptions about the value received -- AND YOU
> > SHOULD NEVER ASSUME YOU HAVE CLEAN DATA FROM AN OUTSIDE SOURCE!!
> >   
> However, looking at it from a 'knowing early the data is tainted' 
> perspective, not from a 'validating and cleaning perspective', if you 
> have coded that (for instance) a variable is set via COOKIE, then only 
> looking for that variable set via COOKIE will eliminate its being 
> tainted by being set via GET or REQUEST. It doesn't eliminate any need 
> for validation or cleaning, but reduces (naive) attempts to set via 
> incorrect means. That is not possible via REQUEST. Personally, I like to 
> toss out possibilities of bad data via simple means as early in the 
> chain as possible.

Any malevolently intentioned hacker will have little properly screwing
around with cookie data. I'm pretty sure browsers allow editing the
cookie values via the cookie browser. And if not, a quick PHP script is
just as simple to create that mucks with cookie data.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Stut wrote:
Mark Charette wrote:
However, looking at it from a 'knowing early the data is tainted' perspective, not from a 'validating and cleaning perspective', if you have coded that (for instance) a variable is set via COOKIE, then only looking for that variable set via COOKIE will eliminate its being tainted by being set via GET or REQUEST. It doesn't eliminate any need for validation or cleaning, but reduces (naive) attempts to set via incorrect means. That is not possible via REQUEST. Personally, I like to toss out possibilities of bad data via simple means as early in the chain as possible.

If I understood that right it's a shocking naive statement for any developer to make. While I agree with what you're saying, you're implying a bad attitude to handling data from untrusted sources.
I am being neither shocking or naive. Why is early discarding of data because it comes in the wrong area shocking? If I were looking for a variable set via a COOKIE, why would I look for the variable set via GET? As I so explicitly said above "It doesn't eliminate any need for validation or cleaning, but reduces (naive) attempts to set via incorrect means." My CPU resources are valuable; writing code that checks whether a variable is set via the correct method is no harder ($_COOKIE vs. $_REQUEST) and throws out trivially spurious data. No more, no less. The same checks still need apply after that, but my CPU won't be burdened by the script kiddies. No more, no less. The data just won't appear.
--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 11:30 -0400, Mark Charette wrote:
> Stut wrote:
> > Mark Charette wrote:
> >> However, looking at it from a 'knowing early the data is tainted' 
> >> perspective, not from a 'validating and cleaning perspective', if you 
> >> have coded that (for instance) a variable is set via COOKIE, then 
> >> only looking for that variable set via COOKIE will eliminate its 
> >> being tainted by being set via GET or REQUEST. It doesn't eliminate 
> >> any need for validation or cleaning, but reduces (naive) attempts to 
> >> set via incorrect means. That is not possible via REQUEST. 
> >> Personally, I like to toss out possibilities of bad data via simple 
> >> means as early in the chain as possible.
> >
> > If I understood that right it's a shocking naive statement for any 
> > developer to make. While I agree with what you're saying, you're 
> > implying a bad attitude to handling data from untrusted sources.

> I am being neither shocking or naive. Why is early discarding of data 
> because it comes in the wrong area shocking?

That's your last line, I think he's commenting on the rest of your
comment. Questionable data is questionable data, it doesn't matter from
whence you clean it. If you haven't cleaned it your still going to get
screwed no matter how much you rely on it being difficult to manipulate
by a site visitor.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
On Sat, 2006-09-09 at 11:30 -0400, Mark Charette wrote:
Stut wrote:
Mark Charette wrote:
However, looking at it from a 'knowing early the data is tainted' perspective, not from a 'validating and cleaning perspective', if you have coded that (for instance) a variable is set via COOKIE, then only looking for that variable set via COOKIE will eliminate its being tainted by being set via GET or REQUEST. It doesn't eliminate any need for validation or cleaning, but reduces (naive) attempts to set via incorrect means. That is not possible via REQUEST. Personally, I like to toss out possibilities of bad data via simple means as early in the chain as possible.
If I understood that right it's a shocking naive statement for any developer to make. While I agree with what you're saying, you're implying a bad attitude to handling data from untrusted sources.

I am being neither shocking or naive. Why is early discarding of data because it comes in the wrong area shocking?

That's your last line, I think he's commenting on the rest of your
comment. Questionable data is questionable data, it doesn't matter from
whence you clean it. If you haven't cleaned it your still going to get
screwed no matter how much you rely on it being difficult to manipulate
by a site visitor.
Where am I being unclear, then? "reduces (naive) attempts to set via incorrect means." doesn't say 'eliminate serious attempts'. I would think my statement "It doesn't eliminate any need for validation or cleaning, " covers the remaining scenarios. Indeed, determining the source of data is one of the essential steps in validation. The one of the rules is 'discard even valid data if it comes from an untrusted source" - and data coming from an _incorrect_ source is, by definition, untrusted even if if you wish to expend the effort to prove it valid.

And I'll wager a brew no one here has ever done a formal, mathematically rigorous proof of a validation routine except as a class project. As a senior member of the software QC department in a major industrial company, I generally find more errors and omissions in validation routines during code reviews and ethical hacks than anywhere else.
--- End Message ---
--- Begin Message ---
Mark Charette wrote:
And I'll wager a brew no one here has ever done a formal, mathematically rigorous proof of a validation routine except as a class project. As a senior member of the software QC department in a major industrial company, I generally find more errors and omissions in validation routines during code reviews and ethical hacks than anywhere else.

Ok, let's not turn this into a pissing contest. I admit I misread the initial email and read more into it than it said. However, since this is a mailing list with a lot of beginners on it we usually make a point to be very clear on issues like validation and it was worth reiterating the point that no data that comes from the user should not be trusted no matter how hard it is for the user to change.

Your point is valid, but in the great scheme of things it's more important to enforce the importance of validation than performance. I felt your post was confusing so I'm sure others did too.

'Nuff pissing.

-Stut

--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 12:12 -0400, Mark Charette wrote:
>
> As a senior member of the software QC department in a major industrial 
> company, I generally find more errors and omissions in validation 
> routines during code reviews and ethical hacks than anywhere else.

http://en.wikipedia.org/wiki/Appeal_to_authority

Where's Tedd, he's got the latin to go with the above link >:)

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 17:27 +0100, Stut wrote:
> Mark Charette wrote:
> > And I'll wager a brew no one here has ever done a formal, mathematically 
> > rigorous proof of a validation routine except as a class project. As a 
> > senior member of the software QC department in a major industrial 
> > company, I generally find more errors and omissions in validation 
> > routines during code reviews and ethical hacks than anywhere else.
> 
> Ok, let's not turn this into a pissing contest. I admit I misread the 
> initial email and read more into it than it said. However, since this is 
> a mailing list with a lot of beginners on it we usually make a point to 
> be very clear on issues like validation and it was worth reiterating the 
> point that no data that comes from the user should not be trusted no 
> matter how hard it is for the user to change.
> 
> Your point is valid, but in the great scheme of things it's more 
> important to enforce the importance of validation than performance. I 
> felt your post was confusing so I'm sure others did too.
> 
> 'Nuff pissing.

Awwww, what about this bonfire I was putting out?? Admittedly there's a
strong odour hanging in the air now, but we don't want forest fires do
we? *heheh*

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
I've been experimenting some time now but i can't get it right.
I have a database in which I have a table with a list of photo_url

Table: photos
Id      photo_url
1       photos/boeket_s40.jpg
2       photos/boeket_k12.jpg
3       photos/boeket_z23.jpg
...


I get this out of the database with this query:
$sqlphoto="select * from photos where photo_type='$category'";
$exephoto=mysql_query($sqlphoto) or die (mysql_error());

Now I need a loop so that the photos are put into a table:

<table width="420" border="0" cellpadding="0" cellspacing="0">
        <!--DWLayoutTable-->

        <tr>
          <td width="17" height="110"
valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          <td width="120" valign="top">PICTURE HERE</td>
          <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          <td width="120" valign="top"> PICTURE HERE </td>
          <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
          <td width="120" valign="top"> PICTURE HERE </td>
          <td width="13"><!--DWLayoutEmptyCell-->&nbsp;</td>
            <td width="4"><!--DWLayoutEmptyCell-->&nbsp;</td>
        </tr>
        <tr>
          <td height="15"></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </table>

The first <tr> must be looped as long as there are photos in the array (so
something like #lines in array/3, with ceil())
In each <tr> there are 3 pictures in it's <td> tags:
 so maybe something like 
 for ($i=1;$i<3;$i++){
 <td width="120" valign="top">PICTURE HERE</td>
 <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
 } 

I have tried several thing and I'm able to create the correct amount of rows
and the loop for the 3 cells in the row.
Unfortunately it always only shows the first picture from the array in each
of those cells.

Can someone help me out? THX

--- End Message ---
--- Begin Message ---
On Sat, 2006-09-09 at 16:49 +0200, Reinhart Viane wrote:
> I've been experimenting some time now but i can't get it right.
> I have a database in which I have a table with a list of photo_url
> 
> Table: photos
> Id    photo_url
> 1     photos/boeket_s40.jpg
> 2     photos/boeket_k12.jpg
> 3     photos/boeket_z23.jpg
> ...
> 
> 
> I get this out of the database with this query:
> $sqlphoto="select * from photos where photo_type='$category'";
> $exephoto=mysql_query($sqlphoto) or die (mysql_error());
> 
> Now I need a loop so that the photos are put into a table:
> 
> <table width="420" border="0" cellpadding="0" cellspacing="0">
>         <!--DWLayoutTable-->
> 
>         <tr>
>           <td width="17" height="110"
> valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
>           <td width="120" valign="top">PICTURE HERE</td>
>           <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
>           <td width="120" valign="top"> PICTURE HERE </td>
>           <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
>           <td width="120" valign="top"> PICTURE HERE </td>
>           <td width="13"><!--DWLayoutEmptyCell-->&nbsp;</td>
>           <td width="4"><!--DWLayoutEmptyCell-->&nbsp;</td>
>         </tr>
>         <tr>
>           <td height="15"></td>
>           <td></td>
>           <td></td>
>           <td></td>
>           <td></td>
>           <td></td>
>           <td></td>
>         </tr>
>       </table>
> 
> The first <tr> must be looped as long as there are photos in the array (so
> something like #lines in array/3, with ceil())
> In each <tr> there are 3 pictures in it's <td> tags:
>  so maybe something like 
>  for ($i=1;$i<3;$i++){
>  <td width="120" valign="top">PICTURE HERE</td>
>  <td width="13" valign="top"><!--DWLayoutEmptyCell-->&nbsp;</td>
>  } 
> 
> I have tried several thing and I'm able to create the correct amount of rows
> and the loop for the 3 cells in the row.
> Unfortunately it always only shows the first picture from the array in each
> of those cells.
> 
> Can someone help me out? THX

Untested... ... ...

<?php

if( ($result = mysql_query( $query )) === false )
{
    // do something other than die you lazy ass programmers.
}
else
{
    $hits = mysql_num_rows( $result );
    $cols = 3;
    $rows = ceil( $hits / $cols );

    for( $i = 0; $i < $rows; $i++ )
    {
        for( $j = 0; $j < $cols; $j++ )
        {
            $picture = '';
            if( ($item = mysql_fetch_assoc( $result )) !== false )
            {
                echo '<td width="120" valign="top">'
                    .$item['picture']
                    .'</td>'
                    .'<td width="13" valign="top">'
                    .'<!--DWLayoutEmptyCell-->&nbsp;'
                    .'</td>';
            }
        }
    }
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
Hi Wolf,

Please correct me if I'm wrong, but neither of these tools from the
MySQL team can SSH tunnel.  I need that.

-Chris

On 9/8/06, Wolf <[EMAIL PROTECTED]> wrote:
MySQL Query Tool
MySQL Admin Tool

Both free, both work flawlessly w/ MySQL  and both maintained by MySQL

Wolf

Christopher Watson wrote:
> Following up on this now...
>
> I have successfully installed Apache 2.0.59 (Win32), and configured
> successfully for PHP 5.1.6 and MySQL 5.0.24.  The app is running fine,
> but I ran a few manipulation queries from the SQLyog interface and
> Apache did crash.  I'll have try to reproduce the problem again in
> order to get the exception data.  This is looking more and more like a
> SQLyog problem.  Maybe I should try another MySQL admin client.
>
> -Christopher
>
> On 9/6/06, Christopher Watson <[EMAIL PROTECTED]> wrote:
>> Spoke too soon.  After a reboot, I had only IE and Homesite open,
>> making changes to PHP code and running the app, and it hit an access
>> violation.  So SQLyog ain't it.
>>
>> -Chris
>>
>> On 9/6/06, Christopher Watson <[EMAIL PROTECTED]> wrote:
>> > Thanks for the input, Jon.  I'll get to the Apache and IIS restart
>> > suggestions soon.
>> >
>> > Meanwhile, I think I have a semi-repeatable recipe for getting the
>> > access violation to happen.  As far as I can tell, everything is cool
>> > until I open up SQLyog and do some sort of database manipulation
>> > within it.  Almost immediately after that, switching back to the
>> > browser and flying through the app a little more brings on the
>> > violation.  So far, my testing is indicating to me that the violation
>> > does not occur without SQLyog having done some work in the database.
>> >
>> > -Chris
>>
>


--- End Message ---
--- Begin Message ---
did you try mysql 5.0.24a? there where some problems with 5.0.24
(segfaults...)
-- 
View this message in context: 
http://www.nabble.com/PHP-Access-Violations-tf2222023.html#a6227018
Sent from the PHP - General forum at Nabble.com.

--- End Message ---
--- Begin Message ---
Thx, it works like a charm
I think the mysql_fetch_assoc was the thing I was looking for.

-----Oorspronkelijk bericht-----
Van: Robert Cummings [mailto:[EMAIL PROTECTED] 
Verzonden: zaterdag 9 september 2006 17:36
Aan: [EMAIL PROTECTED]
CC: php-general@lists.php.net
Onderwerp: Re: [PHP] loop structure


Untested... ... ...

<?php

if( ($result = mysql_query( $query )) === false )
{
    // do something other than die you lazy ass programmers.
}
else
{
    $hits = mysql_num_rows( $result );
    $cols = 3;
    $rows = ceil( $hits / $cols );

    for( $i = 0; $i < $rows; $i++ )
    {
        for( $j = 0; $j < $cols; $j++ )
        {
            $picture = '';
            if( ($item = mysql_fetch_assoc( $result )) !== false )
            {
                echo '<td width="120" valign="top">'
                    .$item['picture']
                    .'</td>'
                    .'<td width="13" valign="top">'
                    .'<!--DWLayoutEmptyCell-->&nbsp;'
                    .'</td>';
            }
        }
    }
}

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---

Reply via email to