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

Posted on 2007-07-27
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
    LVL 17

    Accepted Solution

    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


    this question is related to

    i was in hopes to get both answered

    ill explain a bit more in the other question

    Author Comment

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

    Author Comment

                // 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    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 …

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now