Jump to content

Need some help badly!


studgate

Recommended Posts

can someone explain that code to me

I need to understand it so I can expand it.

Thank you in advance.

<?php $prev_id = get_previous_post(TRUE); $prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A'); $prev_date = strtotime($prevpost['post_date']); ?>

Link to comment
https://forums.phpfreaks.com/topic/56984-need-some-help-badly/
Share on other sites

Ok. lets walk you through it.

<?php $prev_id = get_previous_post(TRUE); $prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A'); $prev_date = strtotime($prevpost['post_date']); ?>

 

First part:

$prev_id = get_previous_post(TRUE);

this saves a variable called $prev_id as the result of a function called get_previous_post. It saves the value as either TRUE or FALSE. In this case, you are defining it as "TRUE".

 

Second part:

$prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A');

 

This puts a MySQL query command into a variable.  I am not sure why you would want to put "$wpdb->" in front, but there it assigns the value "TRUE" or "FALSE" to a variable called wpdb AND prevpost. This is not necessary. Consider revising.

 

Third:

$prev_date = strtotime($prevpost['post_date']);

This outputs the command STRING TO TIME. It converts a string into time and saves it into a variable called prevpost. I do not know why there is a ['post_date'] inside it. Consider revising to:

 

$prev_date = strtotime($_COOKIE['post_date']);

            or

$prev_date = strtotime($_SESSION['post_date']);

            or

$prev_date = strtotime($post_date);

 

Link to comment
https://forums.phpfreaks.com/topic/56984-need-some-help-badly/#findComment-281489
Share on other sites

Thanks bigbob for the explanation.

What I was trying to do was to call upon the previous

post in a wordpress database, so I need the previous

post information from the current one.

So if I have post 3, I want to call upon the content from the 2nd post.

Somehow I am getting an error message.

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
SELECT * FROM wic_posts WHERE ID=

Any help is welcome.

Link to comment
https://forums.phpfreaks.com/topic/56984-need-some-help-badly/#findComment-281500
Share on other sites

Here it is:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

		<?php $prev_id = get_previous_post(TRUE); $prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A'); $prev_date = strtotime($prevpost['post_date']); ?>
<h2><?php previous_post_link('%link') ?></h2>
<small>Posted: <?php echo date('l, F jS, Y', $prev_date); ?></small><br />
<?php echo $prevpost['post_content']; ?>


		<?php endwhile; else: ?>

		<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

		<?php endif; ?>

Link to comment
https://forums.phpfreaks.com/topic/56984-need-some-help-badly/#findComment-281513
Share on other sites

here ya go all fixed up:

<?php 
if ( have_posts() ){
while ( have_posts() ) {
the_post();
}
}
$prev_id = get_previous_post(TRUE); $prevpost = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID={$prev_id->ID}", 'ARRAY_A'); $prev_date = strtotime($prevpost['post_date']); ?>
<h2><?php previous_post_link('%link') ?></h2>
<small>Posted: <?php echo date('l, F jS, Y', $prev_date); ?></small><br />
<?php echo $prevpost['post_content']; ?>


		<?php endwhile; else: ?>

		<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

		<?php endif; ?>

 

Try and see if it works =D

Link to comment
https://forums.phpfreaks.com/topic/56984-need-some-help-badly/#findComment-282677
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.