Get MySQL function clean-up and support required

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • atyndall
    New Member
    • Sep 2007
    • 13

    Get MySQL function clean-up and support required

    OK,
    Well I've got this script and I'd like to implement this function into it where a function retrieves a specified amount of data from a specific mysql database, rearranges a part of it, and outputs the result to another specified varible. this is what i've got so far (please ignore the long-winded variables, they are just for explanations):
    [PHP]function RearrangeDate ($input-data/time, $the-way-it-should-be-arranged-e.g.-D/m/y-(not implemented)) {
    $date = substr($timedat e, 0, 10);
    $year = substr($date, 0, 4);
    $month = substr($date, 5, 2);
    $day = substr($date, 8, 2);
    $newdate = $day . "/" . $month . "/" . $year;
    output $newdate
    }

    function GetWP ($amount-of-rows-to-output) {
    $sql =
    "SELECT `post_date`, `post_title`
    FROM `wp_posts`
    WHERE (post_type = 'post' AND post_status = 'publish')
    ORDER BY `wp_posts`.`pos t_date` DESC
    LIMIT 0,$amount-of-rows-to-output";
    $query = mysql_query($sq l);
    $array = mysql_fetch_row ($query);
    $timedate = $array[0];
    RearrangeDate($ timedate, d/m/y);
    $text = $newdate . " - " . $array[1];
    output $test
    }[/PHP]
    What it does is retrieves the mysql row and renames the variables (which once it is integrated into a function it wouldn't have to do). Then it passes the date/time variable to another function which strips the time and rearranges the date from American date to a specified format. Then the RearrangeDate function outputs the new arrangement and the GetWP function outputs the final variable arranged in a readable way.
    But this script only retrieves the first row and doesn't output the result properly. Can anyone fix this horrific script (as I only have basic PHP knowledge) and point out any areas where it would need a cleanup?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    But this script only retrieves the first row
    That is what you asked for unless you have multiple calls on the function GetWP()[PHP]$array = mysql_fetch_row ($query);[/PHP] What does the following mean?
    and doesn't output the result properly

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      In the current state of the code, you are only retrieving one row of data. You will need to create a loop if you want to retrieve multiple rows.

      You have not set a value to $newdate at the point in your code where you assign a value to test. You likely mean to return the value in RearrangeDate, and store the returned value in your $test variable.

      Comment

      • atyndall
        New Member
        • Sep 2007
        • 13

        #4
        opps.

        [PHP]output $test [/PHP]
        is ment to be
        [PHP]output $text[/PHP]

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by atyndall
          opps.

          [PHP]output $test [/PHP]
          is ment to be
          [PHP]output $text[/PHP]
          And does it work now?

          Comment

          • atyndall
            New Member
            • Sep 2007
            • 13

            #6
            No, but after fiddling around a bit I got it all to work the way I wanted it to, its posted below for anyone who whats to use it. GetWPData(); retrives data from wordpress tables and can be passed a table name and limit how many varibles or strings. RearrangeDateTi me(); rearranges a array of specified date and times to another specified way.

            [PHP]function GetWPData ($tablename, $limit) {
            $sql = "SELECT `post_date`, `post_title` FROM `wp_posts` WHERE (post_type = 'post' AND post_status = 'publish') ORDER BY `wp_posts`.`pos t_date` DESC LIMIT 0,$limit";
            $query = mysql_query($sq l);
            $num = mysql_numrows($ query);
            $i=0;
            while ($i < $num) {
            $table = mysql_result($q uery,$i,"$table name");
            $tablearray["$i"] = $table;
            $i++;
            }
            return $tablearray;
            }

            function RearrangeDateTi me ($var) {
            $i=0;
            foreach ($var as $td) {
            $date = substr($td, 0, 10);
            $time = substr($td, 11, 8);
            $y = substr($date, 0, 4);
            $M = substr($date, 5, 2);
            $d = substr($date, 8, 2);
            $h = substr($time, 0, 2);
            $m = substr($time, 3, 2);
            $s = substr($time, 6, 2);

            $format = "$d/$M/$y $h:$m";
            // $d = day, $M = month, $y = year. $h = hours, $m = minutes, $s = seconds.

            $array["$i"] = $format;
            $i++;
            }
            return $array;
            }[/PHP]

            Enjoy!

            Comment

            Working...