views:

150

answers:

3

I'm new to PHP and have a two part question.

I need to take rows from two separate tables, and arrange them in descending order by their date. The rows do not correspond in order or number and have no relationship with each other.

---EDIT---

They each contain updates on a site, one table holds text, links, dates, titles etc. from a blog. The other has titles, links, specifications, etc. from images. I want to arrange some basic information (title, date, small description) in an updates section on the main page of the site, and for it to be in order of date.

Merging them into one table and modifying it to suit both types isn't what I'd like to do here, the blog table is Wordpress' standard wp_posts and I don't feel comfortable adding columns to make it suit the image table too. I'm afraid it could clash with upgrading later on and it seems like a clumsy solution (but that doesn't mean I'll object if people here advise me it's the best solution).
------EDIT 2------ Here are the DESCRIBES of each table:

mysql> describe images;
+---------+--------------+------+-----+-------------------+----------------+
| Field   | Type         | Null | Key | Default           | Extra          |
+---------+--------------+------+-----+-------------------+----------------+
| id      | int(11)      | NO   | PRI | NULL              | auto_increment |
| project | varchar(255) | NO   |     | NULL              |                |
| title   | varchar(255) | NO   |     | NULL              |                |
| time    | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
| img_url | varchar(255) | NO   |     | NULL              |                |
| alt_txt | varchar(255) | YES  |     | NULL              |                |
| text    | text         | YES  |     | NULL              |                |
| text_id | int(11)      | YES  |     | NULL              |                |
+---------+--------------+------+-----+-------------------+----------------+

mysql> DESCRIBE wp_posts;
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field                 | Type                | Null | Key | Default             | Extra          |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID                    | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| post_author           | bigint(20) unsigned | NO   |     | 0                   |                |
| post_date             | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_date_gmt         | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_content          | longtext            | NO   |     | NULL                |                |
| post_title            | text                | NO   |     | NULL                |                |
| post_excerpt          | text                | NO   |     | NULL                |                |
| post_status           | varchar(20)         | NO   |     | publish             |                |
| comment_status        | varchar(20)         | NO   |     | open                |                |
| ping_status           | varchar(20)         | NO   |     | open                |                |
| post_password         | varchar(20)         | NO   |     |                     |                |
| post_name             | varchar(200)        | NO   | MUL |                     |                |
| to_ping               | text                | NO   |     | NULL                |                |
| pinged                | text                | NO   |     | NULL                |                |
| post_modified         | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_modified_gmt     | datetime            | NO   |     | 0000-00-00 00:00:00 |                |
| post_content_filtered | text                | NO   |     | NULL                |                |
| post_parent           | bigint(20) unsigned | NO   | MUL | 0                   |                |
| guid                  | varchar(255)        | NO   |     |                     |                |
| menu_order            | int(11)             | NO   |     | 0                   |                |
| post_type             | varchar(20)         | NO   | MUL | post                |                |
| post_mime_type        | varchar(100)        | NO   |     |                     |                |
| comment_count         | bigint(20)          | NO   |     | 0                   |                |
+-----------------------+---------------------+------+-----+---------------------+----------------+

---END EDIT---

I can do this easily with a single table like this (I include it here in case I'm using an over-elaborate method without knowing it):

$content = mysql_query("SELECT post_title, post_text, post_date FROM posts ORDER BY post_date DESC");
while($row = mysql_fetch_array($content))
    {
     echo $row['post_date'], $row['post_title'], $row['post_text'];
    }

But how is it possible to call both tables into the same array to arrange them correctly? By correctly, I mean that they will intermix their echoed results based on their date. Maybe I'm looking at this from the wrong perspective, and calling them to a single array isn't the answer?

Additionally, I need a way to form a conditional expression based on which table they came from, so that rows from table 1 get echoed differently than rows from table 2? I want results from table 1 to be echoed differently (with different strings concatenated around them, I mean) for the purpose of styling them differently than those from table two. And vice versa. I know an if...else statement would work here, but I have no idea how can I write the expression that would determine which table the row is from.

All and any help is appreciated, thanks.

A: 
while($row = mysql_fetch_array($content)) 
    { 
     $key=$row['post_date'].'-'.$row['id'];
     $array[$key] = array("table"=>1,"row"=$row);
    } 

кsort($array) once you done with both tables.

Col. Shrapnel
+2  A: 

Maybe a SQL UNION is useful

SELECT * FROM (
    SELECT post_title, post_text, post_date, 'posts1' AS is_from FROM posts1
    UNION
    SELECT post_title, post_text, post_date, 'posts2' AS is_from FROM posts2 
) AS posts1_posts2 ORDER BY post_date DESC
clyfe
A: 

The PHP-based sorting options aren't always the best idea because you might want to paginate your results, at which point you run into difficulties.

The UNION-based query described above will work better, but you will need to tweak it to allow for the different schemas of the tables:

(
  Select 'Image' as data_type, id, title, time as date_posted, ....
  From images
)
UNION
(
  Select 'Post', ID as id, post_title as title, post_date as date_posted, ...
  From wd_posts
)
Order By date_posted Desc Limit $foo, $bar

The key is that you should pull the same number of columns from both tables. The column names don't have to be the same to make it work, but I've made them the same above.

I also think you should put indexes on images.time and wd_posts.post_date.

Mixed-format ordering by the time (timestamp) and post_date (datetime) fields will work on MySQL 5, I just tried it.

p.g.l.hall
-1 for copying my post instead of commenting...
clyfe