Jump to content
Pardon our ads (a necessary update) ×

Separating by comma


Xtremer360

Recommended Posts

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];
}

Link to comment
https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180602
Share on other sites

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/229082-separating-by-comma/#findComment-1180607
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.