LMarie Posted December 1, 2009 Share Posted December 1, 2009 I have a sql table containing id - query - query name the first page contains a drop down menu of all the query names. When chosen the user is sent to a page that prints the result of the query in a html table. Now i nees to find how many rows the table should have. This counts how many rows i need in the table: $parts=explode(' FROM ',$query); $count=explode(",", $parts[0]); $result=count($count); But the problem is that here i explode by commas but the query also contains commas like this: substring (***,19,10) as Part1 My question is how do i count only commas outside these parenteses. something like this: $var=0; start counting commas in parts[0], when ( accures increase $var to 1, if $var = 1 dont count commas, but when ) accures set $var to 0, and start counting again The reason why im making it this way is that it's suppose to be possible to just add a query to the sql table no matter how many rows this query needs in output. Any ideas? Link to comment https://forums.phpfreaks.com/topic/183563-count-only-commas-outside-parenteses/ Share on other sites More sharing options...
cags Posted December 2, 2009 Share Posted December 2, 2009 Can you give an example query that causes problems along with the expected comma count? Link to comment https://forums.phpfreaks.com/topic/183563-count-only-commas-outside-parenteses/#findComment-969694 Share on other sites More sharing options...
LMarie Posted December 3, 2009 Author Share Posted December 3, 2009 When a query contains substring example: SELECT LastName, SUBSTRING(FirstName, 1, 1) AS Initial FROM Person.Contact WHERE LastName like 'Ba%' ORDER BY LastName;[/sub] Link to comment https://forums.phpfreaks.com/topic/183563-count-only-commas-outside-parenteses/#findComment-970305 Share on other sites More sharing options...
cags Posted December 3, 2009 Share Posted December 3, 2009 Assumably the only place unwanted commas will appear will be inside function calls. How about using preg_replace to take them out of the string before counting the commas. preg_replace("#\([^)]*\)#", "", $input); Link to comment https://forums.phpfreaks.com/topic/183563-count-only-commas-outside-parenteses/#findComment-970333 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.