[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# how to count from a given string of '2,2,5,5,7,9,9' the occurrences  of multibles

Posted on 2007-07-27
Medium Priority
256 Views
i want to be able to count and print out the occurrences of multiple finds in the string '2,2,5,5,7,9,9'

so it would display something like
-------------------
2x - 2
2x - 5
1x - 7
2x - 9
------------------
NOTE: the single 7

i need this so i can make a loop to send to a mysql quarry its the product numbers for the ID field
so i need to pass the value of 2,5,7,9 to the WHERE `ID`='".\$Products_Ordered_found."' for each ID number

i know i need a while statement but i don't understand how to split the occurrences of Products_Ordered {=the '2,2,5,5,7,9,9'  sting}
nor do i understand how to pass the \$Products_Ordered_found numbers of 2,5,7,9 when they are parsed

basically count occurrences and then send the single finds to a mysql where ID = in a loop so they can be display the info of each record

0
Question by:Johnny
• 3

LVL 17

Accepted Solution

psimation earned 1500 total points
ID: 19581498
For the second part, ie, once you have the collection of numbers, you can use mysql's IN statement, ie, where ID IN \$Products_Ordered_found.

For the first part, you can use the "extract()" function to split the sting by , and retrn the "peices" in an array, then you can use some other array functions to see how many times element[o] is in the array.
0

Author Comment

ID: 19581537
psimation;

this question is related to
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_22724748.html

i was in hopes to get both answered

ill explain a bit more in the other question
0

Author Comment

ID: 19581943
<?php
\$thing = "1,4,6,3,7,5,2,1,5,4,3,";
\$search = "1";
\$count = substr_count(\$thing, \$search);
?>
0

Author Comment

ID: 19581948
<?
// start
echo "products: ".\$Products_Ordered."<BR>";
//\$Totaled_Products = '2';//count of \$Products_Ordered

\$query2 = "SELECT * FROM `products` WHERE `ID` IN (".\$Products_Ordered.")";
\$result2 = mysql_query(\$query2);

while (\$row2 = mysql_fetch_row(\$result2)) {
if (DEBUG) {
// mysql_info ();
// print out all the rows
echo "<font color='red'><b><BR>-----------------------<BR>ROW_VARS</b></font>";
echo "<pre>";
print_r(\$row2);
echo "</pre>";
}
\$thing = \$Products_Ordered;
\$search = \$row2[0];
\$Totaled_Products = substr_count(\$thing, \$search);
echo "<tr><td valign='top'>".\$Totaled_Products . "x </td><td nowrap> Product - "  . \$row2[3] . ", " . \$row2[5] . "</td><td align='right' valign='top'> \$" . \$row2[13]."</td></tr>";
}

//end
?>
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question