Re: [PHP] mysql_num_rows()
On Tuesday 22 February 2011, "Gary" wrote: > Can someone tell me why this is not working? I do not get an error > message, the results are called and echo'd to screen, the count does not > work, I get a 0 for a result... Are you sure that the table is called `counties` and not `countries`? :) > > > > $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") > or die(mysql_error()); > > if ( isset($_POST['submit']) ) { > for($i=1; $i<=$_POST['counties']; $i++) { > if ( isset($_POST["county$i"] ) ) { > echo "You have chosen ". $_POST["county$i"]." "; > } > } > } > > $county_total=mysql_num_rows($result); > > while($row = mysql_fetch_array($result)) > {$i++; > echo $row['name']; > } > $county_total=mysql_num_rows($result); > echo "$county_total"; > > echo 'You Have ' . "$county_total" . ' Counties '; > > ?> Best regards, -- Daniel Molina Wegener System Programmer & Web Developer Phone: +56 (2) 979-0277 | Blog: http://coder.cl/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] How to write code: how wrong am I?
[snip] The point that I stress on the page is that first you close an element, then you fill it's contents. The page has examples in HTML and PHP. I would like to know what the experienced programmers here think about this. Am I going about it wrong? [/snip] It is an interesting concept Dotan, what is the goal? Who is the page targeted to? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
It's an interesting idea (a different take on coding best practices) but I find the PHP example to be laborious and time consuming with little benefit. If I'm typing an IF statement, I hope to god I know what the condition is before I start typing it. Creating the if/else structure first and then filling in the conditions after is a lot of cursor-shuffling that could be lived without. 2 cents Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_num_rows()
Hello Gary, Please wrap $i in braces in your variable variable: if ( isset($_POST["county{$i}"] ) ) { echo "You have chosen ". $_POST["county{$i}"]." "; -- With best regards from Ukraine, Andre Skype: Francophile My blog: http://oire.org/menelion (mostly in Russian) Twitter: http://twitter.com/m_elensule Facebook: http://facebook.com/menelion Original message From: Gary To: php-general@lists.php.net Date created: , 7:40:48 AM Subject: [PHP] mysql_num_rows() Can someone tell me why this is not working? I do not get an error message, the results are called and echo'd to screen, the count does not work, I get a 0 for a result... $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") or die(mysql_error()); if ( isset($_POST['submit']) ) { for($i=1; $i<=$_POST['counties']; $i++) { if ( isset($_POST["county$i"] ) ) { echo "You have chosen ". $_POST["county$i"]." "; } } } $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) {$i++; echo $row['name']; } $county_total=mysql_num_rows($result); echo "$county_total"; echo 'You Have ' . "$county_total" . ' Counties '; ?> -- Gary __ Information from ESET Smart Security, version of virus signature database 5894 (20110221) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: mysql_num_rows()
On 22/02/11 05:40, Gary wrote: Can someone tell me why this is not working? I do not get an error message, the results are called and echo'd to screen, the count does not work, I get a 0 for a result... $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") or die(mysql_error()); if ( isset($_POST['submit']) ) { for($i=1; $i<=$_POST['counties']; $i++) { if ( isset($_POST["county$i"] ) ) { echo "You have chosen ". $_POST["county$i"].""; } } } $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) {$i++; echo $row['name']; } $county_total=mysql_num_rows($result); echo "$county_total"; echo 'You Have ' . "$county_total" . ' Counties '; ?> The first thing I see is that you do $county_total=mysql_num_rows($result) twice. Now I'm not to sure, but it is possible that looping over mysql_fetch_array($result) moves an array pointer so the count is not valid after the operation... that's a long shot. But since you are looping over the database records anyway, why not just count them as you go? Isn't $i the number of counties after all the looping? -- Peter Ford, Developer phone: 01580 89 fax: 01580 893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 06:29 AM, Dotan Cohen wrote: I wrote a short page on how to actually type of code that one writes, it can be found here: http://dotancohen.com/howto/write_code.html The point that I stress on the page is that first you close an element, then you fill it's contents. The page has examples in HTML and PHP. I would like to know what the experienced programmers here think about this. Am I going about it wrong? I'm more likely to do this stuff for HTML than PHP. However, I do sometimes lay out the structure of some conditionals/functions before fleshing them out so that I can do some early testing... but I always fill the conditional as I'm doing this. In case that's not clear... the code flows from first character to last character as I type all of the conditionals (no jumping around). Later I fill in the associated activity for the conditional. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 6:29 AM, Dotan Cohen wrote: > I wrote a short page on how to actually type of code that one writes, > it can be found here: > http://dotancohen.com/howto/write_code.html > > The point that I stress on the page is that first you close an > element, then you fill it's contents. The page has examples in HTML > and PHP. I would like to know what the experienced programmers here > think about this. Am I going about it wrong? I'll be the first to tell you that I'm not a great programmer, so my take may not be worth a lot. However, this is pretty close to how I write. Not exactly, but close. I also always label open and closing brackets on everything, unless the brackets are less than three lines apart. For instance, I might find myself writing: if($foo == 'bar') { # Begin foo=bar test # If foo=bar, do a lot of things. } # End foo=bar test Then I go back and fill in the conditional stuff. I've found two sets of benefits to doing things this way. 1) I don't forget to close the brackets later, and I know they're indented properly. 2) The comments mean that if I get to the beginning of a section and want to skip to the end, I can just search for whatever comes after "Begin", and know that the next instance will be the end of that section. I also tend to document my code in comments before I start writing actual code. I frequently find that that helps keep me on track, rather than getting distracted by random new features that occur to me after I start. It also means that when the time comes to document what I did, most of it is already there... that's saved me a few times. (I tend to wind up with about a 2:1 ratio of comments to code, which at least means I can figure out later what I was thinking when I wrote that horrible mess.) So I can't say whether it's worthwhile practice for a good programmer who writes on a regular basis: for someone like me, who throws together an occasional script to save time later, or a personal web-site that needs a database backend, it can save a lot of aggravation. -Alex -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_num_rows()
Thanks for your reply "Deva" wrote in message news:aanlktimvycqsi5nejd1lxxaqbqsb+q_onhoc7omgj...@mail.gmail.com... >I tried your script locally. Its working for me. > > On Tue, Feb 22, 2011 at 11:10 AM, Gary wrote: > >> Can someone tell me why this is not working? I do not get an error >> message, >> the results are called and echo'd to screen, the count does not work, I >> get >> a 0 for a result... >> >> >> >> $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") >> or >> die(mysql_error()); >> >> if ( isset($_POST['submit']) ) { >> for($i=1; $i<=$_POST['counties']; $i++) { >> if ( isset($_POST["county$i"] ) ) { >> echo "You have chosen ". $_POST["county$i"]." "; >>} >>} >> } >> >> $county_total=mysql_num_rows($result); >> >> while($row = mysql_fetch_array($result)) >> {$i++; >>echo $row['name']; >> } >> $county_total=mysql_num_rows($result); >> echo "$county_total"; >> >> echo 'You Have ' . "$county_total" . ' Counties '; >> >> ?> >> -- >> Gary Thanks for your reply. I'm curious, how did you try it locally without a database? I am still getting either a 0 or a 1 for a result. Gary __ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] mysql_num_rows()
"Daniel Molina Wegener" wrote in message news:201102220747.11...@coder.cl... > On Tuesday 22 February 2011, > "Gary" wrote: > >> Can someone tell me why this is not working? I do not get an error >> message, the results are called and echo'd to screen, the count does not >> work, I get a 0 for a result... > > Are you sure that the table is called `counties` and not `countries`? > > :) > >> >> >> >> $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") >> or die(mysql_error()); >> >> if ( isset($_POST['submit']) ) { >> for($i=1; $i<=$_POST['counties']; $i++) { >> if ( isset($_POST["county$i"] ) ) { >> echo "You have chosen ". $_POST["county$i"]." "; >> } >> } >> } >> >> $county_total=mysql_num_rows($result); >> >> while($row = mysql_fetch_array($result)) >> {$i++; >> echo $row['name']; >> } >> $county_total=mysql_num_rows($result); >> echo "$county_total"; >> >> echo 'You Have ' . "$county_total" . ' Counties '; >> >> ?> > > Best regards, > -- > Daniel Molina Wegener > System Programmer & Web Developer > Phone: +56 (2) 979-0277 | Blog: http://coder.cl/ Daniel Thank you for your reply, yes the database is named counties, working and returning results. Gary __ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:11, Jay Blanchard wrote: > It is an interesting concept Dotan, what is the goal? Who is the page > targeted to? > The goal is to have every open and close bracket matched, and not have to worry about what is still open. The page was specifically written for a friend who is learning HTML, but I plan on presenting it to my C# and Java classmates who I think would benefit from it as well. I choose to display the concepts with PHP instead of C# or Java as I feel that there would be more PHP learners online to benefit from the concept than C# or Java beginners. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: mysql_num_rows()
"Pete Ford" wrote in message news:a4.c0.39221.b3ca3...@pb1.pair.com... > On 22/02/11 05:40, Gary wrote: >> Can someone tell me why this is not working? I do not get an error >> message, >> the results are called and echo'd to screen, the count does not work, I >> get >> a 0 for a result... >> >> >> >> $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") >> or >> die(mysql_error()); >> >> if ( isset($_POST['submit']) ) { >> for($i=1; $i<=$_POST['counties']; $i++) { >> if ( isset($_POST["county$i"] ) ) { >> echo "You have chosen ". $_POST["county$i"].""; >> } >> } >> } >> >> $county_total=mysql_num_rows($result); >> >> while($row = mysql_fetch_array($result)) >> {$i++; >> echo $row['name']; >> } >> $county_total=mysql_num_rows($result); >> echo "$county_total"; >> >> echo 'You Have ' . "$county_total" . ' Counties '; >> >> ?> > > The first thing I see is that you do > $county_total=mysql_num_rows($result) > twice. Now I'm not to sure, but it is possible that looping over > mysql_fetch_array($result) moves an array pointer so the count is not > valid after the operation... that's a long shot. > > But since you are looping over the database records anyway, why not just > count them as you go? Isn't $i the number of counties after all the > looping? > > > -- > Peter Ford, Developer phone: 01580 89 fax: 01580 > 893399 > Justcroft International Ltd. > www.justcroft.com > Justcroft House, High Street, Staplehurst, Kent TN12 0AH United > Kingdom > Registered in England and Wales: 2297906 > Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 > 1XS Peter Thank you for your reply. I did notice that I had the $county_total=mysql_num_rows($result) twice. I had moved and removed it, always getting either a 0 or 1 result. I put up another test page with $result = mysql_query("SELECT * FROM `counties` ") or die(mysql_error()); $tot=mysql_num_rows($result); echo "$tot"; ?> And it worked fine. I have tried count() as well as mysql_num_rows() but am getting the same result. Could you explain what you mean by count them as I go? Again, Thank you for your reply. Gary __ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 15:00, Andy McKenzie wrote: > I'll be the first to tell you that I'm not a great programmer, so my > take may not be worth a lot. However, this is pretty close to how I > write. Not exactly, but close. I also always label open and closing > brackets on everything, unless the brackets are less than three lines > apart. For instance, I might find myself writing: > > if($foo == 'bar') > { # Begin foo=bar test > # If foo=bar, do a lot of things. > > } # End foo=bar test > > Then I go back and fill in the conditional stuff. I also do something similar, I debated writing about that as well: class someClass{ function someFunction() { if (true) { // Some Code } } // end someFunction } // end someClass I don't strictly subscribe to a three-line limit, but whatever feels reasonable at the time. Often that depends on the complexity of surrounding code. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: mysql_num_rows()
On 22/02/11 13:59, Gary wrote: "Pete Ford" wrote in message news:a4.c0.39221.b3ca3...@pb1.pair.com... On 22/02/11 05:40, Gary wrote: Can someone tell me why this is not working? I do not get an error message, the results are called and echo'd to screen, the count does not work, I get a 0 for a result... $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") or die(mysql_error()); if ( isset($_POST['submit']) ) { for($i=1; $i<=$_POST['counties']; $i++) { if ( isset($_POST["county$i"] ) ) { echo "You have chosen ". $_POST["county$i"].""; } } } $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) {$i++; echo $row['name']; } $county_total=mysql_num_rows($result); echo "$county_total"; echo 'You Have ' . "$county_total" . ' Counties '; ?> The first thing I see is that you do $county_total=mysql_num_rows($result) twice. Now I'm not to sure, but it is possible that looping over mysql_fetch_array($result) moves an array pointer so the count is not valid after the operation... that's a long shot. But since you are looping over the database records anyway, why not just count them as you go? Isn't $i the number of counties after all the looping? -- Peter Ford, Developer phone: 01580 89 fax: 01580 893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS Peter Thank you for your reply. I did notice that I had the $county_total=mysql_num_rows($result) twice. I had moved and removed it, always getting either a 0 or 1 result. I put up another test page with $result = mysql_query("SELECT * FROM `counties` ") or die(mysql_error()); $tot=mysql_num_rows($result); echo "$tot"; ?> And it worked fine. I have tried count() as well as mysql_num_rows() but am getting the same result. Could you explain what you mean by count them as I go? Again, Thank you for your reply. Gary __ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com Well, Lets go back to your original code and cut out the decoration: $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) { echo $row['name']; } echo "$county_total"; That code should do pretty much what you want... But, because you only show the number of records AFTER the loop, you could do: $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") $county_total = 0; while($row = mysql_fetch_array($result)) { echo $row['name']; $county_total++; } echo "$county_total"; -- Peter Ford, Developer phone: 01580 89 fax: 01580 893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 08:58 AM, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 14:11, Jay Blanchard wrote: It is an interesting concept Dotan, what is the goal? Who is the page targeted to? The goal is to have every open and close bracket matched, and not have to worry about what is still open. This is why I use the bracing style I use in PHP. A cursory glance of the code will show you where you've forgotten a brace. Syntax highlighting is also nice. HTML can trip you up with missing or superfluous tags, but a quick run through the W3C validator will catch those errors. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 11-02-22 09:06 AM, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 14:58, Robert Cummings wrote: I'm more likely to do this stuff for HTML than PHP. However, I do sometimes lay out the structure of some conditionals/functions before fleshing them out so that I can do some early testing... but I always fill the conditional as I'm doing this. In case that's not clear... the code flows from first character to last character as I type all of the conditionals (no jumping around). Later I fill in the associated activity for the conditional. Thanks for the insight. I could see where that is a good compromise. Like this I suppose: if (something) if (something) { } if (something) { // Code here } If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:15, Marc Guay wrote: > It's an interesting idea (a different take on coding best practices) > but I find the PHP example to be laborious and time consuming with > little benefit. If I'm typing an IF statement, I hope to god I know > what the condition is before I start typing it. Creating the if/else > structure first and then filling in the conditions after is a lot of > cursor-shuffling that could be lived without. > > If the if statement is simple, then I do agree with you. But once there are nestled functions or conditionals, I like to have the layout already ready. Consider: if ( someFunction( someCheck( srrtolower( $_POST["variable"] ) ) ) ){ // Some Code } Now, after the $_POST variable, how many close parentheses does the code need? This does trip up beginners, especially those who rightfully learn outside an IDE. Similar situations occur for something like this (unindented to not overflow email line character limit): function someFunction(){ if($something==true){ while($someCondition){ switch($someVariable){ // Some Code } } } } Writing that is fine, but if the user goes in to add another while or if, he will get lost in the sea of parenthesis. Therefore, I always close elements before filling them in. It keeps the mind's state machine saner! -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 14:58, Robert Cummings wrote: > I'm more likely to do this stuff for HTML than PHP. However, I do sometimes > lay out the structure of some conditionals/functions before fleshing them > out so that I can do some early testing... but I always fill the conditional > as I'm doing this. In case that's not clear... the code flows from first > character to last character as I type all of the conditionals (no jumping > around). Later I fill in the associated activity for the conditional. > Thanks for the insight. I could see where that is a good compromise. Like this I suppose: if (something) if (something) { } if (something) { // Code here } -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: mysql_num_rows()
"Pete Ford" wrote in message news:76.48.39221.054c3...@pb1.pair.com... > On 22/02/11 13:59, Gary wrote: >> "Pete Ford" wrote in message >> news:a4.c0.39221.b3ca3...@pb1.pair.com... >>> On 22/02/11 05:40, Gary wrote: >>>> Can someone tell me why this is not working? I do not get an error >>>> message, >>>> the results are called and echo'd to screen, the count does not work, I >>>> get >>>> a 0 for a result... >>>> >>>> >>>> >>>> $result = mysql_query("SELECT * FROM `counties` WHERE name = >>>> 'checked'") >>>> or >>>> die(mysql_error()); >>>> >>>> if ( isset($_POST['submit']) ) { >>>> for($i=1; $i<=$_POST['counties']; $i++) { >>>> if ( isset($_POST["county$i"] ) ) { >>>> echo "You have chosen ". $_POST["county$i"].""; >>>> } >>>> } >>>> } >>>> >>>> $county_total=mysql_num_rows($result); >>>> >>>> while($row = mysql_fetch_array($result)) >>>> {$i++; >>>> echo $row['name']; >>>> } >>>> $county_total=mysql_num_rows($result); >>>> echo "$county_total"; >>>> >>>> echo 'You Have ' . "$county_total" . ' Counties '; >>>> >>>> ?> >>> >>> The first thing I see is that you do >>>$county_total=mysql_num_rows($result) >>> twice. Now I'm not to sure, but it is possible that looping over >>> mysql_fetch_array($result) moves an array pointer so the count is not >>> valid after the operation... that's a long shot. >>> >>> But since you are looping over the database records anyway, why not just >>> count them as you go? Isn't $i the number of counties after all the >>> looping? >>> >>> >>> -- >>> Peter Ford, Developer phone: 01580 89 fax: 01580 >>> 893399 >>> Justcroft International Ltd. >>> www.justcroft.com >>> Justcroft House, High Street, Staplehurst, Kent TN12 0AH United >>> Kingdom >>> Registered in England and Wales: 2297906 >>> Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 >>> 1XS >> >> >> Peter >> >> Thank you for your reply. >> >> I did notice that I had the $county_total=mysql_num_rows($result) twice. >> I >> had moved and removed it, always getting either a 0 or 1 result. >> >> I put up another test page with >> >> $result = mysql_query("SELECT * FROM `counties` ") or die(mysql_error()); >> >> $tot=mysql_num_rows($result); >> echo "$tot"; >> >> ?> >> >> And it worked fine. >> >> I have tried count() as well as mysql_num_rows() but am getting the same >> result. >> >> Could you explain what you mean by count them as I go? >> >> Again, Thank you for your reply. >> >> Gary >> >> >> >> __ Information from ESET Smart Security, version of virus >> signature database 5895 (20110222) __ >> >> The message was checked by ESET Smart Security. >> >> http://www.eset.com >> >> >> >> > > Well, > > Lets go back to your original code and cut out the decoration: > > $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") > $county_total=mysql_num_rows($result); > while($row = mysql_fetch_array($result)) > { > echo $row['name']; > } > echo "$county_total"; > > That code should do pretty much what you want... > > But, because you only show the number of records AFTER the loop, you could > do: > > $result = mysql_query("SELECT * FROM `counties` WHERE name = 'checked'") > $county_total = 0; > while($row = mysql_fetch_array($result)) > { > echo $row['name']; > $county_total++; > } > echo "$county_total"; > Peter Thank you for your suggestion...btw "cut out the decoration" LOL I'm sorry to report it did not work. I tried various configurations of it but to no avail. Any other suggestions? Again, thank you for your help. Gary __ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Regex pattern for preg_match_all
Le 19/02/2011 0:23, Tommy Pham a écrit : @Simon, Thanks for explaining about the [^href]. I need to read up more about greediness. I thought I understood it but guess not. @Peter, I tried your pattern but it didn't capture all of my new test cases. Also, it captures the single/double quotes in addition to the fragments inside the href. I couldn't figure out how to modify your pattern to exclude the ', ", and URL fragment from group 1 matches. Below is the new pattern with the new sample test cases that I got it to work. The new pattern failed only 1 of the non-compliant. $html =<Images http://video.search.yahoo.com/video"; data-b="http://www.yahoo.com";>Video http://local.yahoo.com/results"; data-b="http://www.yahoo.com";>Local http://shopping.yahoo.com/search"; data-b="http://www.yahoo.com";>Shopping http://tools.search.yahoo.com/about/forsearchers.html";>More HTML; $pattern = '%]*?href\s*=\s*["\']?([^"\'#>]*)["\']?\s?[^>]*>(.*?)%ims'; preg_match_all($pattern, $html, $matches); Thanks for your time, Tommy Hi Tommy, This is why you shouldn't mix regexes and HTML/XML, especially when you are not sure that you are working with valid/consistent html. A great/fun answer has been posted on StackOverflow about this at http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 You could easily break any regular expressions solution by adding some valid comments, see example here : http://stackoverflow.com/questions/1357357/regexp-to-add-attribute-in-any-xml-tags/1357393#1357393 You really should consider using a XML parser instead for this kind of job. Here is a simple sample that matches your example : $html = $oTidy->repairString($html,array("clean" => true, "drop-proprietary-attributes" => true)); unset($oTidy); $matches = get_links($html); function get_links($html) { // Create a new DOM Document to hold our webpage structure $xml = new DOMDocument(); // Load the url's contents into the DOM $xml->loadHTML($html); // Empty array to hold all links to return $links = array(); //Loop through each tag in the dom and add it to the link array foreach($xml->getElementsByTagName('a') as $link) { $links[] = array('url' => $link->getAttribute('href'), 'text' => $link->nodeValue); } //Return the links return $links; } ?> Regards, Yann -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: > If you switch to vertically aligned braces you will have an easier time > matching up open and close braces... > > if (something) > { > // Code here > } > > It's trivial to match using this style since you only need to scan one axis. > That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. -- Dotan Cohen http://gibberish.co.il http://what-is-what.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On Tue, Feb 22, 2011 at 01:29:30PM +0200, Dotan Cohen wrote: > I wrote a short page on how to actually type of code that one writes, > it can be found here: > http://dotancohen.com/howto/write_code.html > > The point that I stress on the page is that first you close an > element, then you fill it's contents. The page has examples in HTML > and PHP. I would like to know what the experienced programmers here > think about this. Am I going about it wrong? Not a bad idea for HTML, not so great for PHP. Some of what you're trying to solve/avoid can be handled by a decent editor, one that does syntax highlighting and/or does brace and parenthesis checking. It doesn't require an expensive IDE. I use Vim, and it works fine for this. And honestly, if you're serious about programming, you really shouldn't be using something like Notepad on Windows. Seriously. We had a coder working for a company where I was who coded in Word of all things. We just looked at him like he was crazy. Annotating end-braces is also very helpful for long conditionals. Vim can track start and stop braces, but when they span several screen pages, it can be a problem. Likewise commenting code as you go is an excellent idea. 2:1 comments are a bit much; Linus Torvalds would kick your butt. But I often do this if I'm not exactly sure how I'm going to code something, but I know the rough logic of it. Lay out the conditionals, comment on what should happen within each code block, and then go back and fill in the blocks with actual code. It's also worth noting that sometimes code is hard to follow. And even if you're going to be the only one updating it, you may well forget the logic six months from now. Commenting allows you to recapture that logic more easily. And then there's the PHP interpreter. If you make a syntax error, like failing to add an closing brace, the interpreter will tell you so. If you spend *enough* time coding, you can usually track down where your mistakes are relatively easily. If you've ever coded in C, the same is true for the C compiler. Its behavior is very very similar to that of the PHP interpreter. It's something you get used to over time as you code more and more. Also, what could be called "incremental coding" or "stepwise development" helps: Code a little. Run/compile. Code a little, run/compile. That way, errors are easier to catch than after you've written 5000 lines in a file. Paul -- Paul M. Foster http://noferblatz.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 22/02/11 09:40, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. Well the way you do it, drives me nuts :) Morale of the story, do whatever *you* are most comfortable with..personally I indent as well, has it's downsides I admit, but it's what I've done for the last 20 years and I'm comfortable with it. Alexis -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to write code: how wrong am I?
On 2/22/11 12:04 PM, Alexis wrote: On 22/02/11 09:40, Dotan Cohen wrote: On Tue, Feb 22, 2011 at 16:15, Robert Cummings wrote: If you switch to vertically aligned braces you will have an easier time matching up open and close braces... if (something) { // Code here } It's trivial to match using this style since you only need to scan one axis. That bracing style drives me nuts! But I have no problem scanning a single axis for the close brace and any statement, be it an if, while, switch, or something else. Well the way you do it, drives me nuts :) Morale of the story, do whatever *you* are most comfortable with..personally I indent as well, has it's downsides I admit, but it's what I've done for the last 20 years and I'm comfortable with it. Alexis The most important thing is that you are consistent within a project and within a team. Your entire team should be consistently using one format standard, whatever it is. Failure to do so results in unreadable code, because your brain has to shift gears depending on who wrote a given block of code. That's bad news. I'd rather see code written in brace-on-own-line style (which I despise) than code that freely switches between that and inline-brace style (my preference) every other for-loop. That's simply unreadable. --Larry Garfield -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Large binary objects in MySQL - problem
Hi. I use PDO and have written myself a class to abstract the database a bit. It has a method for querying the database specifically for large objects, like this: $connection = $this->getConnection(); // what's returned is a PDOStatement, and a transaction is already started $statement = $connection->prepare($sql); $result = true; foreach ($parameters as $key => $parameter) $statement->bindValue($key, $parameter->value, $parameter->type); try { $result = $statement->execute(); } catch(Exception $ex) { $statement->closeCursor(); throw $ex; } if (!$result) { $statement->closeCursor(); throw new Exception("SQL statement failed: ".$sql); } $data = array(); $receiverRow = array(); $i = 0; foreach ($columns as $column => $type) { $receiverRow[$column] = NULL; // this is probably not necessary, I added it after it didn't work without, but it didn't help $statement->bindColumn($i++, &$receiverRow[$column], $type); } while ($statement->fetch(PDO::FETCH_BOUND)) { $row = array(); foreach ($columns as $column => $type) $row[$column] = $receiverRow[$column]; $data[] = $row; } $statement->closeCursor(); return $data; The problem is, after $statement-> execute() the first fetch returns false, although there's definitely a record in there - $statement->rowCount() says 1, if called before $statement->fetch(). No exception is thrown, $statement->errorInfo() and $statement->errorCode don't contain anything useful. What am I doing wrong? br, flj -- Fine counsel is confusing, but example is always clear. (Edgar A. Guest, The Light of Faith) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Problems with PDO and LOB columns in mysql
Hi. The code I use to extract the data from a query which was just performed looks like this: $data = array(); $receiverRow = array(); $i = 0; foreach ($columns as $column => $type) $statement->bindColumn($i++, &$receiverRow[$column], $type); while ($statement->fetch(PDO::FETCH_BOUND)) { $row = array(); foreach ($columns as $column => $type) $row[$column] = $receiverRow[$column]; $data[] = $row; } I know there is a record that fetch should fetch, since $statement-> rowCount() says so. Everything works really well, and even if I check $statement->errorInfo() and $statement->errorCode() after each operation with the database, I can see no error. The types with which the variables are bound are also right. However, the $statement->fetch() inside the the while statement returns false at the very first call, so I can't fetch anything from within the $statement. There is no exception, no non-zero value in $statement->errorInfo(), and $statement->errorCode() says uninitialized. So I don't know what actually went wrong. The entire process happens inside a transaction. At the end, finding out that the record was not retrieved, the transaction is rolled back. The statement is an insert following by a select. When calling PDO::lastInsertId, I get a proper value, which means the insert is succesful. When checking $statement->rowCount() I also get a count of 1, which means the select also worked properly. I just fail to fetch the just inserted record into some PHP variables. What am I doing wrong? As far as I can tell, I did everything as the manual says. br, -- Fine counsel is confusing, but example is always clear. (Edgar A. Guest, The Light of Faith) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Dynamically Created Checkboxes
I have a script that was working, however I discovered it was only working in IE7, not in FF or Chrome. It also has stopped working in IE7 once I put it on the remote server. It is to create checkboxes from data called from the db. I have echo'd the mysql_num_row and am getting a result, so the query seems to be working. At this point I get a blank page. Can someone point me in the right direction to get this working (again). $choice=$_POST['state']; mysql_select_db($database_assess, $assess_remote); $query_Recordset1 = ("SELECT * FROM `counties` WHERE state_id = '$choice'")or die(mysql_error()); $Recordset1 = mysql_query($query_Recordset1, $assess_remote) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); "; } } } else { // if form isn't submitted, output the form $county_choice=$_SESSION['county{$1}']; print "\n"; if ($Recordset1) { print "\n"; print " \n"; print " State \n"; //2 fields in Counties table, State and County print " County \n"; print "\n"; //create table $i = 0; while ( $row = mysql_fetch_array($Recordset1) ) { $i++; print "\n"; print "\n"; echo "{$row['state_id']}\n"; echo "{$row['name']}\n"; echo "\n"; }//end while print "\n"; } else { echo("Error performing query: " . mysql_error() . ""); } print "\n"; print "\n"; } ?> Would appriciate your help. Thank you. -- Gary __ Information from ESET Smart Security, version of virus signature database 5897 (20110222) __ The message was checked by ESET Smart Security. http://www.eset.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Large binary objects in MySQL - problem
On Tue, Feb 22, 2011 at 1:07 PM, Florin Jurcovici wrote: > Hi. > > I use PDO and have written myself a class to abstract the database a bit. > > It has a method for querying the database specifically for large > objects, like this: > > $connection = $this->getConnection(); // what's returned is a > PDOStatement, and a transaction is already started > $statement = $connection->prepare($sql); > $result = true; > foreach ($parameters as $key => $parameter) > $statement->bindValue($key, $parameter->value, > $parameter->type); > try > { > $result = $statement->execute(); > } > catch(Exception $ex) Wrong catch? [1] Regards, Tommy [1] http://php.net/pdoexception > { > $statement->closeCursor(); > throw $ex; > } > if (!$result) > { > $statement->closeCursor(); > throw new Exception("SQL statement failed: ".$sql); > } > $data = array(); > $receiverRow = array(); > $i = 0; > foreach ($columns as $column => $type) > { > $receiverRow[$column] = NULL; // this is probably > not necessary, I > added it after it didn't work without, but it didn't help > $statement->bindColumn($i++, &$receiverRow[$column], > $type); > } > while ($statement->fetch(PDO::FETCH_BOUND)) > { > $row = array(); > foreach ($columns as $column => $type) > $row[$column] = $receiverRow[$column]; > $data[] = $row; > } > $statement->closeCursor(); > return $data; > > The problem is, after $statement-> execute() the first fetch returns > false, although there's definitely a record in there - > $statement->rowCount() says 1, if called before $statement->fetch(). > No exception is thrown, $statement->errorInfo() and > $statement->errorCode don't contain anything useful. > > What am I doing wrong? > > br, > > flj > > -- > Fine counsel is confusing, but example is always clear. (Edgar A. > Guest, The Light of Faith) >