liquidskin Posted October 15, 2010 Share Posted October 15, 2010 I need to loop through my db and create strings from 2 columns on my db. data in column1 is '22,33,33,33,25,25,25,14,14,14,14,14,14' data in column2 is '11am,12pm,1pm,2pm,3pm,4pm,5pm,6pm,7pm,8pm,9pm,10pm, 11pm' the end result I need is a string such as: $body= "11am, 22 <line break> 12pm, 33 <line break> ...... If anyone has any examples of something like this or can get me started it would be much appreciated. Link to comment https://forums.phpfreaks.com/topic/215971-comma-separated-data-in-db-array/ Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 show us your code thus far Link to comment https://forums.phpfreaks.com/topic/215971-comma-separated-data-in-db-array/#findComment-1122588 Share on other sites More sharing options...
liquidskin Posted October 15, 2010 Author Share Posted October 15, 2010 I feel like I'm getting close... // separate array values by comma and store for ( $i = 0; $i < $rowcount; $i += 1) { $string1 = $precipData[$i]; $string2 = $hourData[$i]; $explode1[$i] = explode(",", $string1); //precip $explode2[$i] = explode(",", $string2); //hours $countHolder = $explode1[$i]; $countValuesInArray = count($countHolder); // loop through # of values in array for ( $j = 0; $j < $countValuesInArray; $j += 1) { $bodyBuild[$j] = $explode2[$i][$j]. "\n". $explode1[$i][$j]; } } $comma_separated = implode(",", $bodyBuild); echo $comma_separated; this outputs " 9pm 40,10pm 40,11pm 40,12am 40,1am 40,2am 40,3am 40,4am 40,5am 40,6am 40,7am 40,8am 20,9am 20" which is the last row in the db. I need to store this for each row, plus I need the linebreak. Link to comment https://forums.phpfreaks.com/topic/215971-comma-separated-data-in-db-array/#findComment-1122594 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2010 Share Posted October 15, 2010 Something like this should work: <?php $col1 = explode(',','22,33,33,33,25,25,25,14,14,14,14,14,14'); $col2 = explode(',','11am,12pm,1pm,2pm,3pm,4pm,5pm,6pm,7pm,8pm,9pm,10pm,11pm'); $tmp = array(); foreach ($col2 as $i=>$v) { $tmp[] = $v . ',' . $col1[$i]; } $body = implode("<br>\n",$tmp) . "<br>\n"; echo $body; ?> Ken Link to comment https://forums.phpfreaks.com/topic/215971-comma-separated-data-in-db-array/#findComment-1122601 Share on other sites More sharing options...
liquidskin Posted October 16, 2010 Author Share Posted October 16, 2010 Super helpful, thanks! Link to comment https://forums.phpfreaks.com/topic/215971-comma-separated-data-in-db-array/#findComment-1122715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.