php-general Digest 1 Sep 2005 08:32:50 -0000 Issue 3657

Topics (messages 221665 through 221677):

Re: Nested IFs Problem
        221665 by: Jordan Miller
        221666 by: Jim Moseby
        221668 by: Eric Gorr
        221670 by: Satyam

Re: [SPAM] - [PHP] Nested IFs Problem - Bayesian Filter detected spam
        221667 by: Justin Francis
        221669 by: Albert Padley

ID based on position?
        221671 by: Gustav Wiberg
        221672 by: Jasper Bryant-Greene
        221673 by: Jason Davidson
        221675 by: Miles Thompson

Re: ZCE Reccommendations
        221674 by: hitek
        221676 by: Greg Donald

Re: String format problem
        221677 by: Mark Rees

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:
        [email protected]


----------------------------------------------------------------------
--- Begin Message --- why not rewrite it to be more concise... i can't see a problem at the moment. are you sure you can do a "<" comparison operator on the '12:00' and '2005-10-02' string?? Maybe if you are using 24 hr format you could just get rid of the ":" on both sides of the operator to have the "<" properly evaluated... do the same for the date comparison.

just try each if statement individually. then, when you find the problem, rewrite like this:

if ((putFirstExpressionHere) && (putSecondExpressionHere) && (putThirdExpressionHere)) {
    // success
} else {
    // failure
}

Jordan



On Aug 31, 2005, at 3:05 PM, Albert Padley wrote:

if ($row['date'] < '2005-10-02') {
            if ($row['time'] < '12:00') {
                if ($row['field'] == 'P5' ) {

                    echo "<td class=\"tabletextbluebg\">Success";

}
}
}

else {

        echo "<td class=\"tabletextred\">Failed";
}


--- End Message ---
--- Begin Message ---
> 
> I have the following nested ifs:
> 
> if ($row['date'] < '2005-10-02') {
>              if ($row['time'] < '12:00') {
>                  if ($row['field'] == 'P5' ) {
> 
>                      echo "<td class=\"tabletextbluebg\">Success";
> 
> }
> }
> }
> 
> else {
> 
>          echo "<td class=\"tabletextred\">Failed";
> }
> </td>
> 
> Whenever the 3 if statements are true, I always get the correct  
> "Success" to echo. However, if any or all of the if statements are  
> false, I never get "Failed" to echo.
> 
> I know it's something simple, but I just can't see it at the moment.
> 
> TIA
> 
> Albert Padley

Just shooting from the hip here, you will never get the "Failed" echo unless
the FIRST if is false.

JM

--- End Message ---
--- Begin Message ---
Albert Padley wrote:
Whenever the 3 if statements are true, I always get the correct "Success" to echo. However, if any or all of the if statements are false, I never get "Failed" to echo.

I know it's something simple, but I just can't see it at the moment.

The code is doing exactly what you told it to do.

To make it do what you what you seem to want it to do, get rid of the nested IFs and place all three tests within a single IF.


--
== Eric Gorr =============================== http://www.ericgorr.net ===
"Government is not reason, it is not eloquence, it is force; like fire,
a troublesome servant and a fearful master." - George Washington
== Insults, like violence, are the last refuge of the incompetent... ===

--- End Message ---
--- Begin Message ---
"Jim Moseby" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>>
>> I have the following nested ifs:
>>
>> if ($row['date'] < '2005-10-02') {
>>              if ($row['time'] < '12:00') {
>>                  if ($row['field'] == 'P5' ) {
>>
>>                      echo "<td class=\"tabletextbluebg\">Success";
>>
>> }

// This, if there was anything, would be executed if the date and the time 
condition is true and the field is false

>> }

// this would be executed if the date condition is true and the time is 
false, regardless of what the field condition is
>> }
>>
>> else {
>>

// this would be executed only if the date condition is false, regardless of 
the other conditions

>>          echo "<td class=\"tabletextred\">Failed";
>> }
>> </td>
>>


So, you better put all the conditions in a single if, joined by 'and' or &&:

if ($row['date'] < '2005-10-02' && $row['time'] < '12:00' && $row['field'] 
== 'P5' ) {

That would do what you want

--- End Message ---
--- Begin Message ---
Albert Padley wrote:

I have the following nested ifs:

if ($row['date'] < '2005-10-02') {
            if ($row['time'] < '12:00') {
                if ($row['field'] == 'P5' ) {

                    echo "<td class=\"tabletextbluebg\">Success";

}
}
}

else {

        echo "<td class=\"tabletextred\">Failed";
}
</td>

Whenever the 3 if statements are true, I always get the correct "Success" to echo. However, if any or all of the if statements are false, I never get "Failed" to echo.

Once the first if statement is evaluated as true, the else statement will never be executed -- regardless of the outcome of the two nested ifs. I am not sure what you are trying to accomplish here, but perhaps all three of your if conditions should be combined into one if condition ANDed (&&) together? Or maybe the if...elseif...else structure is what you need.

Hope this helps.

Justin



I know it's something simple, but I just can't see it at the moment.

TIA

Albert Padley


--- End Message ---
--- Begin Message ---
Justin & Jordan,

Thanks. The && was what I needed.

Albert Padley


On Aug 31, 2005, at 2:27 PM, Justin Francis wrote:

Albert Padley wrote:


I have the following nested ifs:

if ($row['date'] < '2005-10-02') {
            if ($row['time'] < '12:00') {
                if ($row['field'] == 'P5' ) {

                    echo "<td class=\"tabletextbluebg\">Success";

}
}
}

else {

        echo "<td class=\"tabletextred\">Failed";
}
</td>

Whenever the 3 if statements are true, I always get the correct "Success" to echo. However, if any or all of the if statements are false, I never get "Failed" to echo.


Once the first if statement is evaluated as true, the else statement will never be executed -- regardless of the outcome of the two nested ifs. I am not sure what you are trying to accomplish here, but perhaps all three of your if conditions should be combined into one if condition ANDed (&&) together? Or maybe the if...elseif...else structure is what you need.

Hope this helps.

Justin




I know it's something simple, but I just can't see it at the moment.

TIA

Albert Padley







--- End Message ---
--- Begin Message ---
Hi there!

Is there any function in PHP that gives an ID from a MySQL-db based on which position the record has in the table?


Let's say, there's a table like this:

1. Record1 ID 33
2. Record2 ID 76
3. Record3 ID 100


If I know position 2, I want to get ID 76. Is the only way to loop through the recordset?

/G
@varupiraten.se

--- End Message ---
--- Begin Message ---
Gustav Wiberg wrote:
Hi there!

Is there any function in PHP that gives an ID from a MySQL-db based on which position the record has in the table?

Let's say, there's a table like this:

1. Record1 ID 33
2. Record2 ID 76
3. Record3 ID 100

If I know position 2, I want to get ID 76. Is the only way to loop through the recordset?

Why don't you use MySQL's LIMIT clause? Example:

SELECT * FROM myTable LIMIT 1,1

will retrieve the second row from myTable. You could combine that with WHERE clauses or whatever. Note that the LIMIT syntax is LIMIT [rowcount] or LIMIT [offset],[rowcount] and the offset starts at 0.

--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/

If you find my advice useful, please consider donating to a poor
student! You can choose whatever amount you think my advice was
worth to you. http://tinyurl.com/7oa5s

--- End Message ---
--- Begin Message ---
You could LIMIT your query to the record number you are looking for, and 
grab the last element in the array from your result set. But this is a 
serious hack, and I am really wondering why you need to do what your asking, 
it seems (without knowing more) that you are tackling the problem in the 
wrong direction,

Jason

On 8/31/05, Gustav Wiberg <[EMAIL PROTECTED]> wrote:
> 
> Hi there!
> 
> Is there any function in PHP that gives an ID from a MySQL-db based on 
> which
> position the record has in the table?
> 
> 
> Let's say, there's a table like this:
> 
> 1. Record1 ID 33
> 2. Record2 ID 76
> 3. Record3 ID 100
> 
> 
> If I know position 2, I want to get ID 76. Is the only way to loop through
> the recordset?
> 
> /G
> @varupiraten.se <http://varupiraten.se>
> 
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
>

--- End Message ---
--- Begin Message ---

Auugh!! Why would you want to do this? You're flying in the face of relational database theory and practice. Position of a record in the table is, or should be irrelevant.

What if you have twenty thousand records, or two hundred, and the 45th record in the table is deleted? Fetching an ID from anything beyond that record, based on the order of insertion (position), is instantly broken.

Please rethink what you want to do, and if you are not familiar with relational databases read some of the excellent tutorials available on the 'Net about them and their design. It's pretty straightforward, common sense stuff -- but you can back yourself into an awkward corner.

Regards - Miles


At 07:54 PM 8/31/2005, Gustav Wiberg wrote:
Hi there!

Is there any function in PHP that gives an ID from a MySQL-db based on which position the record has in the table?


Let's say, there's a table like this:

1. Record1 ID 33
2. Record2 ID 76
3. Record3 ID 100


If I know position 2, I want to get ID 76. Is the only way to loop through the recordset?

/G
@varupiraten.se

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


--- End Message ---
--- Begin Message ---
Chris Shiflett wrote:

hitek wrote:

I'm taking my ZCE exam soon and would like general advice on
what to study up on.


This is a good resource:

http://zend.com/store/education/certification/zend-php-certification-objectives.php

You picked a good month to take the exam, since it's $125 this month (normally $200).

Chris

Thanks for the tip. For the record, I passed :)

--- End Message ---
--- Begin Message ---
On 8/31/05, hitek <[EMAIL PROTECTED]> wrote:
> For the record, I passed :)


Congrats.


-- 
Greg Donald
Zend Certified Engineer
MySQL Core Certification
http://destiney.com/

--- End Message ---
--- Begin Message ---
> In fact, this is a poor example since the difference gets larger with
longer
> string and more arguments.  When you use dots, the interpreter has to
> actually concatenate the string, looking for memory to do so and freeing
it
> up afterwards.  This takes time.  With commas, each argument is sent to
the
> output stream as soon as it is found, no further processing is needed in
> between.
>

I have been wondering about this topic for a few months now, so thanks for
this fascinating explanation. Is this the only difference between using .
and , as concatenation operators>


> Then the single vs. double quotes:
>
> echo 'uno ' , ' dos ' , ' tres ': 0.94
> echo "uno " , " dos " , " tres ": 6.76
>
> Notice that when variables are involved, the difference in between echoing
> with arguments separated with commas and separated with dots is more than
9
> times faster for the commas.    Using double quotes with variable
expansion
> is almost 4 times slower than the commas, but is still faster than
> concatenating them externaly with dots.   Using heredoc-style strings is
not
> so bad compared to double quotes.

Never heard of heredoc before. What is it for? I have read
http://uk.php.net/types.string

and can only imagine that it is for laying out complex or long strings more
clearly

>
> So, if you are sending out the rule would be:
> Use echo, not print.   Separate arguments with commas.
>
> Now, if you are not using echo, for example, concatenating to a variable,
> the best is to use variable expansion inside double quoted or heredoc
> strings.   Concatenating with dots is more than twice as slow.
>
> Satyam

--- End Message ---

Reply via email to