[PHP] Usiing FOREACH to loop through Array
Hi All, I've stumped myself here. In a nutshell, I have a function that returns my array based on a SQL query and here's the code: -begin code--- function getCourses($UID) { global $link; $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); return mysql_fetch_array( $result ); } end code I call this from a PHP page with the following code: begin code-- $myCourses = getCourses($session[id]); foreach ($myCourses as $value) { print br$value; } end code--- Now, when I test the SQL from my function directly on the database, it returns just want I want it to return but it isn't working that way on my PHP page. For results where there is a single entry, I am getting the same entry TWICE and for records with more than a single entry I am getting ONLY the FIRST entry TWICE. Now I know my SQL code is correct (I am testing it against a MySQL database using MySQL-Front) so I suspect I'm doing something stupid in my foreach loop. I'm hoping someone will spot my dumb mistake. Thanks very much for any help at all on this. Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Usiing FOREACH to loop through Array
Steve, Thanks very much. This make total sense to me. Now, I was pretty sure that I was getting an array back from mysql_fetch_array because when I do a 'print $myCourses' without specifying any value, I just get 'array' which I believe I am supposed to, IF it is truly an array. Anyway, I'm going to revisit the mysql_fetch_array function right now. Thanks for your help, it is greatly appreciated. ...Brad Steve Edberg [EMAIL PROTECTED] wrote in message news:p05100300b943f2a7feef@[168.150.239.37]... At 3:27 PM -0700 6/29/02, Brad Melendy wrote: Hi All, I've stumped myself here. In a nutshell, I have a function that returns my array based on a SQL query and here's the code: -begin code--- function getCourses($UID) { global $link; $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); return mysql_fetch_array( $result ); } end code I call this from a PHP page with the following code: begin code-- $myCourses = getCourses($session[id]); foreach ($myCourses as $value) { print br$value; } end code--- Now, when I test the SQL from my function directly on the database, it returns just want I want it to return but it isn't working that way on my PHP page. For results where there is a single entry, I am getting the same entry TWICE and for records with more than a single entry I am getting ONLY the FIRST entry TWICE. Now I know my SQL code is correct (I am testing it against a MySQL database using MySQL-Front) so I suspect I'm doing something stupid in my foreach loop. I think your problem lies in a misunderstanding of the mysql_fetch_array() function. It doesn't return the entire result set in an array - just one record at a time. You can fix this in one of two ways: (1) Loop though the entire result set in your function: function getCourses($UID) { global $link; $ResultSet = array(); $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); while ($Row = mysql_fetch_array( $result )) { $ResultSet[] = $Row['C.CourseName']; } return $ResultSet; } ... $myCourses = getCourses($session[id]); foreach ($myCourses as $value) { print br$value; } or (2) set a flag in getCourses() so that the query is only executed once, otherwise returning a result line - something like: function getCourses($UID) global $link; static $result = false; if (!$result) { $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); } { return mysql_fetch_array( $result ); } ... while ($Row = getCourses($session[id]) as $value) { print br, $Row['C.CourseName']; } (standard caveats about off-top-of-head, untested code apply) The reason you are getting the first record TWICE is becaouse of the default behaviour of the mysql_fetch_array() function. It returns both an associative array - ie, elements of the form field-name = value - and a numerically indexed array (0, 1, 2, etc.). You can alter this behaviour by the second parameter of the function: see http://www.php.net/manual/en/function.mysql-fetch-array.php -steve I'm hoping someone will spot my dumb mistake. Thanks very much for any help at all on this. Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- ++ | Steve Edberg [EMAIL PROTECTED] | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | ++ | The end to politics as usual: | | The Monster Raving Loony Party (http://www.omrlp.com/) | ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Usiing FOREACH to loop through Array
Steve, Thanks a ton! I'm now running slick as a whistle using your option #1 (looping through the results in the function) and using the newer mysql_fetch_assoc() function. I have a much better understanding of this now and I really appreciate your help. Hopefully I'll get a chance to help someone else next time. :-) Steve Edberg [EMAIL PROTECTED] wrote in message news:p05100300b943f2a7feef@[168.150.239.37]... At 3:27 PM -0700 6/29/02, Brad Melendy wrote: Hi All, I've stumped myself here. In a nutshell, I have a function that returns my array based on a SQL query and here's the code: -begin code--- function getCourses($UID) { global $link; $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); return mysql_fetch_array( $result ); } end code I call this from a PHP page with the following code: begin code-- $myCourses = getCourses($session[id]); foreach ($myCourses as $value) { print br$value; } end code--- Now, when I test the SQL from my function directly on the database, it returns just want I want it to return but it isn't working that way on my PHP page. For results where there is a single entry, I am getting the same entry TWICE and for records with more than a single entry I am getting ONLY the FIRST entry TWICE. Now I know my SQL code is correct (I am testing it against a MySQL database using MySQL-Front) so I suspect I'm doing something stupid in my foreach loop. I think your problem lies in a misunderstanding of the mysql_fetch_array() function. It doesn't return the entire result set in an array - just one record at a time. You can fix this in one of two ways: (1) Loop though the entire result set in your function: function getCourses($UID) { global $link; $ResultSet = array(); $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); while ($Row = mysql_fetch_array( $result )) { $ResultSet[] = $Row['C.CourseName']; } return $ResultSet; } ... $myCourses = getCourses($session[id]); foreach ($myCourses as $value) { print br$value; } or (2) set a flag in getCourses() so that the query is only executed once, otherwise returning a result line - something like: function getCourses($UID) global $link; static $result = false; if (!$result) { $result = mysql_query( SELECT C.CourseName FROM tblcourses C, tblusers U, tblEnrollment E WHERE C.ID = E.CourseID AND E.UserID = U.ID AND U.ID = $UID, $link ); if ( ! $result ) die ( getRow fatal error: .mysql_error() ); } { return mysql_fetch_array( $result ); } ... while ($Row = getCourses($session[id]) as $value) { print br, $Row['C.CourseName']; } (standard caveats about off-top-of-head, untested code apply) The reason you are getting the first record TWICE is becaouse of the default behaviour of the mysql_fetch_array() function. It returns both an associative array - ie, elements of the form field-name = value - and a numerically indexed array (0, 1, 2, etc.). You can alter this behaviour by the second parameter of the function: see http://www.php.net/manual/en/function.mysql-fetch-array.php -steve I'm hoping someone will spot my dumb mistake. Thanks very much for any help at all on this. Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- ++ | Steve Edberg [EMAIL PROTECTED] | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | ++ | The end to politics as usual: | | The Monster Raving Loony Party (http://www.omrlp.com/) | ++ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: One more 'Headers Already Sent' error. :-(
No Worries, I know when people post the same questions over and over again, it pisses a lot of the folks here off. I really do read other messages first and then post only when I can't figure out the answer from other posts. I appreciate your input. It makes more sense when you explain what the browser sees as output. Thanks again. ...Brad John Holmes [EMAIL PROTECTED] wrote in message news:000f01c21cbd$7f49e0e0$b402a8c0@mango... Sorry if I was a little rude. The thing to remember that anything outside of a ? And ? is considered output, even if it's in an include. So if I have an include that does ? $var = 'value'; ?, then I'm fine. But if I have an include that does pHip, or html, or even just a space within it outside of a ? And ?, it's going to be considered output. and any output before a header() call is going to break it (unless output buffering is on). ---John Holmes... -Original Message- From: Brad Melendy [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 25, 2002 10:39 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] Re: One more 'Headers Already Sent' error. :-( Oh I read all the error messages, I just don't understand them. ;-) I'm working through a book Teach Yourself PHP4 in 24 Hours so much of this is new to me. I think I understand now and it makes sense that by putting the include statement farther down in the page so that it wasn't being executed until after the header function in the fist portion of code. Thanks for your help. Brad John Holmes [EMAIL PROTECTED] wrote in message news:000101c21ca8$5040e970$b402a8c0@mango... If header.html has HTML in it, THEN THAT IS OUTPUT! You can't send a header after output. Do you even read the error message? It says OUTPUT STARTED AT /home/bmelendy/websites/www.e-learn.net/quizD/header.html:3. THAT MEANS LINE 3 STARTED OUTPUT... ---John been a long day, deal with it! Holmes... -Original Message- From: Brad Melendy [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 25, 2002 6:08 PM To: [EMAIL PROTECTED] Subject: [PHP] Re: One more 'Headers Already Sent' error. :-( Well, I have traced this down to line 4 in the following code 'include(header.html);' which just includes my navigation bar. If I comment it out, everything works great, if I leave it in, I get the error Headers Already Sent. The file it purely html. What could be the problem?? Thanks in advance! ?php error_reporting(2039); include(dblib.inc); include(userlib.inc); include(header.html); $message=; if ( isset( $actionflag ) $actionflag == login ) { if ( empty( $form[login] ) || empty( $form[password] ) ) $message .= You must fill in all fieldsbr\n; if ( ! ( $row_array = checkPass( $form[login], $form[password] ) ) ) $message .= Incorrect password. Please try againbr\n; if ( $message == ) // we found no errors { LastLogin($row_array[login]); cleanSession( $row_array[id], $row_array[login], $row_array[password] ); header( Location: index.php?.SID ); } } ? Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi All, I know this has come up before and I've read some posts so far, but this is still eluding me. I have come to understand that typically, if you are getting an error message about your headers having already been sent to the browser, you are writing bad code. Now, I want to right good code so I really want to figure this out the right way and stay away from doing stuff like setting Output Buffering to ON in the php.ini file, (which I can do in my case). So, basically, I get the error message: Warning: Cannot add header information - headers already sent by (output started at /home/bmelendy/websites/www.e-learn.net/quizD/header.html:3) in /home/bmelendy/websites/www.e-learn.net/quizD/login.php on line 17 Now, I can see right on line 17 that I'm outputting a header with the following line: header( Location: index.php?.SID ); This is part of an if/then statement that is supposed to redirect the user to index.php upon login. The conflict occurs because I have an include file with a function checkUser() that also redirects the user to the login.php page if they fail the the check with a line: header( Location: /main/login.php ); So, is there no way for me to redirect a user from both a function under certain circumstances and also from withing a page that calls that function? I don't completely understand when exactly the headers are being sent and this does all work properly if I turn the Output B
Re: [PHP] Re: One more 'Headers Already Sent' error. :-(
I wanted the script to redirect to different locations based on what conditions were being met. My problem turned out that I had an include statement including an html file prior to the header function on line 17. If I understand this correctly, the include statement was providing 'output' and then it was trying to output again later in the header() function. By moving my include statement outside the ?php .? in the intial section of code, it wasn't providing the output until after my header() function had completed and there wasn't an issue anymore. Thanks for your help. ...Brad Erik Price [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... On Tuesday, June 25, 2002, at 06:07 PM, Brad Melendy wrote: Well, I have traced this down to line 4 in the following code 'include(header.html);' which just includes my navigation bar. If I comment it out, everything works great, if I leave it in, I get the error Headers Already Sent. The file it purely html. What could be the problem?? Thanks in advance! You want this script to redirect twice? Or do you just want it to redirect the first time, but have the second header() function be called if the first one doesn't? Then you should call exit() after the first header() function to keep the rest of the script from executing. This is also generally a good practice after a header('Location: ') call because the user-agent doesn't HAVE to respect the header and redirect, so it protects your stuff. Always feature an exit() with a header-based redirect. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] One more 'Headers Already Sent' error. :-(
Hi All, I know this has come up before and I've read some posts so far, but this is still eluding me. I have come to understand that typically, if you are getting an error message about your headers having already been sent to the browser, you are writing bad code. Now, I want to right good code so I really want to figure this out the right way and stay away from doing stuff like setting Output Buffering to ON in the php.ini file, (which I can do in my case). So, basically, I get the error message: Warning: Cannot add header information - headers already sent by (output started at /home/bmelendy/websites/www.e-learn.net/quizD/header.html:3) in /home/bmelendy/websites/www.e-learn.net/quizD/login.php on line 17 Now, I can see right on line 17 that I'm outputting a header with the following line: header( Location: index.php?.SID ); This is part of an if/then statement that is supposed to redirect the user to index.php upon login. The conflict occurs because I have an include file with a function checkUser() that also redirects the user to the login.php page if they fail the the check with a line: header( Location: /main/login.php ); So, is there no way for me to redirect a user from both a function under certain circumstances and also from withing a page that calls that function? I don't completely understand when exactly the headers are being sent and this does all work properly if I turn the Output Buffering to 'ON' in the php.ini file, but I'd like to try to make this work the 'right' way rather than cut corners and just make the server work for my code, instead of making my code work for the server. ;-) Thanks very much for any input. ...Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: One more Regular Expression Question...
Thanks Miguel, I'm going to check out preg_match(). What if there are more than one instance of a and /a in my string, but I know that I want the last two occurances at the end. Can I tell it to check starting at the end of the string and work forwards? Thanks again! ...Brad Miguel Cruz [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Preg is more flexible than ereg. if (preg_match('/\a.*?\(.+?)\\/a/', $string1, $matches)) print Matched: {$matches[1]}; That'll just get what's between the a and /a. http://php.net/preg_match for more info. miguel On Mon, 13 May 2002, Brad Melendy wrote: Thanks Philip. This gives me everything before the ' that I'm looking for but I believe it is because it occurs more than once. I think I need to count the times that ' occurs and then try to target the specific occurance that I need. But I'm guessing a little. Thanks for your help. ...Brad Philip Hallstrom [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... $string1 = a href='/Schedule_Detail'Here's the Schedule/a; ereg((.*)/a, $string1, $matches); $string2 = $matches[1]; Something close to that anyway... On Mon, 13 May 2002, Brad Melendy wrote: Hello, I'm pretty stuck here. I wish to assign a variable the value of a particular substrint of text from a second variable. It's HTML so I've got something like: $string1 = a href='/Schedule_Detail'Here's the Schedule/a I want to create a variable $string2 and assign it the value of whatever is between and /a. I just can't figure out how to do that. Any kind souls want to give me a hand up? Thanks very much in advance. ...Brad PS. I do have a PHP book and I did try reading up on regular expressions at php.net first, but I'm just not getting it. :-\ -- 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
Re: [PHP] Re: One more Regular Expression Question...
I want to thank everyone for their help. I finally found the right expression. I needed to start at the end of the string and work backwards. STRRCHR did the trick for me. I was able to use: $match = substr(strrchr($string1, ), 1 ); echo $match; I'm actually not sure what role the last 1 has in the above expression, but it works so I'm not gonna complain. But if anyone wants to explain it to me, that's wonderful. The info for STRRCHR at php.net is a tad light to say the least on this particular function. Thanks again. Brad Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Thanks Miguel, I'm going to check out preg_match(). What if there are more than one instance of a and /a in my string, but I know that I want the last two occurances at the end. Can I tell it to check starting at the end of the string and work forwards? Thanks again! ...Brad Miguel Cruz [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Preg is more flexible than ereg. if (preg_match('/\a.*?\(.+?)\\/a/', $string1, $matches)) print Matched: {$matches[1]}; That'll just get what's between the a and /a. http://php.net/preg_match for more info. miguel On Mon, 13 May 2002, Brad Melendy wrote: Thanks Philip. This gives me everything before the ' that I'm looking for but I believe it is because it occurs more than once. I think I need to count the times that ' occurs and then try to target the specific occurance that I need. But I'm guessing a little. Thanks for your help. ...Brad Philip Hallstrom [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... $string1 = a href='/Schedule_Detail'Here's the Schedule/a; ereg((.*)/a, $string1, $matches); $string2 = $matches[1]; Something close to that anyway... On Mon, 13 May 2002, Brad Melendy wrote: Hello, I'm pretty stuck here. I wish to assign a variable the value of a particular substrint of text from a second variable. It's HTML so I've got something like: $string1 = a href='/Schedule_Detail'Here's the Schedule/a I want to create a variable $string2 and assign it the value of whatever is between and /a. I just can't figure out how to do that. Any kind souls want to give me a hand up? Thanks very much in advance. ...Brad PS. I do have a PHP book and I did try reading up on regular expressions at php.net first, but I'm just not getting it. :-\ -- 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] One more Regular Expression Question...
Hello, I'm pretty stuck here. I wish to assign a variable the value of a particular substrint of text from a second variable. It's HTML so I've got something like: $string1 = a href='/Schedule_Detail'Here's the Schedule/a I want to create a variable $string2 and assign it the value of whatever is between and /a. I just can't figure out how to do that. Any kind souls want to give me a hand up? Thanks very much in advance. ...Brad PS. I do have a PHP book and I did try reading up on regular expressions at php.net first, but I'm just not getting it. :-\ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: One more Regular Expression Question...
Thanks Philip. This gives me everything before the ' that I'm looking for but I believe it is because it occurs more than once. I think I need to count the times that ' occurs and then try to target the specific occurance that I need. But I'm guessing a little. Thanks for your help. ...Brad Philip Hallstrom [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... $string1 = a href='/Schedule_Detail'Here's the Schedule/a; ereg((.*)/a, $string1, $matches); $string2 = $matches[1]; Something close to that anyway... On Mon, 13 May 2002, Brad Melendy wrote: Hello, I'm pretty stuck here. I wish to assign a variable the value of a particular substrint of text from a second variable. It's HTML so I've got something like: $string1 = a href='/Schedule_Detail'Here's the Schedule/a I want to create a variable $string2 and assign it the value of whatever is between and /a. I just can't figure out how to do that. Any kind souls want to give me a hand up? Thanks very much in advance. ...Brad PS. I do have a PHP book and I did try reading up on regular expressions at php.net first, but I'm just not getting it. :-\ -- 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] Segmented Code/HTML VS. ECHO??
Hello, Other than the fact that sometimes, you just can't get raw HTML to process properly by dropping out of PHP code, what are the pros and cons of using RAW HTML or just ECHOING everything in PHP? Thanks for any insights. ..Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: Can If Else statements be split into code blocks??
Ok, I figured out that just using echo seems to be the best way to do this under PHP. In ASP, you can end your code block and start in with HTML, but I couldn't get that to work with PHP. However, I was able to just use the echo statement to get the conditional HTML I wanted to show up when the proper condition was met in the If Else statement. Brad Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hello, I'm trying to execute some HTML in an IF ELSE statement. I'm trying something like: ?php if (strstr($DomResults,$Match)) print Congratulations! $domain.$suffix is available!; ? form method=POST action=step2.asp name=form2 pinput type=submit value=Register name=B1/p /form ?php else print Sorry, $domain.$suffix is already taken.; ? Basically, it works great without the form I'm trying to insert, but with the form after the IF statement, it fails. Is what I want to do against the rules? I'm converting an ASP script I have to PHP and I have it all working under ASP. That means it should be eaiser with PHP right? ;-) Thanks in advance. ...Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: Using Logical OR operator in IF statement???
Thanks James, I tried a regular expression comparison first, but it was eluding me. I'm new to PHP (trying to convert form ASP) and part of this is me trying to convert my ASP/VBSCRIPT to PHP. ;-) I actually have a FOR loop evaluated when the conditions of the IF statement are met, and I suspect that is messing things up. Here's my entire code and I've used your expression comparison instead but it still fails to find the - unless I split things up and check either ONLY for the '-' at the beginning or the end, but not both in the same line: function StringCheck($sString) { if (preg_match(/^-|-$/s, $sString)) { for ( $counter=0; $counter strlen($sString); $counter++ ) { $nChar = ord(strtolower(substr($sString, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or ($nChar == 45)) { $result = TRUE; } else { $result = FALSE; break; } } } else { $result = FALSE; } return $result; } //End Function StringCheck Maybe you see something I don't. I can't get over the fact that if I check for just the front, or the end, it works, but if I check for both the front and end of the string in the same line with the OR, it fails. :-\ Thanks in advance for any thing you might notice. Brad Yz James [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi Brad, This worked for me: ? if ((substr($sString, 0, (strlen($sString)-1) == -)) || (substr($sString, 0, 1) == -)) { echo you can't have a dash at the beginning or end of your string.; } ? .. but I'd tend to go for a regex as a solution to what you're after, which involves less code: ? if (preg_match(/^-|-$/s, $string)) { echo You cannot have a \-\ character at the beginning or end of your string.; } else { echo Whatever; } ? Just my thoughts... James Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hello, Ok, this works: if (substr($sString,(strlen($sString)-1)!=-)) { print You can't have a dash at the end of your string.; } and this works: if (substr($sString,0,1)!=-) { print You can't have a dash at the beginning of your string.; } But, this doesn't work for any case: if ((substr($sString,(strlen($sString)-1)!=-)) or (substr($sString,0,1)!=-)) { print you can't have a dash at the beginning or end of your string.; } What could be wrong? I've used a logical OR operator in the middle of an IF statement like this before, but for some reason, this just isn't working. Anyone got any ideas? I suppose I can just evaluate this with two different IF statements, but it seems like I shoud be able to do it in one and reduce duplicate code. Thanks very much in advance. .Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: Using Logical OR operator in IF statement???
Ok, it works!! Thanks to everyone for their suggestions and answers. This group is a great resourse. Thanks again ...Brad Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Thanks James, I tried a regular expression comparison first, but it was eluding me. I'm new to PHP (trying to convert form ASP) and part of this is me trying to convert my ASP/VBSCRIPT to PHP. ;-) I actually have a FOR loop evaluated when the conditions of the IF statement are met, and I suspect that is messing things up. Here's my entire code and I've used your expression comparison instead but it still fails to find the - unless I split things up and check either ONLY for the '-' at the beginning or the end, but not both in the same line: function StringCheck($sString) { if (preg_match(/^-|-$/s, $sString)) { for ( $counter=0; $counter strlen($sString); $counter++ ) { $nChar = ord(strtolower(substr($sString, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or ($nChar == 45)) { $result = TRUE; } else { $result = FALSE; break; } } } else { $result = FALSE; } return $result; } //End Function StringCheck Maybe you see something I don't. I can't get over the fact that if I check for just the front, or the end, it works, but if I check for both the front and end of the string in the same line with the OR, it fails. :-\ Thanks in advance for any thing you might notice. Brad Yz James [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hi Brad, This worked for me: ? if ((substr($sString, 0, (strlen($sString)-1) == -)) || (substr($sString, 0, 1) == -)) { echo you can't have a dash at the beginning or end of your string.; } ? .. but I'd tend to go for a regex as a solution to what you're after, which involves less code: ? if (preg_match(/^-|-$/s, $string)) { echo You cannot have a \-\ character at the beginning or end of your string.; } else { echo Whatever; } ? Just my thoughts... James Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Hello, Ok, this works: if (substr($sString,(strlen($sString)-1)!=-)) { print You can't have a dash at the end of your string.; } and this works: if (substr($sString,0,1)!=-) { print You can't have a dash at the beginning of your string.; } But, this doesn't work for any case: if ((substr($sString,(strlen($sString)-1)!=-)) or (substr($sString,0,1)!=-)) { print you can't have a dash at the beginning or end of your string.; } What could be wrong? I've used a logical OR operator in the middle of an IF statement like this before, but for some reason, this just isn't working. Anyone got any ideas? I suppose I can just evaluate this with two different IF statements, but it seems like I shoud be able to do it in one and reduce duplicate code. Thanks very much in advance. .Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Can I have If...Else inside a For Loop???
H. dunno about that. It's returning the results I'd expect as it is now. If I pass a string with characters that don't match the IF's criteria, I'm getting the ELSE statement's results. Thanks though. Brad ~~~I Leonid ~~ [EMAIL PROTECTED] wrote in message 3bd15547.14743496@localhost">news:3bd15547.14743496@localhost... On Fri, 19 Oct 2001 21:46:29 -0700 impersonator of [EMAIL PROTECTED] (Brad Melendy) planted I saw in php.general: arrh! I don't think I can say much else. Thanks so much for pointing that out David. ;-) .Brad David Pearson [EMAIL PROTECTED] wrote in message 003101c1591a$e00ec290$320110ac@david">news:003101c1591a$e00ec290$320110ac@david... Could it be the semi colon at the end of the 'if' line, at '... $nChar = 45); { ...' ? Looks like the if statement ends there, the ' { print OK.; } ' is a block on it's own and the 'else' is now out of context. -Original Message----- From: Brad Melendy [mailto:[EMAIL PROTECTED]] Sent: Friday, October 19, 2001 8:03 PM To: [EMAIL PROTECTED] Subject: [PHP] Can I have If...Else inside a For Loop??? Hi All, I'm stumped. I've got the following code: ?php if(isSet($domain)) { for ( $counter=0; $counter = strlen($domain); $counter++ ) { $nChar = ord(strtolower(substr($domain, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or $nChar = 45); I'd also ^^put == here, or you NEVER get to else{ anyway -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Can I have If...Else inside a For Loop???
Oops, I spoke too soon. I wasn't getting the results I thought I was until I used == instead of = at the very end of my IF statement. Thanks very much for pointing that out. Brad Brad Melendy [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... H. dunno about that. It's returning the results I'd expect as it is now. If I pass a string with characters that don't match the IF's criteria, I'm getting the ELSE statement's results. Thanks though. Brad ~~~I Leonid ~~ [EMAIL PROTECTED] wrote in message 3bd15547.14743496@localhost">news:3bd15547.14743496@localhost... On Fri, 19 Oct 2001 21:46:29 -0700 impersonator of [EMAIL PROTECTED] (Brad Melendy) planted I saw in php.general: arrh! I don't think I can say much else. Thanks so much for pointing that out David. ;-) .Brad David Pearson [EMAIL PROTECTED] wrote in message 003101c1591a$e00ec290$320110ac@david">news:003101c1591a$e00ec290$320110ac@david... Could it be the semi colon at the end of the 'if' line, at '... $nChar = 45); { ...' ? Looks like the if statement ends there, the ' { print OK.; } ' is a block on it's own and the 'else' is now out of context. -Original Message----- From: Brad Melendy [mailto:[EMAIL PROTECTED]] Sent: Friday, October 19, 2001 8:03 PM To: [EMAIL PROTECTED] Subject: [PHP] Can I have If...Else inside a For Loop??? Hi All, I'm stumped. I've got the following code: ?php if(isSet($domain)) { for ( $counter=0; $counter = strlen($domain); $counter++ ) { $nChar = ord(strtolower(substr($domain, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or $nChar = 45); I'd also ^^put == here, or you NEVER get to else{ anyway -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Using Logical OR operator in IF statement???
Hello, Ok, this works: if (substr($sString,(strlen($sString)-1)!=-)) { print You can't have a dash at the end of your string.; } and this works: if (substr($sString,0,1)!=-) { print You can't have a dash at the beginning of your string.; } But, this doesn't work for any case: if ((substr($sString,(strlen($sString)-1)!=-)) or (substr($sString,0,1)!=-)) { print you can't have a dash at the beginning or end of your string.; } What could be wrong? I've used a logical OR operator in the middle of an IF statement like this before, but for some reason, this just isn't working. Anyone got any ideas? I suppose I can just evaluate this with two different IF statements, but it seems like I shoud be able to do it in one and reduce duplicate code. Thanks very much in advance. .Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Can I have If...Else inside a For Loop???
Hi All, I'm stumped. I've got the following code: ?php if(isSet($domain)) { for ( $counter=0; $counter = strlen($domain); $counter++ ) { $nChar = ord(strtolower(substr($domain, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or $nChar = 45); { print OK.; } else { print Not OK.; } } } ? What's got me beat is that this code fails on the ELSE statement. Now, if I comment out the ELSE statement, the code proceeds, I just don't get the results I want when the coditions in IF statement are false. Anyone know what I'm doing wrong? It's as if you can't nest an IF/Else under a FOR, but you can use just an IF. Thanks very much in advance. Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Can I have If...Else inside a For Loop???
arrh! I don't think I can say much else. Thanks so much for pointing that out David. ;-) .Brad David Pearson [EMAIL PROTECTED] wrote in message 003101c1591a$e00ec290$320110ac@david">news:003101c1591a$e00ec290$320110ac@david... Could it be the semi colon at the end of the 'if' line, at '... $nChar = 45); { ...' ? Looks like the if statement ends there, the ' { print OK.; } ' is a block on it's own and the 'else' is now out of context. -Original Message- From: Brad Melendy [mailto:[EMAIL PROTECTED]] Sent: Friday, October 19, 2001 8:03 PM To: [EMAIL PROTECTED] Subject: [PHP] Can I have If...Else inside a For Loop??? Hi All, I'm stumped. I've got the following code: ?php if(isSet($domain)) { for ( $counter=0; $counter = strlen($domain); $counter++ ) { $nChar = ord(strtolower(substr($domain, $counter, 1))); if (($nChar 47 And $nChar 58) or ($nChar 96 And $nChar 123) or $nChar = 45); { print OK.; } else { print Not OK.; } } } ? What's got me beat is that this code fails on the ELSE statement. Now, if I comment out the ELSE statement, the code proceeds, I just don't get the results I want when the coditions in IF statement are false. Anyone know what I'm doing wrong? It's as if you can't nest an IF/Else under a FOR, but you can use just an IF. Thanks very much in advance. Brad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]