NerdConcepts Posted July 20, 2007 Share Posted July 20, 2007 I am trying to count the number of pieces of an explode so that I can run a foreach command. My code goes out and grabs work order codes from the database. Then runs and explode command to separate those codes. But I have to count how many codes there are to be able to run a foreach. Any ideas? Here is my code. example of wo_coding: E)|$$|JH|KA so the count would be 4. the code I have so far: $id = $_GET['view']; $query = "SELECT wo_coding FROM work_orders WHERE "; $result = mysql_query($result); $row = mysql_fetch_array($result, MYSQL_ASSOC); $coding = $row['wo_coding']; $excode = explode("|", $coding); // counting of elements in $excode would go here. //foreach stuff would be here. some have counts up to 20/30. But almost all are different, so I cannot just put a static number in there are it would throw an error. Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/ Share on other sites More sharing options...
lightningstrike Posted July 20, 2007 Share Posted July 20, 2007 $count = count($excode); I assume thats what you want. Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/#findComment-303500 Share on other sites More sharing options...
NerdConcepts Posted July 20, 2007 Author Share Posted July 20, 2007 Yeah, that is what I want. Never did it before, good to know it's that easy. Thanks Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/#findComment-303538 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 Ummm.... why not just use a foreach loop? $str = 'this|is|something|to|explode'; $e = explode("\t", $str); foreach($e as $v) echo $v . '<br />'; $i = 0; $c = count($e); while($i < $c) echo $e[$i] . '<br />'; for($i = 0; $i < $c; $i++) echo $e[$i] . '<br /'; All of those would do the exact same thing.... Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/#findComment-303552 Share on other sites More sharing options...
NerdConcepts Posted July 20, 2007 Author Share Posted July 20, 2007 I figured maybe an easier way, and it works. $id = $_GET['view']; $query = "SELECT wo_coding FROM work_orders WHERE wo_num='$id'"; $result = mysql_query($query); $row = mysql_fetch_array($result, MYSQL_ASSOC); $coding = $row['wo_coding']; $excode = explode("|", $coding); $ctr = -1; foreach ($excode as $x) { $ctr++; echo $x . '<br />'; } the echo in the foreach will be replaced but it still works great. Not sure why I thought I needed a count...but thanks a bunch anyways. Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/#findComment-303597 Share on other sites More sharing options...
corbin Posted July 20, 2007 Share Posted July 20, 2007 Hehe... no problem ;p Link to comment https://forums.phpfreaks.com/topic/60994-solved-explode-array-counting/#findComment-303609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.