Jump to content

Items Repeat Themselves in PHP App...HELP...


jamesnaat

Recommended Posts

I have a web app. When admin logs in there is a dashboard showing a summary of various items. The one area I am having trouble with is the "Customer Recently Modified" section of this index page. I add a customer - no issues. When I modify a customer's info and go back to dashboard their information shows up twice under "Recently Modified" section of table.  :wtf: ANY help would be appreciated please see screen shot below).

 

Here is the related code:

 

Admin Index (Shows summary of various items) Relevant section is shown below (I have limited the number of items shown on the index page to 5)

              <?php foreach(Customer::getRecentlyModified(5) as $customer): ?>

              <tr>

                <td>

                  <?php echo $customer->getFirstName(); ?> <?php echo $customer->getLastName(); ?>

                  <small>- <?php echo $customer->getEmail(); ?></small><br />

                  <?php echo $customer->getCompany(); ?>

                </td>

                <td>

                  <?php

                  if($customer->getModifyDate()->format('Y-m-d H:i:s') !== '-0001-11-30 00:00:00')

                    echo ago(strtotime($customer->getModifyDate()->format('Y-m-d H:i:s')));

                  else echo ago(strtotime($customer->getCreateDate()->format('Y-m-d H:i:s')));

                  ?>

                </td>

              </tr>

              <?php endforeach; ?>

 

 

 

Functions to code above

 

public static function getRecentlyCreated($count=5)

  {

    $database = Database::getInstance();

    $customers = array();

    $result = $database->executeQuery('SELECT * FROM customer ORDER BY CreateDate DESC LIMIT ' . $count);

    if(!is_null($result))

    {

      foreach($result as $record)

      {

        $customers[] = Customer::get($record['ID']);

      }

    }

    return $customers;

  }

  public static function getRecentlyModified($count=5)

  {

    $database = Database::getInstance();

    $customers = array();

    $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer"

      . " UNION SELECT ID, ModifyDate AS Date FROM customer"

      . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count;

    $result = $database->executeQuery($sql);

    if(!is_null($result))

    {

      foreach($result as $record)

      {

        $customers[] = Customer::get($record['ID']);

      }

    }

    return $customers;

  }

post-136071-13482403740916_thumb.png

Link to comment
Share on other sites

  $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer"

      . " UNION SELECT ID, ModifyDate AS Date FROM customer"

      . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count;

 

Why are you doing that?

Link to comment
Share on other sites

  $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer"

      . " UNION SELECT ID, ModifyDate AS Date FROM customer"

      . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count;

 

Why are you doing that?

 

Why am I doing which part??

Link to comment
Share on other sites

I'm curious of your thought process on why you decided to use UNION. UNION is used to combine the results from multiple SELECT's into one result set. So, since you are basically running two SELECT's against the same table with the same criteria, you are getting duplicate results.

 

UNION is absolutely unnecessary for this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.