1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

retrive defrent inserted data from mysql & compaer between it to show the graph

Discussion in 'PHP' started by abosirage, Jun 3, 2012.

  1. abosirage

    abosirage New Member

    Joined:
    Jul 29, 2010
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT
    Location:
    Tripoli-Libya
    hi every body
    I need your help friends
    I am inserting about 40 item in my database four time weekly , I want to get today insert date & the last inserted data in my php page to compare between them & show the graph
    I use select twice to get current and previous but the result first and last,
    my code is like
    Code:
    <?php 
       include ('config.php');
      $select = mysql_query("SELECT DISTINCT ...... FROM .... ORDER BY `id` desc") ;
    while($row = mysql_fetch_array($select))
    {
        $date[] = $row['.....'];    
     }
    $ert= $date[1];
    function isUnique($ert){ 
     
         return (array_unique($array) != $array); 
     }
     echo $ert;
    $result = mysql_query("SELECT * FROM ...... WHERE ....='.......' or ......='......' or ......='..........' or ORDER BY `id` desc LIMIT 4");
    ?>  
        <?php 
       include ('config.php');
    $result1 = mysql_query("SELECT * FROM ......... WHERE ......... ='......... ' or ......... ='......... ' or ......... ='......... ' or ORDER BY `id` asc LIMIT 4");
    
    ?>
     
    Last edited by a moderator: Jun 3, 2012
  2. pein87

    pein87 New Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    0
    Occupation:
    Web Dev
    Location:
    Limbo
    Your query already selects none duplicates because your using the DISTINCT keyword. You will not get duplicates using that keyword nor would you if you used UNIQUE. you could just get the last item and the date of today and use php to check the date difference.

    get last item using id column

    Code:
    SELECT date FROM table_name ORDER BY id DESC LIMIT 1
    or

    Code:
    SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1;
    the last method requires a proper date data type like DATETIME or TIMESTAMP and does not work on any VARCHAR types.

    Your array would then have three values year, month, and day which you can use php's built in date function to get these for the current day and subtract the values to see the time elapsed.

    PHP:
    <?php
    include("config.php");

    $Q mysql_query("SELECT DAY(date) AS lDay, MONTH(date) AS lMonth, YEAR(date) AS lYEAR FROM table_name ORDER BY id DESC LIMIT 1");

    $tDate = array(date("m"),date("d"),date("Y"));

    $R mysql_fetch_array($Q);

    if(
    $R['lDay'] == $tDate[1] && $R['lMonth'] == $tDate[0] && $R['lYear'] == $tDate[2])
    {

    // some code here that says everything was done today

    }
    else
    {

    // subtract the values using $tDate as the big number ie $tDate[0] - $R['Month']

    }

    ?>
    That isn't the best example of course but you could compare which one is greater only if the years are different and calculate how many days, months, and years have passed since the last insert.

    if you

    PHP:
    print_f($tDate);
    it shows

    Code:
    Array ( [0] => 06 [1] => 10 [2] => 2012 )
    you can also get todays date using this SQL

    Code:
    SELECT MONTH(NOW()) AS thisMonth, DAY(NOW()) AS thisDay, YEAR(NOW()) AS thisYEAR
    just giving you options to get todays date with code and compare your last entries date.
     
    Last edited: Jun 10, 2012
    shabbir likes this.

Share This Page