To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Newbies

Newbies Help for those who are just getting started

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-01-2009, 04:30 AM   #1
griever
Member
 
Join Date: Sep 2009
Posts: 70
how to sort authors into divs from left to right by newest post

I'm making a blog using wordpress with multiple authors and currently each author's posts are placed into a vertical div from left to right in a set order (ie tony, hippie, erik etc) and I am trying to sort the authors from left to right according to who has posted most recently. I was getting help on this site in another post about using arrays but it kind of changed topics so I thought I'd start a new thread. I've gotten no help at all from the wordpress.org site...thanks a lot!

tony

hippievstony.com


main index:

PHP Code:
<?php
get_header
();
?>
<div id="box">
    






<?php
$last
= wp_get_recent_posts( '1');
$last_id = $last['0']['ID'];
echo
$last_id;
?>

<div id="one">
     <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/tonyname.jpg"
     <?php $my_query = new WP_Query ( 'author_name=tony' );
     while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>
        
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->    
        
                   <?php $withcomments = 1; ?>
                   <?php comments_template(); ?>
            </div><!-- end post div -->    
<?php
endwhile;/*end tony while have posts*/

?>
    </div><!-- End one Div -->





    <div id="two">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/hippiename.jpg"
    <?php
    $my_query
= new WP_Query ( 'author_name=hippie' );
    while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>

        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>

        </div><!-- end post div -->    
<?php
endwhile;/*end hippie while have posts*/

?>
    </div><!-- End two Div -->



<div id="three">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/erikuname.jpg"
    <?php
    $my_query
= new WP_Query ( 'author_name=eriku' );
    while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>

        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>

        </div><!-- end post div -->    
<?php
endwhile;/*end eriku while have posts*/

?>
    </div><!-- End three Div -->


<div id="four">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/benname.jpg"
    <?php
    $my_query
= new WP_Query ( 'author_name=ben' );
    while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>

        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>

        </div><!-- end post div -->    
<?php
endwhile;/*end ben while have posts*/

?>
    </div><!-- End four Div -->



<div id="five">
    <img src="http://www.hippievstony.com/wp-content/uploads/2009/11/hingyiname.jpg"
    <?php
    $my_query
= new WP_Query ( 'author_name=hingyi' );
    while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>

        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->
        
            <?php $withcomments = 1; ?>
            <?php comments_template(); ?>

        </div><!-- end post div -->    
<?php
endwhile;/*end hingyi while have posts*/

?>
    </div><!-- End five Div -->




</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();
?>


index.css:

PHP Code:
/*  
Theme Name: Duel
Theme URI: hippievstony.com
Description: Side-By-Side Comparison Blog Mayhem
Version: 1.0
Author: tony+mostly rincewind456
Author URI: http://www.hippievstony.com

*/


#one{
     
float: left;
     
width: 400px;
     
     }

#two{
     
float: left;
     
width: 400px;
     
padding-left:15px;
     }

#three{
     
float: left;
     
width: 400px;
     
padding-left:15px;
     }

#four{
     
float: left;
     
width: 400px;
     
padding-left:15px;
     }

#five{
     
float: right;
     
width: 400px;
     
padding-left:15px;
     
padding-right:15px;
     }


body{
    
margin: 0;
    
font-family: Arial, Helvetica, Georgia, Sans-serif;
    
font-size: 12px;
    
text-align: left;
    
vertical-align: top;
    
background: #ffffff;
    
color: #000000;
}

a:link, a:visited{
    
text-decoration: underline;
    
color: #336699;
}

a:hover{
    
text-decoration: none;
}

#wrapper{
    
margin: 0 auto 0 15px;
    
width: 2290px;
    
text-align: left;
}

#header{
    
    
width: 2290px;
        
height: 545px;
        
background: url(http://www.hippievstony.com/wp-content/uploads/2009/11/headerpic.jpg) no-repeat;
        
padding-left:0px;
        }


#box{
    
float: left;
    
width: 2090px;
}



.
sidebar{
    
float: right;
    
width: 200px;
    
background: white;
    
margin: 200px 0 0 0px;
    
display: inline;
}

h2 {font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 110%;
color: black;
font-weight: bold;}

#sidebar h2 {font: font-size: 85%;
color: black; background: transparent;font-weight: bold; border-bottom: dashed 2px yellow; }

#sidebar ul {
            
margin-left: 20em;
            }

#sidebar ul li ul{
        
margin-left: 3.5em;
               }


#footer{
    
clear: both;
    
margin: auto;
    
width: 100%;
        
text-align: center;
}

img
{
border:0;
}




.
comments-template{
    
margin: 10px 0 0;
    
border-top: 1px solid #ccc;
    
padding: 0px 0 0;
}

.
comments-template ol{
    
margin: 0;
    
padding: 10px 0 15px;
    list-
style: none;
}

.
comments-template ol li{
    
margin: 10px 0 0;
    
line-height: 18px;
    
padding: 0 0 0px;
    
border-bottom: 1px solid #ccc;
}

.
comments-template h2, .comments-template h3{
    
font-family: Georgia, Sans-serif;
    
font-size: 16px;
}

.
commentmetadata{
    
font-size: 12px;
}

.
comments-template p.nocomments{
    
padding: 0;
}

.
comments-template textarea{
    
font-family: Arial, Helvetica, Georgia, Sans-serif;
    
font-size: 12px;
}

Last edited by griever; 12-01-2009 at 04:35 AM.
griever is offline   Reply With Quote
Old 12-01-2009, 07:58 AM   #2
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
I've no idea what you can or cannot do with a "WP_Query", so this may be of no help. The way I'd solve it is by querying the db for posts from all authors at once (and being more efficient in the same time since that's one query instead of four) and order by author ascending and time of last post descending.

Just used to show three divs with different border colours for the sake of this example's output. Your CSS will put them where you want to.
HTML Code:
<style type="text/css">
div {
	margin-bottom: 1pt;
}
#one {
	border: 1pt solid black;
}
#two {
	border: 1pt solid red;
}
#three {
	border: 1pt solid blue;
}
</style>
PHP Code:
// simulate mysql result set. your data will obviously come from your db.
class MockResult {
    
private $i;
    
private $mockData = array();
    
private $c;
    
    
public function MockResult() {
        
$this->mockData = array(
            array(    
'author_name'    => 'tony',
                    
'post_date'        => '2009-11-25'
            
),
            array(    
'author_name'    => 'tony',
                    
'post_date'        => '2009-11-20'
            
),
            array(    
'author_name'    => 'hippie',
                    
'post_date'        => '2009-11-28'
            
),
            array(    
'author_name'    => 'hippie',
                    
'post_date'        => '2009-11-27'
            
),
            array(    
'author_name'    => 'eric',
                    
'post_date'        => '2009-11-28'
            
),
            array(    
'author_name'    => 'tony',
                    
'post_date'        => '2009-11-21'
            
)
        );
        
$this->c = count($this->mockData);
        
$this->i = -1;
    }
    
public function fetch_assoc() {
        ++
$this->i;
        if (
$this->i >= $this->c)
            return
false;
        return
$this->mockData[$this->i];
    }
}

// This is where your code would start with the proper query instead of my fake one.
$res = new MockResult();

if (
$res) {
    while (
$row = $res->fetch_assoc()) {
        
/* array key must be unique, so if more than one person has the same name
           you need to use $row['author_name'] . '_' . $row['author_id']
           The name is used for ordering. If you don't care about that, then author_id is fine.
            (and if you don't need krsort and ksort below, just the author_id is fine */
        
$author[$row['author_name']][] = $row;
    }
    foreach(
$author as $k => $v) {
        
/*    $v[0] is the first post for each author.
                since it's sorted by date DESC, it's the latest post by that poster
            $v is the array of all posts for that author.
        */
        
$order[$v[0]['post_date']][$k] = $v;
    }

    
/* Both krsort and ksort below should not have to be used if you order your
        sql result properly. Unless I'm misstaken. Do double check it.
        I didn't sort my test data, so I needed this anyway.
    */
    // Sort by dates in descending order
    
krsort($order);

    
// for each date, sort by author name in ascending order
    
foreach ($order as $k => $v) {
        
ksort($order[$k], SORT_STRING);
    }
}
else {
    
// error
    
exit;
}

$divIds = array('one', 'two', 'three');
$i = 0;
foreach (
$order as $date => $authors) {
    foreach (
$authors as $author => $posts) {
        echo
'<div id="'.$divIds[$i++].'">';
            foreach(
$posts as $post) {
                echo
'By ' . $post['author_name'] . ' on ' . $post['post_date'] . '<br/>';
            }
        echo
'</div>';
    }
}

Last edited by johanafm; 12-01-2009 at 08:00 AM.
johanafm is offline   Reply With Quote
Old 12-02-2009, 05:35 AM   #3
griever
Member
 
Join Date: Sep 2009
Posts: 70
thanks a lot johana!


so I need to figure out how to assign the values to the array using a wp_query instead of the values you assigned to the array right? I think I need more help about how the keys are created and values assigned if you aren't directly assigning them like you show here:

array( 'author_name' => 'tony',
'post_date' => '2009-11-25');


If I figure out how to make the right query, where should I put the query so that the values get assigned into the array?

Last edited by griever; 12-02-2009 at 05:49 AM.
griever is offline   Reply With Quote
Old 12-02-2009, 07:17 AM   #4
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
I just simulated the behaviour of performing a query and using its result for mysql_fetch_assoc. So this part
PHP Code:
// This is where your code would start with the proper query instead of my fake one.
$res = new MockResult();

if (
$res) {
    while (
$row = $res->fetch_assoc()) {
would become something like
PHP Code:
// This is where your code would start with the proper query instead of my fake one.
$qry = 'The sql query';    // we get to this further down
$res = mysql_query($qry)

if (
$res) {
    while (
$row = $res->mysql_fetch_assoc()) {
Your fields (corresponding to the array element keys) may have other names than what I used, such as person instead of author, created or modified instead of post_date etc.
And you'd obviously need to get more information than just name and date, such as the actual post, title etc.
This information should let you handle the SQL yourself: http://codex.wordpress.org/Database_Description.
After a quick glance at http://core.trac.wordpress.org/brows...udes/query.php, I'd definitely forgo WP_query and handle this specific SQL query on my own.
Quote:
Currently, the only database supported by WordPress Version 2.7 is MySQL version 4.0 or greater.
So for one, we know that it's indeed mysql or mysqli you should use.

Looking at http://codex.wordpress.org/Database_Description/2.7 (the same db schema since 2.5), the tables you need are wp_posts and wp_users.

So the SQL query should look something like this
PHP Code:
$qry = "SELECT u.id, u.nicename, post_date, post_content, post_title
    FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
    WHERE nicename = 'tony' OR nicename = 'eric' OR nicename = 'hippie'
    ORDER BY nicename ASC, post_date DESC"
;
[/php]
johanafm is offline   Reply With Quote
Old 12-03-2009, 04:17 AM   #5
griever
Member
 
Join Date: Sep 2009
Posts: 70
Johana,

I put all the stuff together in my index.php file, I know I'm doing something stupid, because it's just outputting the code as text. For the query, can you explain what it is doing? And do I need to use the $wpdb variable they have setup to talk to the database? :



PHP Code:
<?php
get_header
();
?>




<div id="box">
    

$qry =  "SELECT u.id, u.nicename, post_date, post_content, post_title
    FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
    WHERE nicename = 'tony' OR nicename = 'eric' OR nicename = 'hippie'
    ORDER BY nicename ASC, post_date DESC";

$res = mysql_query($qry)

if ($res) {
    while ($row = $res->mysql_fetch_assoc()) {
    $author[$row['author_name']][] = $row;
    }
    foreach($author as $k => $v) {
        
        $order[$v[0]['post_date']][$k] = $v;
    }

    
    krsort($order);

   
    foreach ($order as $k => $v) {
        ksort($order[$k], SORT_STRING);
    }
}
else {
    // error
    exit;
}



$divIds = array('one', 'two', 'three', 'four', 'five');
$i = 0;
foreach ($order as $date => $authors) {
    foreach ($authors as $author => $posts) {
        echo '<div id="'.$divIds[$i++].'">';
            foreach($posts as $post) {
                echo 'By ' . $post['author_name'] . ' on ' . $post['post_date'] . '<br/>';
            }
        echo '</div>';
    }
}



</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();




?>

I was wondering where I should put the "Loop" that wordpress uses to output the posts. My old loops, one for each author, was:

PHP Code:
<div id="one">
     <img src="http://www.hippievstony.com/wp-content/uploads/2009/12/tonyname.jpg"
     <?php $my_query = new WP_Query ( 'author_name=tony' );
     while (
$my_query->have_posts () ) :
        
$my_query->the_post () ?>
        
        <div class="post">
        <h3>
        <a href="<?php the_permalink ();?>"><?php the_title ();?></a>
        </h3>
            <div class="entry">
                <?php the_content ();?>
            </div><!-- end entry div -->    
        
                   <?php $withcomments = 1; ?>
                   <?php comments_template(); ?>
            </div><!-- end post div -->    
<?php
endwhile;/*end tony while have posts*/

?>
    </div><!-- End one Div -->
I bet a lot of this is wordpress specific but any help is really appreciated
griever is offline   Reply With Quote
Old 12-03-2009, 08:47 AM   #6
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
Quote:
Originally Posted by griever View Post
(...) because it's just outputting the code as text.
Reason
PHP Code:
<?php
get_header
();
// drops out of php parsing mode here
?>

<!-- this is html and should not be parsed -->
<div id="box">

<!-- still not in php parse mode, but this is php code. add php opening tag... -->
$qry = ...
Quote:
Originally Posted by griever View Post
For the query, can you explain what it is doing?

And do I need to use the $wpdb variable they have setup to talk to the database? :
I've no idea if you know any SQL at all, so bear with me if I'm explaining things you know.
This part tells the db what fields your interested in. Think of these as columns in a spreadsheet, and you're telling the db which of these columns should be retrieved.
Code:
SELECT field1, field 2, ..., fieldN
Table in which to find the columns
Code:
FROM table1
One table is not enough. Inner join tells the db to combine the first table with the second table. Every post where table1.someColumn is equal to table2.aColumn will be joined together. If there is no matching value in table2, then no row is retrieved from table1. (Compare this to LEFT JOIN where every row in table1 is retrieved, and if someColumn has no matching value in aColumn, the columns from table2 are filled with null values for this row).
Code:
INNER JOIN table2 ON table1.someColumn = table2.aColumn

users
id    name
---  -------
1    tony
2    eric

posts
id  author     title
--  --------   ------------------------
1           1    Tony's first post
2           2    Eric's first post
3           2    Eric's second post

.... (for which author is neither 1 or 2) ...

115        2   Eric's third post


SELECT name, title
FROM users
INNER JOIN posts ON posts.author = author.id

This would link the user row for 'tony', id = 1, to the post with title "Tony's first post", author = 1, since id = author.
It would do the same for user 'eric', but get the posts with id 2, 3 and 115, where author = 2, since that matches eric's user id.
And the result is:
name     title
------     ------------------
tony       Tony's first post
eric        Eric's first post
eric        Eric's second post
eric        Eric's third post

(just never assume this comes in any specific order, unless you tell the db to order the result in some way)
The where clause tells the DB that we only want posts where a certain field or fields matches certain criteria
Code:
WHERE somefield = somevalue


WHERE nicename = 'eric' OR nicename like 'tony%'

Only include rows from users where the user's name is eric, or where the user's name starts with tony (tony_the_tiger would be ok, but not tiger_tony)
Order by tells the db how to order the result. ASC stands for ascending, DESC for descending, and ordering is done depending on the data type of the field. An int field will be sorted as number, where 9 comes before 10, while a text field like nicename will be ordered as a string, where 10 comes before 9.
First order by field2 from lowest to highest, then for any rows having the same field2 value, also order by field1 in descending orer.
Code:
ORDER BY field2 ASC, field1 DESC
And no, you do not have to use the instance of word press db class. There are some things I don't like about it at all. For example, it has a member method called prepare, which "prepares" a query, except that it has nothing to do with prepared statements. In other words, it has no correlation to functions like mysqli->prepare(). Moreover, it does some unnecessary things, such as replacing '%s' with %s, "%s" with %s, and then replacing %s with '%s'. The result is that whatever the user (you) uses, it will always turn out to be '%s' in the end. Why not just assume that the coder deals with writing correct code in the first place, or at least learn to do so when he recognizes his error?

Also, the code is riddled with the error supression operator @. And there are word press specific settings for how to deal with errors. Perhaps it's useful if you have no control over this for some reason.
It would be interesting to hear what others think, but I'd skip wpdb and do this with mysqli.

Quote:
Originally Posted by griever View Post
I was wondering where I should put the "Loop" that wordpress uses to output the posts. My old loops, one for each author, was:

I bet a lot of this is wordpress specific but any help is really appreciated
With my solution, you wouldn't. You'd use the loop I suggested in my first post.

Not really. Apart from the db schema they use, for which info is here, this is pretty much generic.
johanafm is offline   Reply With Quote
Old 12-08-2009, 02:00 AM   #7
griever
Member
 
Join Date: Sep 2009
Posts: 70
thanks for the explanation! I've been going over what's going on, and for now, it's getting to my "box" div and stopping. The page code stops there as well. It outputs the header but never calls out the sidebar or the footer. Maybe it is running into an error and exiting after that ksort section? Also I closed the php tag just before the end of the "box" div.

PHP Code:
<?php
get_header
();
?>




<div id="box">
    
<?php
$qry
=  "SELECT u.id, u.nicename, post_date, post_content, post_title
    FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
    WHERE nicename = 'tony' OR nicename = 'eric' OR nicename = 'hippie'
    ORDER BY nicename ASC, post_date DESC"
;


$res = mysql_query($qry);

if (
$res) {
    while (
$row = $res->mysql_fetch_assoc()) {
    
$author[$row['author_name']][] = $row;
    }
    foreach(
$author as $k => $v) {
        
        
$order[$v[0]['post_date']][$k] = $v;
    }

    
    
krsort($order);

   
    foreach (
$order as $k => $v) {
        
ksort($order[$k], SORT_STRING);
    }
}
else {
    
// error
    
exit;
}
?>



$divIds = array('one', 'two', 'three', 'four', 'five');
$i = 0;
foreach ($order as $date => $authors) {
    foreach ($authors as $author => $posts) {
        echo '<div id="'.$divIds[$i++].'">';
            foreach($posts as $post) {
                echo 'By ' . $post['author_name'] . ' on ' . $post['post_date'] . '<br/>';
            }
        echo '</div>';
    }
}




</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();




?>
griever is offline   Reply With Quote
Old 12-08-2009, 08:19 AM   #8
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
Where it says //error, you typically want to either echo (in development environment) or log errors.
PHP Code:
echo mysql_errno() . ': ' . mysql_error();
johanafm is offline   Reply With Quote
Old 12-08-2009, 09:38 PM   #9
griever
Member
 
Join Date: Sep 2009
Posts: 70
that is a real helpful function! - I got the sql errors sorted, I just had to rename some of the fields, now it is giving me:


Fatal error: Call to a member function mysql_fetch_assoc() on a non-object in /home1/hippievs/public_html/wp-content/themes/duel/index.php on line 20

which I found means that I might be calling for a field that doesn't exist? This is line 20:
while ($row = $res->mysql_fetch_assoc()) {
griever is offline   Reply With Quote
Old 12-09-2009, 05:01 AM   #10
griever
Member
 
Join Date: Sep 2009
Posts: 70
ok I fixed that by changing the language a bit:

while ($row = $res->mysql_fetch_assoc()) {

becomes:

while($row=mysql_fetch_assoc($res)) {

--------

now I get the error:

Cannot use string offset as an array for line 25 which is

$order[$v[0]['post_date']][$k] = $v;

here is my newest code:
PHP Code:
<?php
get_header
();
?>




<div id="box">
    
<?php
$qry
=  "SELECT u.id, user_nicename, post_date, post_content, post_title
         FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
         WHERE user_nicename = 'tony' OR user_nicename = 'eric' OR user_nicename = 'hippie'
         ORDER BY user_nicename ASC, post_date DESC"
;


$res = mysql_query($qry);

if (
$res) {
    while (
$row = mysql_fetch_assoc($res)) {
    
$author[$row['author_name']][] = $row;
    }
    foreach(
$author as $k => $v) {
        
          
$order[$v[0]['post_date']][$k] = $v;
    }

    
    
krsort($order);

   
    foreach (
$order as $k => $v) {
        
ksort($order[$k], SORT_STRING);
    }
}
else {
    
// error
    
echo mysql_errno();
    echo
mysql_error();
    exit;
}




$divIds = array('one', 'two', 'three', 'four', 'five');
$i = 0;
foreach (
$order as $date => $authors) {
    foreach (
$authors as $author => $posts) {
        echo
'<div id="'.$divIds[$i++].'">';
            foreach(
$posts as $post) {
                echo
'By ' . $post['author_name'] . ' on ' . $post['post_date'] . '<br/>';
            }
        echo
'</div>';
    }
}

?>


</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();




?>
griever is offline   Reply With Quote
Old 12-09-2009, 05:32 AM   #11
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
It could be because you have this a few lines up
PHP Code:
$author[$row['author_name']][] = $row;
While there is no field named 'author_name' in the SQL query. Either change 'author_name' to 'nicename' everywhere in the code, or change the SQL to be "SELECT ..., nicename AS author_name, ..."
johanafm is offline   Reply With Quote
Old 12-09-2009, 03:30 PM   #12
griever
Member
 
Join Date: Sep 2009
Posts: 70
I changed all the author_name to user_nicename but the same error pops up. Does this error have to do with a field being called out as a string instead of an int or vice versa?

PHP Code:
<?php
get_header
();
?>




<div id="box">
    
<?php
$qry
=  "SELECT u.id, user_nicename, post_date, post_content, post_title
         FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
         WHERE user_nicename = 'tony' OR user_nicename = 'eric' OR user_nicename = 'hippie'
         ORDER BY user_nicename ASC, post_date DESC"
;


$res = mysql_query($qry);

if (
$res) {
    while (
$row = mysql_fetch_assoc($res)) {
    
$author[$row['user_nicename']][] = $row;
    }
    foreach(
$author as $k => $v) {
        
        
$order[$v[0]['post_date']][$k] = $v;
    }

    
    
krsort($order);

   
    foreach (
$order as $k => $v) {
        
ksort($order[$k], SORT_STRING);
    }
}
else {
    
// error
    
echo mysql_errno();
    echo
mysql_error();
    exit;
}




$divIds = array('one', 'two', 'three', 'four', 'five');
$i = 0;
foreach (
$order as $date => $authors) {
    foreach (
$authors as $author => $posts) {
        echo
'<div id="'.$divIds[$i++].'">';
            foreach(
$posts as $post) {
                echo
'By ' . $post['user_nicename'] . ' on ' . $post['user_nicename'] . '<br/>';
            }
        echo
'</div>';
    }
}

?>


</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();




?>
griever is offline   Reply With Quote
Old 12-10-2009, 05:34 AM   #13
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
Almost. You can index into both arrays and strings in the same way
PHP Code:
$arr = array('0', '1');
$str = '01';

echo
$arr[0] . $arr[1];
echo
$str[0] . $str[1];

// but if you try to use the string offset $str[1] as an array, for example like this, you get that error
$str[1][] = 'a';
So, having another look at it, I thought you might have used $order somewhere earlier in your code. Try
PHP Code:
var_dump($order);
foreach(
$author as $k => $v) {
    
$order[$v[0]['post_date']][$k] = $v;
}
johanafm is offline   Reply With Quote
Old 12-10-2009, 04:03 PM   #14
griever
Member
 
Join Date: Sep 2009
Posts: 70
now the error message remained but output a little bit before:

string(4) "DESC"
Fatal error: Cannot use string offset as an array in /home1/hippievs/public_html/wp-content/themes/duel/index.php on line 26

PHP Code:
<?php
get_header
();
?>




<div id="box">
    
<?php
$qry
=  "SELECT u.id, user_nicename, post_date, post_content, post_title
         FROM wp_posts INNER JOIN wp_users AS u ON post_author = u.id
         WHERE user_nicename = 'tony' OR user_nicename = 'eric' OR user_nicename = 'hippie'
         ORDER BY user_nicename ASC, post_date DESC"
;


$res = mysql_query($qry);

if (
$res) {
    while (
$row = mysql_fetch_assoc($res)) {
    
$author[$row['user_nicename']][] = $row;
    }
    
var_dump($order);
    foreach(
$author as $k => $v) {
        
        
$order[$v[0]['post_date']][$k] = $v;
    }

    
    
krsort($order);

   
    foreach (
$order as $k => $v) {
        
ksort($order[$k], SORT_STRING);
    }
}
else {
    
// error
    
echo mysql_errno();
    echo
mysql_error();
    exit;
}




$divIds = array('one', 'two', 'three', 'four', 'five');
$i = 0;
foreach (
$order as $date => $authors) {
    foreach (
$authors as $author => $posts) {
        echo
'<div id="'.$divIds[$i++].'">';
            foreach(
$posts as $post) {
                echo
'By ' . $post['user_nicename'] . ' on ' . $post['user_nicename'] . '<br/>';
            }
        echo
'</div>';
    }
}

?>


</div><!-- end box div -->
<?php
get_sidebar
();
get_footer ();




?>
griever is offline   Reply With Quote
Old 12-10-2009, 04:07 PM   #15
johanafm
Senior Member
 
Join Date: Jul 2007
Posts: 1,239
Since $order has not been used in this little piece of code before the var_dump, that has been set before. Use some other variable instead. $orderByDate or whatever.
johanafm is offline   Reply With Quote
Reply

Bookmarks

Tags
sort divs by most recent


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:40 PM.






Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.