Re: foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tuesday, October 16, 2012 09:46:26 AM you wrote:
> > Here's what I ended up with after you gave me the advise:
> > $result = mysql_query("SELECT * FROM items");
> > 
> >   $rows = array();
> >   while($row = mysql_fetch_array($result))
> >   
> >    $rows[] = $row;
> >   
> >   foreach($rows as $row){
> >   $product = $row['product'];
> >   $price = $row['price'];
> >   
> >     echo "$product ";
> >     echo "$price ";
> >     
> >     
> >     $justright = 0;
> >     $toohigh = 5; //I was going to use this to check if the price was too
> > 
> > high
> > 
> > 				just so I could use an elseif in the
> 
> exercise, but
> 
> > I realized that
> > 
> > 				it would only work if the if() evaluated to
> 
> false,
> 
> > which would be
> > 
> > 				impossible. Ahhh pizz on it, it was fun
> 
> anyway! :-
> 
> > )
> > 
> >     if($justright <= $price){
> >     
> >      echo "Not bad. I'll buy it.<br />";
> >      
> >       } else
> >      
> >      echo "Too expensive I'm going home LOL ";
> >   
> >   }
> > 
> > It's a dumb script that makes no sense but I had a blast doing this. When
> > things start coming together like this, it gets so gratifying. :-)
> > 
> >  --
> > 
> > David M.
> 
> David,
> 
> Just putting this out there, but the use of a foreach() loop here, is
> redundant...
> You are putting your query results into an array, and then looping through
> them after with the foreach(), instead of just using the while loop to loop
> through them initially... so you're doing the same thing, twice, just using
> the foreach() after the while.
> 
> One thing I was always told when I was learning c++ (my teacher was anal,
> and always forced us to try and be more efficient), using >= uses more cpu
> cycles, than > or <, so when you're checking 0 <= 0.1 true, false, you could
> exchange your check to be 0 > 0.1 false, else true...

This is what I like to hear. It's stuff like this this that I haven't found in 
a text book. Even when Bastian said "$row is a pointer....." Ah Yes! That 
helped tremendously.

> 
> Also, setting the variable $product and $price, with the value from the
> database $row['product'], would be less cycles than to just echo the
> $row['product']...

Hehehehe who said I wanted this exercise to be easy? LMBO
> 
> That is just some $0.02...  I wouldn't personally create more code, just to
> "try" something out... use the right function for the job, write less code
> than needed (sometimes, a little more code for readability is better
> though), and most importantly... have fun.

I appreciate your feedback and it's conversations like this that help the 
most.
> 
> One thing I do, is my coding and bracing style is something that Tedd
> Sperling doesn't like (there have been many discussions about bracing
> styles),  I keep my braces all in line, and always use them in my if()s...
> ie:
> 	If($yourmom == $hot)
> 	{
> 		Echo "MILF!";
> 	}
> 	Else
> 	{
> 		Echo "Pass.";
> 	}
> 
> And I do this for readability, so I can see if I forgot a brace somewhere,
> and also, I always know that there are braces (with a 4space indentation, or
> tab stops set at 4 space)

I have Kate set up to create auto brackets so I'll never forget to close one 
and I have lines on the left hand side that connect each open bracket to it's 
corresponding close bracket. Any open end lines means I'm missing something.
> 
> 1 more point, doing multiline comments, use /*  insert comment here */  and
> not just //, and with that, I use inline comments with #,  but that is just
> me.

That long comment I made about the variable I didn't use was actually typed in 
the e-mail in my reply to Bastian and wasn't in the code. If it was in the 
code I would have used /* */
> 
> Overall though, I am glad to see you're learning, and having fun doing so...

Thanks. The whole purpose of me doing this was to get to play with foreach() 
and I've been down with while for a while.. LMBO it was just too easy to use 
while and I was trying to force myself to keep thinking.

When Bastian pointed out that $row was a pointer, just that little bit of info 
changed the whole course of how I was thinking. I was thinking $row was an 
array with all the results in it and with his little bit of info and knowing I 
wanted to play around with foreach() I just stuck to whatever came up and this 
is what the result was... :-)

Alot of times I need to be reminded. Like hey! that's a CONSTANT not a 
$variable!!

-- 
David M.


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux