Xtremer360 Posted February 28, 2011 Share Posted February 28, 2011 Lets say I pull a value from a database field that has a value of 1,2 and what I want to do is seperate them and then run each of those values against a separate table how would that be accomplished. Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/ Share on other sites More sharing options...
litebearer Posted February 28, 2011 Share Posted February 28, 2011 explode using the comma as the delimiter Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180600 Share on other sites More sharing options...
MattDunbar Posted February 28, 2011 Share Posted February 28, 2011 Depending on the version of mysql, you might be able to get away with doing it all in one query. Regardless, here's a decent way to do it, using 2 queries that will work on nearly any version of MySQL: $result = '1,2,3';//in reality, you're going to get this through a query instead, not assigning it by hand. $newQuery = 'SELECT * FROM table2 WHERE somevalue IN ('.$result.')'; You could also explode it as mentioned above, and individually deal with the records in a loop. E.g. $result = '1,2,3';//in reality, you're going to get this through a query instead, not assigning it by hand. $resultArray = explode(',',$result); for($i=0;$i<sizeof($resultArray);$i++) { $newQuery = 'SELECT * FROM table2 WHERE somevalue = '.$resultArray[$i]; } Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180602 Share on other sites More sharing options...
Xtremer360 Posted February 28, 2011 Author Share Posted February 28, 2011 This is what I have and for the matchTypeID, titleID, and stipulationID all three of these could/could not have values and if they do then they could possibly have just one integer or they could have more than one separated by that comma. <?php $count=1; $query = "SELECT eventSegments.matchTypeID, eventSegments.titleID, eventSegments.stipulationID, eventSegments.segmentTitle, eventSegments.preview FROM eventSegments WHERE eventSegments.eventID = '$eventID' AND eventSegments.type ='match' ORDER BY eventSegments.sortOrder"; $result = mysql_query ($query); $numrows = mysql_numrows($result); while ($row = mysql_fetch_assoc($result)) { $fieldarray=array('titleName','matchType','stipulation','segmentTitle','preview'); foreach ($fieldarray as $fieldlabel) { if (isset($row[$fieldlabel])) { ${$fieldlabel} = $row[$fieldlabel]; } } if ($count != $numrows) { print "<h2 class=matchnum>Match ".$count."</h2>\n"; } else { print "<h2 class=matchnum>Main Event</h2>\n"; } if (!empty($stipulation)) { echo '<h3 class="title">'; if (!empty($titleName)) { echo $titleName . ' '; } echo 'Championship Match</h3>'; } print "<h3 class=match>".$segmentTitle."</h3>\n"; if (!empty($preview)) { print "<p class=blurb>".nl2br($preview)."</p>\n"; } $count++; } print "<p class=cardchange>Card Subject To Change.</p>\n"; } else { print "<p>No Forthcoming Events booked.</p>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180607 Share on other sites More sharing options...
Xtremer360 Posted February 28, 2011 Author Share Posted February 28, 2011 Any clue to how I"d work that in with my code? Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180846 Share on other sites More sharing options...
floridaflatlander Posted February 28, 2011 Share Posted February 28, 2011 Are you trying to get the number out of an array run a query on a table then get another number and run a query on it and so on? Quote Link to comment https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.