Jump to content

Same content showing please help!


buffsteve24

Recommended Posts

Ok so basically I have created a simple blog for my website which does what I want it to do pretty much I'm having a problem with the date though If I echo the date out in to my page it just shows the main body of my content and not the actual date :/

the other problem is when I am echoing the blog into my page It is showing the same content two or three times instead of selecting the other content in the db I currently have two articles in my db but it will only show the article with  the id of 1 instead of outputting id 1 and id 2 into there respective places on the page

 

 

<?php

$mysql = new mysqli('localhost', 'root', 'root', 'blog') or die('There was a problem connecting to the database');
$stmt = $mysql->prepare('SELECT id, title, date, author, category, LEFT(body, 600) AS excerpt, body from posts ') or die('Problem preparing the query');
$stmt->execute();
$stmt->bind_result($id, $title, $body, $author, $category, $excerpt, $date);

while($row = $stmt->fetch()) :
    $lastspaceindex = strrpos($excerpt, ' ');

?>    
    <?php endwhile; ?>

 

 

then where I am echoing them out here my code is

 

<div class="extra-wrap">
<span class="text3"><a href="blog.php?id=<?php echo $id; ?>" class="link1"><?php echo $title; ?> </a></span>
<div class="inner4">
<p> Posted by <b><?php echo $author; ?></b> in <b><?php echo $category; ?></b>  </p>
</div>
</div>
<div class="clear"></div>
<div class="page2-box2">
<figure class="page2-img1"><img src="images/page2-img2.jpg" alt=""></figure>
<div class="extra-wrap">
<?php echo substr($excerpt, 0, $lastspaceindex) . "<a href='blog.php?id=$id'>...</a>"; ?><br>
<span class="text3"><?php echo substr() . "<a href='blog.php?id=$id'>Read More</a>"; ?></span>
</div>

 

Link to comment
Share on other sites

For the date/body issue, have you tried switching the variables in the bind statement to match the column order in the prepare statement?

<?php
//...
 
$stmt = $mysql->prepare('SELECT id, title, date, author, category, LEFT(body, 600) AS excerpt, body from posts ') or die('Problem preparing the query');
$stmt->execute();
$stmt->bind_result($id, $title, $body, $author, $category, $excerpt, $date);
 
//...
?>
Edited by cyberRobot
Link to comment
Share on other sites

 

For the date/body issue, have you tried switching the variables in the bind statement to match the column order in the prepare statement?

<?php
//...
 
$stmt = $mysql->prepare('SELECT id, title, date, author, category, LEFT(body, 600) AS excerpt, body from posts ') or die('Problem preparing the query');
$stmt->execute();
$stmt->bind_result($id, $title, $body, $author, $category, $excerpt, $date);
 
//...
?>

Hi,

 

Thanks that worked a charm amazing it's always the little things like that you miss :)

Link to comment
Share on other sites

if you are still working on why only one (that last one) piece of information is being displayed, it's because your display code isn't inside of your while(){} loop. your display code is after the end of your while(){} loop and is only running once, after the while(){} loop has finished and it is using the last values fetched into the variables.

Edited by mac_gyver
Link to comment
Share on other sites

if you are still working on why only one (that last one) piece of information is being displayed, it's because your display code isn't inside of your while(){} loop. your display code is after the end of your while(){} loop and is only running once, after the while(){} loop has finished and it is using the last values fetched into the variables.

Thanks I will try that when I finish work and let you know if it solved it :)

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.