[Webinar] Streamline your web hosting managementRegister Today


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
Last Modified: 2013-12-12
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

Thank you in advance for any code or help you may provide
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.

Author Comment

ID: 19581537

this question is related to

i was in hopes to get both answered

ill explain a bit more in the other question

Author Comment

ID: 19581943
$thing = "1,4,6,3,7,5,2,1,5,4,3,";
$search = "1";
$count = substr_count($thing, $search);

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


Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

613 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question